|
38 | 38 | <script>
|
39 | 39 | (function(){
|
40 | 40 | var dog = {
|
41 |
| - $ : function(id){ |
42 |
| - return document.querySelector('#' + id); |
| 41 | + $ : function(selector){ |
| 42 | + return document.querySelector(selector); |
43 | 43 | },
|
44 | 44 | on: function(el, type, handler){
|
45 | 45 | el.addEventListener(type, handler, false);
|
46 | 46 | },
|
47 | 47 | off: function(el, type, handler){
|
48 |
| - el.removeEventListener(type, handler, false); |
| 48 | + el.removeEventListener(type, handler); |
49 | 49 | },
|
50 | 50 | position : function(obj, attr){
|
51 | 51 | if(obj){
|
52 | 52 | var method = obj.getBoundingClientRect();
|
53 | 53 | return attr ? method[attr] : method;
|
54 | 54 | } else {
|
55 |
| - alert('对象不存在!'); |
| 55 | + console.log('对象不存在!'); |
56 | 56 | }
|
57 | 57 | },
|
58 | 58 | cache : {},
|
|
74 | 74 | }
|
75 | 75 | }
|
76 | 76 | function Calendar(){
|
77 |
| - var args = arguments[0]; |
| 77 | + var args = arguments[0], |
| 78 | + dom = args.dom; |
78 | 79 |
|
79 |
| - for(var i in args){ |
80 |
| - this[i] = args[i]; |
| 80 | + for(var key in args){ |
| 81 | + if(key == 'dom'){ |
| 82 | + this.getDom(dom); |
| 83 | + } else { |
| 84 | + this[key] = args[key]; |
| 85 | + } |
81 | 86 | }
|
82 | 87 | this.init();
|
83 | 88 | }
|
84 | 89 | Calendar.prototype = {
|
85 | 90 | init : function(){
|
86 |
| - this.getDom(); |
87 | 91 | this.render();
|
88 |
| - this.hide(); |
89 |
| - }, |
90 |
| - getDom : function(){ |
91 |
| - this.container = dog.$(this.id); |
92 |
| - this.input = dog.$(this.input); |
| 92 | + this.setDom(); |
| 93 | + this.setHide(); |
| 94 | + // this.bindNext(); |
93 | 95 | },
|
94 | 96 | bind : function(){
|
95 | 97 |
|
| 98 | + }, |
| 99 | + _dom : { |
| 100 | + prev : '.prev', |
| 101 | + next : '.next', |
| 102 | + switch : '.switch' |
| 103 | + }, |
| 104 | + setDom : function(){ |
| 105 | + this.getDom(this._dom); |
| 106 | + }, |
| 107 | + getDom : function(dom){ |
| 108 | + for(var prop in dom){ |
| 109 | + this[prop] = dog.$(dom[prop]); |
| 110 | + } |
96 | 111 | },
|
97 | 112 | render : function(){
|
98 | 113 | var that = this,
|
|
102 | 117 | el = null,
|
103 | 118 | html = dog.tpl(this.tpl[0], data);
|
104 | 119 |
|
| 120 | + this.time = this.getFullTime(); |
| 121 | + |
105 | 122 | if(el = document.getElementById('my-calendar')){
|
106 | 123 | el.style.display = 'block';
|
107 | 124 | this.calendar = el;
|
108 | 125 | } else {
|
109 | 126 | createCalendar();
|
110 | 127 | that.fillWeek();
|
111 | 128 | that.fill();
|
| 129 | + that.fillYear(); |
112 | 130 | }
|
113 | 131 | function createCalendar(){
|
114 | 132 | div.innerHTML = that.tpl[0];
|
|
118 | 136 | div.style.left = pos.left + 'px';
|
119 | 137 | div.children[0].style.display = 'block';
|
120 | 138 | that.container.appendChild(div);
|
121 |
| - that.calendar = div; |
| 139 | + that.calendar = div; |
122 | 140 | }
|
123 | 141 | },
|
124 | 142 | fillWeek : function(){
|
|
132 | 150 | html += '</tr>';
|
133 | 151 | this.calendar.querySelector('.datetimepicker-days thead').innerHTML += html;
|
134 | 152 | },
|
135 |
| - fill : function(){ |
136 |
| - var html = '', |
137 |
| - calendar = this.calendar, |
| 153 | + fill : function(now){ |
| 154 | + var calendar = this.calendar, |
138 | 155 | tBody = calendar.querySelector('tbody'),
|
139 |
| - time = new Date(2016,3,1), |
140 |
| - year = time.getFullYear(), |
141 |
| - day = time.getDay(), |
142 |
| - today = time.getDate(), |
143 |
| - month = time.getMonth(), |
144 |
| - date = this.getDaysInMonth(year, month+1), |
| 156 | + time = now || this.time, |
| 157 | + date = this.getDaysInMonth(time.year, time.month), |
| 158 | + html = '', |
145 | 159 | n = 1,
|
146 | 160 | tr = null,
|
147 | 161 | td = null;
|
|
151 | 165 | for(var j = 0; j < 7; j++){
|
152 | 166 | td = tr.insertCell(j);
|
153 | 167 | if(n <= date){
|
154 |
| - td.innerHTML = day-- > 0 ? '' : (td.className = 'day', n++); |
| 168 | + td.innerHTML = time.week-- > 0 ? '' : (td.className = 'day', n++); |
155 | 169 | }
|
156 |
| - if(td.innerHTML == today){ |
| 170 | + if(td.innerHTML == time.day){ |
157 | 171 | td.className += ' active';
|
158 | 172 | }
|
159 | 173 | }
|
160 | 174 | }
|
161 | 175 |
|
162 | 176 | tBody.appendChild(tr);
|
163 | 177 | },
|
| 178 | + fillYear : function(){ |
| 179 | + var el = this.calendar.querySelector('.switch'), |
| 180 | + time = this.time; |
| 181 | + el.innerHTML = time.year + '年' + time.month + '月'; |
| 182 | + }, |
| 183 | + getFullTime : function(){ |
| 184 | + var time = new Date(2016,3,1), |
| 185 | + year = time.getFullYear(), |
| 186 | + week = time.getDay(), |
| 187 | + day = time.getDate(), |
| 188 | + month = time.getMonth(); |
| 189 | + |
| 190 | + return { |
| 191 | + year : year, |
| 192 | + week : week, |
| 193 | + day : day, |
| 194 | + month : month + 1 |
| 195 | + } |
| 196 | + }, |
| 197 | + /** |
| 198 | + * 得到一个月多少天 |
| 199 | + * @param {[type]} year [description] |
| 200 | + * @param {[type]} month [description] |
| 201 | + * @return {[type]} [description] |
| 202 | + */ |
164 | 203 | getDaysInMonth : function(year, month){
|
165 | 204 | return new Date(year, parseInt(month), 0).getDate();
|
166 | 205 | },
|
167 |
| - hide : function(){ |
168 |
| - var that = this; |
| 206 | + /** |
| 207 | + * 点击其它地方隐藏 |
| 208 | + * @return {[type]} [description] |
| 209 | + */ |
| 210 | + setHide : function(){ |
| 211 | + var that = this, |
| 212 | + target, |
| 213 | + nextEvent = that.nextHandle(that); |
| 214 | + |
169 | 215 | dog.on(document, 'click', function(e){
|
170 |
| - that.calendar.style.display = 'none'; |
| 216 | + that.hide(); |
| 217 | + dog.off(that.next, 'click', nextEvent); |
171 | 218 | });
|
| 219 | + |
172 | 220 | dog.on(this.calendar, 'click', function(e){
|
173 |
| - if(e.target.className.indexOf('day') != -1){ |
174 |
| - that.calendar.style.display = 'none'; |
| 221 | + target = e.target; |
| 222 | + |
| 223 | + if(target.className.indexOf('day') != -1){ |
| 224 | + that.hide(); |
| 225 | + that.setTime.call(that, target.innerHTML); |
175 | 226 | }
|
176 | 227 | e.stopPropagation();
|
177 | 228 | });
|
| 229 | + |
| 230 | + dog.on(that.next, 'click', nextEvent); |
| 231 | + }, |
| 232 | + setTime : function(val){ |
| 233 | + var time = this.time; |
| 234 | + this.input.value = time.year + '-' + time.month + '-' + val; |
| 235 | + }, |
| 236 | + hide : function(){ |
| 237 | + this.calendar.style.display = 'none'; |
| 238 | + }, |
| 239 | + bindNext : function(){ |
| 240 | + var that = this; |
| 241 | + dog.on(this.next, 'click', that.nextHandle(this)); |
| 242 | + }, |
| 243 | + nextHandle : function(self){ |
| 244 | + var that = this; |
| 245 | + return function(){ |
| 246 | + console.log(self); |
| 247 | + } |
178 | 248 | }
|
179 | 249 | }
|
180 | 250 | var defaults = {
|
181 |
| - id : 'calendar', |
182 |
| - input : 'input', |
| 251 | + dom : { |
| 252 | + container : '#calendar', |
| 253 | + input : '#input' |
| 254 | + }, |
183 | 255 | zh : {
|
184 |
| - days : ['日','一','二','三','四','五','六'], |
185 |
| - months : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'], |
186 |
| - monthsShort : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一','十二'], |
187 |
| - today : '今天', |
188 |
| - clear : '清空' |
| 256 | + days : ['日','一','二','三','四','五','六'], |
| 257 | + months : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'], |
| 258 | + monthsShort : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一','十二'], |
| 259 | + today : '今天', |
| 260 | + clear : '清空' |
189 | 261 | },
|
190 | 262 | tpl :
|
191 | 263 | [
|
|
195 | 267 | '<thead>' +
|
196 | 268 | '<tr>' +
|
197 | 269 | '<th class="prev"><i class="icon-arrow-left"></i></th>' +
|
198 |
| - '<th colspan="5" class="switch">12123123</th>' + |
| 270 | + '<th colspan="5" class="switch"></th>' + |
199 | 271 | '<th class="next"><i class="icon-arrow-right"></i></th>' +
|
200 | 272 | '</tr>' +
|
201 | 273 | '</thead>' +
|
|
0 commit comments