Best JavaScript code snippet using devicefarmer-stf
laydate.dev.js
Source:laydate.dev.js
1/**2 3 @Name : layDate v1.1 æ¥ææ§ä»¶4 @Author: è´¤å¿5 @Date: 2014-06-256 @QQ群ï¼1760471957 @Siteï¼http://sentsin.com/layui/laydate8 9 */10;!function(win){11//å
¨å±é
ç½®ï¼å¦æéç¨é»è®¤åä¸éè¦æ¹å¨12var config = {13 path: '', //laydateæå¨è·¯å¾14 defSkin: 'default', //åå§åç®è¤15 format: 'YYYY-MM-DD', //æ¥ææ ¼å¼16 min: '1900-01-01 00:00:00', //æå°æ¥æ17 max: '2099-12-31 23:59:59', //æ大æ¥æ18 isv: false19};20var Dates = {}, doc = document, creat = 'createElement', byid = 'getElementById', tags = 'getElementsByTagName';21var as = ['laydate_box', 'laydate_void', 'laydate_click', 'LayDateSkin', 'skins/', '/laydate.css'];22//主æ¥å£23win.laydate = function(options){24 options = options || {};25 try{26 as.event = win.event ? win.event : laydate.caller.arguments[0];27 } catch(e){};28 Dates.run(options);29 return laydate;30};31laydate.v = '1.1';32//è·åç»ä»¶åæ¾è·¯å¾33Dates.getPath = (function(){34 var js = document.scripts, jsPath = js[js.length - 1].src;35 return config.path ? config.path : jsPath.substring(0, jsPath.lastIndexOf("/") + 1);36}());37Dates.use = function(lib, id){38 var link = doc[creat]('link');39 link.type = 'text/css';40 link.rel = 'stylesheet';41 link.href = Dates.getPath + lib + as[5];42 id && (link.id = id);43 doc[tags]('head')[0].appendChild(link);44 link = null;45};46Dates.trim = function(str){47 str = str || '';48 return str.replace(/^\s|\s$/g, '').replace(/\s+/g, ' ');49};50//è¡¥é½æ°ä½51Dates.digit = function(num){52 return num < 10 ? '0' + (num|0) : num;53};54Dates.stopmp = function(e){55 e = e || win.event;56 e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;57 return this;58};59Dates.each = function(arr, fn){60 var i = 0, len = arr.length;61 for(; i < len; i++){62 if(fn(i, arr[i]) === false){63 break64 }65 }66};67Dates.hasClass = function(elem, cls){68 elem = elem || {};69 return new RegExp('\\b' + cls +'\\b').test(elem.className);70};71Dates.addClass = function(elem, cls){72 elem = elem || {};73 Dates.hasClass(elem, cls) || (elem.className += ' ' + cls);74 elem.className = Dates.trim(elem.className);75 return this;76};77Dates.removeClass = function(elem, cls) {78 elem = elem || {};79 if (Dates.hasClass(elem, cls)) {80 var reg = new RegExp('\\b' + cls +'\\b');81 elem.className = elem.className.replace(reg, '');82 }83 return this;84};85//æ¸
é¤csså±æ§86Dates.removeCssAttr = function(elem, attr){87 var s = elem.style;88 if(s.removeProperty){89 s.removeProperty(attr);90 } else {91 s.removeAttribute(attr);92 }93};94//æ¾ç¤ºéè95Dates.shde = function(elem, type){96 elem.style.display = type ? 'none' : 'block';97};98//ç®æéæ©å¨99Dates.query = function(node){100 if(node && node.nodeType === 1){101 if(node.tagName.toLowerCase() !== 'input'){102 throw new Error('éæ©å¨elemé误');103 }104 return node;105 }106 var node = (Dates.trim(node)).split(' '), elemId = doc[byid](node[0].substr(1)), arr;107 if(!elemId){108 return;109 } else if(!node[1]){110 return elemId;111 } else if(/^\./.test(node[1])){112 var find, child = node[1].substr(1), exp = new RegExp('\\b' + child +'\\b');113 arr = []114 find = doc.getElementsByClassName ? elemId.getElementsByClassName(child) : elemId[tags]('*');115 Dates.each(find, function(ii, that){116 exp.test(that.className) && arr.push(that); 117 });118 return arr[0] ? arr : '';119 } else {120 arr = elemId[tags](node[1]);121 return arr[0] ? elemId[tags](node[1]) : '';122 }123};124//äºä»¶çå¬å¨125Dates.on = function(elem, even, fn){126 elem.attachEvent ? elem.attachEvent('on'+ even, function(){127 fn.call(elem, win.even);128 }) : elem.addEventListener(even, fn, false);129 return Dates;130};131//é»æmouseup132Dates.stopMosup = function(evt, elem){133 if(evt !== 'mouseup'){134 Dates.on(elem, 'mouseup', function(ev){135 Dates.stopmp(ev);136 });137 }138};139Dates.run = function(options){140 var S = Dates.query, elem, devt, even = as.event, target;141 try {142 target = even.target || even.srcElement || {};143 } catch(e){144 target = {};145 }146 elem = options.elem ? S(options.elem) : target;147 if(even && target.tagName){148 if(!elem || elem === Dates.elem){149 return;150 }151 Dates.stopMosup(even.type, elem);152 Dates.stopmp(even);153 Dates.view(elem, options);154 Dates.reshow();155 } else {156 devt = options.event || 'click';157 Dates.each((elem.length|0) > 0 ? elem : [elem], function(ii, that){158 Dates.stopMosup(devt, that);159 Dates.on(that, devt, function(ev){160 Dates.stopmp(ev);161 if(that !== Dates.elem){162 Dates.view(that, options);163 Dates.reshow();164 }165 });166 });167 }168};169Dates.scroll = function(type){170 type = type ? 'scrollLeft' : 'scrollTop';171 return doc.body[type] | doc.documentElement[type];172};173Dates.winarea = function(type){174 return document.documentElement[type ? 'clientWidth' : 'clientHeight']175};176//å¤æé°å¹´177Dates.isleap = function(year){178 return (year%4 === 0 && year%100 !== 0) || year%400 === 0;179};180//æ£æµæ¯å¦å¨æææ181Dates.checkVoid = function(YY, MM, DD){182 var back = [];183 YY = YY|0;184 MM = MM|0;185 DD = DD|0;186 if(YY < Dates.mins[0]){187 back = ['y'];188 } else if(YY > Dates.maxs[0]){189 back = ['y', 1];190 } else if(YY >= Dates.mins[0] && YY <= Dates.maxs[0]){191 if(YY == Dates.mins[0]){192 if(MM < Dates.mins[1]){193 back = ['m'];194 } else if(MM == Dates.mins[1]){195 if(DD < Dates.mins[2]){196 back = ['d'];197 }198 }199 }200 if(YY == Dates.maxs[0]){201 if(MM > Dates.maxs[1]){202 back = ['m', 1];203 } else if(MM == Dates.maxs[1]){204 if(DD > Dates.maxs[2]){205 back = ['d', 1];206 }207 }208 }209 }210 return back;211};212//æ¶åç§çæææ£æµ213Dates.timeVoid = function(times, index){214 if(Dates.ymd[1]+1 == Dates.mins[1] && Dates.ymd[2] == Dates.mins[2]){215 if(index === 0 && (times < Dates.mins[3])){216 return 1;217 } else if(index === 1 && times < Dates.mins[4]){218 return 1;219 } else if(index === 2 && times < Dates.mins[5]){220 return 1;221 }222 } else if(Dates.ymd[1]+1 == Dates.maxs[1] && Dates.ymd[2] == Dates.maxs[2]){223 if(index === 0 && times > Dates.maxs[3]){224 return 1;225 } else if(index === 1 && times > Dates.maxs[4]){226 return 1;227 } else if(index === 2 && times > Dates.maxs[5]){228 return 1;229 }230 }231 if(times > (index ? 59 : 23)){232 return 1;233 }234};235//æ£æµæ¥ææ¯å¦åæ³236Dates.check = function(){237 var reg = Dates.options.format.replace(/YYYY|MM|DD|hh|mm|ss/g,'\\d+\\').replace(/\\$/g, '');238 var exp = new RegExp(reg), value = Dates.elem[as.elemv];239 var arr = value.match(/\d+/g) || [], isvoid = Dates.checkVoid(arr[0], arr[1], arr[2]);240 if(value.replace(/\s/g, '') !== ''){241 if(!exp.test(value)){242 Dates.elem[as.elemv] = '';243 Dates.msg('æ¥æä¸ç¬¦åæ ¼å¼ï¼è¯·éæ°éæ©ã');244 return 1;245 } else if(isvoid[0]){246 Dates.elem[as.elemv] = '';247 Dates.msg('æ¥æä¸å¨æææå
ï¼è¯·éæ°éæ©ã');248 return 1;249 } else {250 isvoid.value = Dates.elem[as.elemv].match(exp).join();251 arr = isvoid.value.match(/\d+/g);252 if(arr[1] < 1){253 arr[1] = 1;254 isvoid.auto = 1;255 } else if(arr[1] > 12){256 arr[1] = 12;257 isvoid.auto = 1;258 } else if(arr[1].length < 2){259 isvoid.auto = 1;260 }261 if(arr[2] < 1){262 arr[2] = 1;263 isvoid.auto = 1;264 } else if(arr[2] > Dates.months[(arr[1]|0)-1]){265 arr[2] = 31;266 isvoid.auto = 1;267 } else if(arr[2].length < 2){268 isvoid.auto = 1;269 }270 if(arr.length > 3){271 if(Dates.timeVoid(arr[3], 0)){272 isvoid.auto = 1;273 };274 if(Dates.timeVoid(arr[4], 1)){275 isvoid.auto = 1;276 };277 if(Dates.timeVoid(arr[5], 2)){278 isvoid.auto = 1;279 };280 }281 if(isvoid.auto){282 Dates.creation([arr[0], arr[1]|0, arr[2]|0], 1);283 } else if(isvoid.value !== Dates.elem[as.elemv]){284 Dates.elem[as.elemv] = isvoid.value;285 }286 }287 }288};289//çææ¥æ290Dates.months = [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];291Dates.viewDate = function(Y, M, D){292 var S = Dates.query, log = {}, De = new Date();293 Y < (Dates.mins[0]|0) && (Y = (Dates.mins[0]|0));294 Y > (Dates.maxs[0]|0) && (Y = (Dates.maxs[0]|0));295 296 De.setFullYear(Y, M, D);297 log.ymd = [De.getFullYear(), De.getMonth(), De.getDate()];298 299 Dates.months[1] = Dates.isleap(log.ymd[0]) ? 29 : 28;300 301 De.setFullYear(log.ymd[0], log.ymd[1], 1);302 log.FDay = De.getDay();303 304 log.PDay = Dates.months[M === 0 ? 11 : M - 1] - log.FDay + 1;305 log.NDay = 1;306 307 //渲ææ¥308 Dates.each(as.tds, function(i, elem){309 var YY = log.ymd[0], MM = log.ymd[1] + 1, DD;310 elem.className = '';311 if(i < log.FDay){312 elem.innerHTML = DD = i + log.PDay;313 Dates.addClass(elem, 'laydate_nothis');314 MM === 1 && (YY -= 1);315 MM = MM === 1 ? 12 : MM - 1; 316 } else if(i >= log.FDay && i < log.FDay + Dates.months[log.ymd[1]]){317 elem.innerHTML = DD = i - log.FDay + 1;318 if(i - log.FDay + 1 === log.ymd[2]){319 Dates.addClass(elem, as[2]);320 log.thisDay = elem;321 }322 } else {323 elem.innerHTML = DD = log.NDay++;324 Dates.addClass(elem, 'laydate_nothis');325 MM === 12 && (YY += 1);326 MM = MM === 12 ? 1 : MM + 1; 327 }328 329 if(Dates.checkVoid(YY, MM, DD)[0]){330 Dates.addClass(elem, as[1]);331 }332 333 Dates.options.festival && Dates.festival(elem, MM + '.' + DD);334 elem.setAttribute('y', YY);335 elem.setAttribute('m', MM);336 elem.setAttribute('d', DD);337 YY = MM = DD = null;338 });339 340 Dates.valid = !Dates.hasClass(log.thisDay, as[1]);341 Dates.ymd = log.ymd;342 343 //éå®å¹´æ344 as.year.value = Dates.ymd[0] + 'å¹´';345 as.month.value = Dates.digit(Dates.ymd[1] + 1) + 'æ';346 347 //å®ä½æ348 Dates.each(as.mms, function(i, elem){349 var getCheck = Dates.checkVoid(Dates.ymd[0], (elem.getAttribute('m')|0) + 1);350 if(getCheck[0] === 'y' || getCheck[0] === 'm'){351 Dates.addClass(elem, as[1]);352 } else {353 Dates.removeClass(elem, as[1]);354 }355 Dates.removeClass(elem, as[2]);356 getCheck = null357 });358 Dates.addClass(as.mms[Dates.ymd[1]], as[2]);359 360 //å®ä½æ¶åç§361 log.times = [362 Dates.inymd[3]|0 || 0, 363 Dates.inymd[4]|0 || 0, 364 Dates.inymd[5]|0 || 0365 ];366 Dates.each(new Array(3), function(i){367 Dates.hmsin[i].value = Dates.digit(Dates.timeVoid(log.times[i], i) ? Dates.mins[i+3]|0 : log.times[i]|0);368 });369 370 //ç¡®å®æé®ç¶æ371 Dates[Dates.valid ? 'removeClass' : 'addClass'](as.ok, as[1]);372};373//èæ¥374Dates.festival = function(td, md){375 var str;376 switch(md){377 case '1.1':378 str = 'å
æ¦';379 break;380 case '3.8':381 str = 'å¦å¥³';382 break;383 case '4.5':384 str = 'æ¸
æ';385 break;386 case '5.1':387 str = 'å³å¨';388 break;389 case '6.1':390 str = 'å¿ç«¥';391 break;392 case '9.10':393 str = 'æå¸';394 break;395 case '10.1':396 str = 'å½åº';397 break;398 };399 str && (td.innerHTML = str);400 str = null;401};402//çæå¹´å表403Dates.viewYears = function(YY){404 var S = Dates.query, str = '';405 Dates.each(new Array(14), function(i){406 if(i === 7) {407 str += '<li '+ (parseInt(as.year.value) === YY ? 'class="'+ as[2] +'"' : '') +' y="'+ YY +'">'+ YY +'å¹´</li>';408 } else {409 str += '<li y="'+ (YY-7+i) +'">'+ (YY-7+i) +'å¹´</li>';410 }411 }); 412 S('#laydate_ys').innerHTML = str;413 Dates.each(S('#laydate_ys li'), function(i, elem){414 if(Dates.checkVoid(elem.getAttribute('y'))[0] === 'y'){415 Dates.addClass(elem, as[1]);416 } else {417 Dates.on(elem, 'click', function(ev){418 Dates.stopmp(ev).reshow();419 Dates.viewDate(this.getAttribute('y')|0, Dates.ymd[1], Dates.ymd[2]);420 });421 }422 });423};424//åå§åé¢æ¿æ°æ®425Dates.initDate = function(){426 var S = Dates.query, log = {}, De = new Date();427 var ymd = Dates.elem[as.elemv].match(/\d+/g) || [];428 if(ymd.length < 3){429 ymd = Dates.options.start.match(/\d+/g) || [];430 if(ymd.length < 3){431 ymd = [De.getFullYear(), De.getMonth()+1, De.getDate()];432 }433 }434 Dates.inymd = ymd;435 Dates.viewDate(ymd[0], ymd[1]-1, ymd[2]);436};437//æ¯å¦æ¾ç¤ºé¶ä»¶438Dates.iswrite = function(){439 var S = Dates.query, log = {440 time: S('#laydate_hms')441 };442 Dates.shde(log.time, !Dates.options.istime);443 Dates.shde(as.oclear, !('isclear' in Dates.options ? Dates.options.isclear : 1));444 Dates.shde(as.otoday, !('istoday' in Dates.options ? Dates.options.istoday : 1));445 Dates.shde(as.ok, !('issure' in Dates.options ? Dates.options.issure : 1));446};447//æ¹ä½è¾¨å«448Dates.orien = function(obj, pos){449 var tops, rect = Dates.elem.getBoundingClientRect();450 obj.style.left = rect.left + (pos ? 0 : Dates.scroll(1)) + 'px';451 if(rect.bottom + obj.offsetHeight/1.5 <= Dates.winarea()){452 tops = rect.bottom - 1; 453 } else {454 tops = rect.top > obj.offsetHeight/1.5 ? rect.top - obj.offsetHeight + 1 : Dates.winarea() - obj.offsetHeight;455 }456 obj.style.top = tops + (pos ? 0 : Dates.scroll()) + 'px';457};458//å¸éå®ä½459Dates.follow = function(obj){460 if(Dates.options.fixed){461 obj.style.position = 'fixed';462 Dates.orien(obj, 1);463 } else {464 obj.style.position = 'absolute';465 Dates.orien(obj);466 }467};468//çæè¡¨æ ¼469Dates.viewtb = (function(){470 var tr, view = [], weeks = [ 'æ¥', 'ä¸', 'äº', 'ä¸', 'å', 'äº', 'å
'];471 var log = {}, table = doc[creat]('table'), thead = doc[creat]('thead');472 thead.appendChild(doc[creat]('tr'));473 log.creath = function(i){474 var th = doc[creat]('th');475 th.innerHTML = weeks[i];476 thead[tags]('tr')[0].appendChild(th);477 th = null;478 };479 480 Dates.each(new Array(6), function(i){481 view.push([]);482 tr = table.insertRow(0);483 Dates.each(new Array(7), function(j){484 view[i][j] = 0;485 i === 0 && log.creath(j);486 tr.insertCell(j);487 });488 });489 490 table.insertBefore(thead, table.children[0]); 491 table.id = table.className = 'laydate_table';492 tr = view = null;493 return table.outerHTML.toLowerCase();494}());495//渲ææ§ä»¶éª¨æ¶496Dates.view = function(elem, options){497 var S = Dates.query, div, log = {};498 options = options || elem;499 Dates.elem = elem;500 Dates.options = options;501 Dates.options.format || (Dates.options.format = config.format);502 Dates.options.start = Dates.options.start || '';503 Dates.mm = log.mm = [Dates.options.min || config.min, Dates.options.max || config.max];504 Dates.mins = log.mm[0].match(/\d+/g);505 Dates.maxs = log.mm[1].match(/\d+/g);506 507 as.elemv = /textarea|input/.test(Dates.elem.tagName.toLocaleLowerCase()) ? 'value' : 'innerHTML';508 509 if(!Dates.box){510 div = doc[creat]('div');511 div.id = as[0];512 div.className = as[0];513 div.style.cssText = 'position: absolute;';514 div.setAttribute('name', 'laydate-v'+ laydate.v);515 516 div.innerHTML = log.html = '<div class="laydate_top">'517 +'<div class="laydate_ym laydate_y" id="laydate_YY">'518 +'<a class="laydate_choose laydate_chprev laydate_tab"><cite></cite></a>'519 +'<input id="laydate_y" readonly><label></label>'520 +'<a class="laydate_choose laydate_chnext laydate_tab"><cite></cite></a>'521 +'<div class="laydate_yms">'522 +'<a class="laydate_tab laydate_chtop"><cite></cite></a>'523 +'<ul id="laydate_ys"></ul>'524 +'<a class="laydate_tab laydate_chdown"><cite></cite></a>'525 +'</div>'526 +'</div>'527 +'<div class="laydate_ym laydate_m" id="laydate_MM">'528 +'<a class="laydate_choose laydate_chprev laydate_tab"><cite></cite></a>'529 +'<input id="laydate_m" readonly><label></label>'530 +'<a class="laydate_choose laydate_chnext laydate_tab"><cite></cite></a>'531 +'<div class="laydate_yms" id="laydate_ms">'+ function(){532 var str = '';533 Dates.each(new Array(12), function(i){534 str += '<span m="'+ i +'">'+ Dates.digit(i+1) +'æ</span>';535 });536 return str;537 }() +'</div>'538 +'</div>'539 +'</div>'540 541 + Dates.viewtb542 543 +'<div class="laydate_bottom">'544 +'<ul id="laydate_hms">'545 +'<li class="laydate_sj">æ¶é´</li>'546 +'<li><input readonly>:</li>'547 +'<li><input readonly>:</li>'548 +'<li><input readonly></li>'549 +'</ul>'550 +'<div class="laydate_time" id="laydate_time"></div>'551 +'<div class="laydate_btn">'552 +'<a id="laydate_clear">æ¸
空</a>'553 +'<a id="laydate_today">ä»å¤©</a>'554 +'<a id="laydate_ok">确认</a>'555 +'</div>'556 +(config.isv ? '<a href="http://sentsin.com/layui/laydate/" class="laydate_v" target="_blank">laydate-v'+ laydate.v +'</a>' : '')557 +'</div>';558 doc.body.appendChild(div); 559 Dates.box = S('#'+as[0]); 560 Dates.events();561 div = null;562 } else {563 Dates.shde(Dates.box);564 }565 Dates.follow(Dates.box);566 options.zIndex ? Dates.box.style.zIndex = options.zIndex : Dates.removeCssAttr(Dates.box, 'z-index');567 Dates.stopMosup('click', Dates.box);568 569 Dates.initDate();570 Dates.iswrite();571 Dates.check();572};573//éèå
é¨å¼¹åºå
ç´ 574Dates.reshow = function(){575 Dates.each(Dates.query('#'+ as[0] +' .laydate_show'), function(i, elem){576 Dates.removeClass(elem, 'laydate_show');577 });578 return this;579};580//å
³éæ§ä»¶581Dates.close = function(){582 Dates.reshow();583 Dates.shde(Dates.query('#'+ as[0]), 1);584 Dates.elem = null;585};586//转æ¢æ¥ææ ¼å¼587Dates.parse = function(ymd, hms, format){588 ymd = ymd.concat(hms);589 format = format || (Dates.options ? Dates.options.format : config.format);590 return format.replace(/YYYY|MM|DD|hh|mm|ss/g, function(str, index){591 ymd.index = ++ymd.index|0;592 return Dates.digit(ymd[ymd.index]);593 }); 594};595//è¿åæç»æ¥æ596Dates.creation = function(ymd, hide){597 var S = Dates.query, hms = Dates.hmsin;598 var getDates = Dates.parse(ymd, [hms[0].value, hms[1].value, hms[2].value]);599 Dates.elem[as.elemv] = getDates;600 if(!hide){601 Dates.close();602 typeof Dates.options.choose === 'function' && Dates.options.choose(getDates); 603 }604};605//äºä»¶606Dates.events = function(){607 var S = Dates.query, log = {608 box: '#'+as[0]609 };610 611 Dates.addClass(doc.body, 'laydate_body');612 613 as.tds = S('#laydate_table td');614 as.mms = S('#laydate_ms span');615 as.year = S('#laydate_y');616 as.month = S('#laydate_m');617 //æ¾ç¤ºæ´å¤å¹´æ618 Dates.each(S(log.box + ' .laydate_ym'), function(i, elem){619 Dates.on(elem, 'click', function(ev){620 Dates.stopmp(ev).reshow();621 Dates.addClass(this[tags]('div')[0], 'laydate_show');622 if(!i){623 log.YY = parseInt(as.year.value);624 Dates.viewYears(log.YY);625 }626 });627 });628 629 Dates.on(S(log.box), 'click', function(){630 Dates.reshow();631 });632 633 //åæ¢å¹´634 log.tabYear = function(type){ 635 if(type === 0){636 Dates.ymd[0]--;637 } else if(type === 1) {638 Dates.ymd[0]++;639 } else if(type === 2) {640 log.YY -= 14;641 } else {642 log.YY += 14;643 }644 if(type < 2){645 Dates.viewDate(Dates.ymd[0], Dates.ymd[1], Dates.ymd[2]);646 Dates.reshow();647 } else {648 Dates.viewYears(log.YY);649 }650 };651 Dates.each(S('#laydate_YY .laydate_tab'), function(i, elem){652 Dates.on(elem, 'click', function(ev){653 Dates.stopmp(ev);654 log.tabYear(i);655 });656 });657 658 659 //åæ¢æ660 log.tabMonth = function(type){661 if(type){662 Dates.ymd[1]++;663 if(Dates.ymd[1] === 12){664 Dates.ymd[0]++;665 Dates.ymd[1] = 0;666 } 667 } else {668 Dates.ymd[1]--;669 if(Dates.ymd[1] === -1){670 Dates.ymd[0]--;671 Dates.ymd[1] = 11;672 }673 }674 Dates.viewDate(Dates.ymd[0], Dates.ymd[1], Dates.ymd[2]);675 };676 Dates.each(S('#laydate_MM .laydate_tab'), function(i, elem){677 Dates.on(elem, 'click', function(ev){678 Dates.stopmp(ev).reshow();679 log.tabMonth(i);680 });681 });682 683 //éæ©æ684 Dates.each(S('#laydate_ms span'), function(i, elem){685 Dates.on(elem, 'click', function(ev){686 Dates.stopmp(ev).reshow();687 if(!Dates.hasClass(this, as[1])){688 Dates.viewDate(Dates.ymd[0], this.getAttribute('m')|0, Dates.ymd[2]);689 }690 });691 });692 693 //éæ©æ¥694 Dates.each(S('#laydate_table td'), function(i, elem){695 Dates.on(elem, 'click', function(ev){696 if(!Dates.hasClass(this, as[1])){697 Dates.stopmp(ev);698 Dates.creation([this.getAttribute('y')|0, this.getAttribute('m')|0, this.getAttribute('d')|0]);699 }700 });701 });702 703 //æ¸
空704 as.oclear = S('#laydate_clear');705 Dates.on(as.oclear, 'click', function(){706 Dates.elem[as.elemv] = '';707 Dates.close();708 });709 710 //ä»å¤©711 as.otoday = S('#laydate_today');712 Dates.on(as.otoday, 'click', function(){713 var now = new Date();714 Dates.creation([now.getFullYear(), now.getMonth() + 1, now.getDate()]);715 });716 717 //确认718 as.ok = S('#laydate_ok');719 Dates.on(as.ok, 'click', function(){720 if(Dates.valid){721 Dates.creation([Dates.ymd[0], Dates.ymd[1]+1, Dates.ymd[2]]);722 }723 });724 725 //éæ©æ¶åç§726 log.times = S('#laydate_time');727 Dates.hmsin = log.hmsin = S('#laydate_hms input');728 log.hmss = ['å°æ¶', 'åé', 'ç§æ°'];729 log.hmsarr = [];730 731 //çææ¶åç§æè¦åä¿¡æ¯732 Dates.msg = function(i, title){733 var str = '<div class="laydte_hsmtex">'+ (title || 'æ示') +'<span>Ã</span></div>';734 if(typeof i === 'string'){735 str += '<p>'+ i +'</p>';736 Dates.shde(S('#'+as[0]));737 Dates.removeClass(log.times, 'laydate_time1').addClass(log.times, 'laydate_msg');738 } else {739 if(!log.hmsarr[i]){740 str += '<div id="laydate_hmsno" class="laydate_hmsno">';741 Dates.each(new Array(i === 0 ? 24 : 60), function(i){742 str += '<span>'+ i +'</span>';743 });744 str += '</div>'745 log.hmsarr[i] = str;746 } else {747 str = log.hmsarr[i];748 }749 Dates.removeClass(log.times, 'laydate_msg');750 Dates[i=== 0 ? 'removeClass' : 'addClass'](log.times, 'laydate_time1');751 }752 Dates.addClass(log.times, 'laydate_show');753 log.times.innerHTML = str;754 };755 756 log.hmson = function(input, index){757 var span = S('#laydate_hmsno span'), set = Dates.valid ? null : 1;758 Dates.each(span, function(i, elem){759 if(set){760 Dates.addClass(elem, as[1]);761 } else if(Dates.timeVoid(i, index)){762 Dates.addClass(elem, as[1]);763 } else {764 Dates.on(elem, 'click', function(ev){765 if(!Dates.hasClass(this, as[1])){766 input.value = Dates.digit(this.innerHTML|0);767 }768 });769 } 770 });771 Dates.addClass(span[input.value|0], 'laydate_click');772 };773 774 //å±å¼éæ©775 Dates.each(log.hmsin, function(i, elem){776 Dates.on(elem, 'click', function(ev){777 Dates.stopmp(ev).reshow();778 Dates.msg(i, log.hmss[i]);779 log.hmson(this, i);780 });781 });782 783 Dates.on(doc, 'mouseup', function(){784 var box = S('#'+as[0]);785 if(box && box.style.display !== 'none'){786 Dates.check() || Dates.close();787 }788 }).on(doc, 'keydown', function(event){789 event = event || win.event;790 var codes = event.keyCode;791 //å¦æå¨æ¥ææ¾ç¤ºçæ¶åæå车792 if(codes === 13 && Dates.elem){793 Dates.creation([Dates.ymd[0], Dates.ymd[1]+1, Dates.ymd[2]]);794 }795 });796};797Dates.init = (function(){798 Dates.use('need');799 Dates.use(as[4] + config.defSkin, as[3]);800 Dates.skinLink = Dates.query('#'+as[3]);801}());802//éç½®å®ä½803laydate.reset = function(){804 (Dates.box && Dates.elem) && Dates.follow(Dates.box);805};806//è¿åæå®æ¥æ807laydate.now = function(timestamp, format){808 var De = new Date((timestamp|0) ? function(tamp){809 return tamp < 86400000 ? (+new Date + tamp*86400000) : tamp;810 }(parseInt(timestamp)) : +new Date);811 return Dates.parse(812 [De.getFullYear(), De.getMonth()+1, De.getDate()],813 [De.getHours(), De.getMinutes(), De.getSeconds()],814 format815 );816};817//ç®è¤éæ©818laydate.skin = function(lib){819 Dates.skinLink.href = Dates.getPath + as[4] + lib + as[5];820};...
multidatespicker.js
Source:multidatespicker.js
1/*2 * MultiDatesPicker v1.6.43 * http://multidatespickr.sourceforge.net/4 * 5 * Copyright 2014, Luca Lauretta6 * Dual licensed under the MIT or GPL version 2 licenses.7 */8(function( $ ){9 $.extend($.ui, { multiDatesPicker: { version: "1.6.4" } });10 11 $.fn.multiDatesPicker = function(method) {12 var mdp_arguments = arguments;13 var ret = this;14 var today_date = new Date();15 var day_zero = new Date(0);16 var mdp_events = {};17 18 function removeDate(date, type) {19 if(!type) type = 'picked';20 date = dateConvert.call(this, date);21 for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++)22 if(!methods.compareDates(this.multiDatesPicker.dates[type][i], date))23 return this.multiDatesPicker.dates[type].splice(i, 1).pop();24 }25 function removeIndex(index, type) {26 if(!type) type = 'picked';27 return this.multiDatesPicker.dates[type].splice(index, 1).pop();28 }29 function addDate(date, type, no_sort) {30 if(!type) type = 'picked';31 date = dateConvert.call(this, date);32 33 // @todo: use jQuery UI datepicker method instead34 date.setHours(0);35 date.setMinutes(0);36 date.setSeconds(0);37 date.setMilliseconds(0);38 39 if (methods.gotDate.call(this, date, type) === false) {40 this.multiDatesPicker.dates[type].push(date);41 if(!no_sort) this.multiDatesPicker.dates[type].sort(methods.compareDates);42 } 43 }44 function sortDates(type) {45 if(!type) type = 'picked';46 this.multiDatesPicker.dates[type].sort(methods.compareDates);47 }48 function dateConvert(date, desired_type, date_format) {49 if(!desired_type) desired_type = 'object';/*50 if(!date_format && (typeof date == 'string')) {51 date_format = $(this).datepicker('option', 'dateFormat');52 if(!date_format) date_format = $.datepicker._defaults.dateFormat;53 }54 */55 return methods.dateConvert.call(this, date, desired_type, date_format);56 }57 58 var methods = {59 init : function( options ) {60 var $this = $(this);61 this.multiDatesPicker.changed = false;62 63 var mdp_events = {64 beforeShow: function(input, inst) {65 this.multiDatesPicker.changed = false;66 if(this.multiDatesPicker.originalBeforeShow) 67 this.multiDatesPicker.originalBeforeShow.call(this, input, inst);68 },69 onSelect : function(dateText, inst) {70 var $this = $(this);71 this.multiDatesPicker.changed = true;72 73 if (dateText) {74 $this.multiDatesPicker('toggleDate', dateText);75 this.multiDatesPicker.changed = true;76 // @todo: this will be optimized when I'll move methods to the singleton.77 }78 79 if (this.multiDatesPicker.mode == 'normal' && this.multiDatesPicker.pickableRange) {80 if(this.multiDatesPicker.dates.picked.length > 0) {81 var min_date = this.multiDatesPicker.dates.picked[0],82 max_date = new Date(min_date.getTime());83 84 methods.sumDays(max_date, this.multiDatesPicker.pickableRange-1);85 86 // counts the number of disabled dates in the range87 if(this.multiDatesPicker.adjustRangeToDisabled) {88 var c_disabled, 89 disabled = this.multiDatesPicker.dates.disabled.slice(0);90 do {91 c_disabled = 0;92 for(var i = 0; i < disabled.length; i++) {93 if(disabled[i].getTime() <= max_date.getTime()) {94 if((min_date.getTime() <= disabled[i].getTime()) && (disabled[i].getTime() <= max_date.getTime()) ) {95 c_disabled++;96 }97 disabled.splice(i, 1);98 i--;99 }100 }101 max_date.setDate(max_date.getDate() + c_disabled);102 } while(c_disabled != 0);103 }104 105 if(this.multiDatesPicker.maxDate && (max_date > this.multiDatesPicker.maxDate))106 max_date = this.multiDatesPicker.maxDate;107 108 $this109 .datepicker("option", "minDate", min_date)110 .datepicker("option", "maxDate", max_date);111 } else {112 $this113 .datepicker("option", "minDate", this.multiDatesPicker.minDate)114 .datepicker("option", "maxDate", this.multiDatesPicker.maxDate);115 }116 }117 118 if(this.multiDatesPicker.originalOnSelect && dateText)119 this.multiDatesPicker.originalOnSelect.call(this, dateText, inst);120 121 },122 beforeShowDay : function(date) {123 var $this = $(this),124 gotThisDate = $this.multiDatesPicker('gotDate', date) !== false,125 isDisabledCalendar = $this.datepicker('option', 'disabled'),126 isDisabledDate = $this.multiDatesPicker('gotDate', date, 'disabled') !== false,127 areAllSelected = this.multiDatesPicker.maxPicks <= this.multiDatesPicker.dates.picked.length;128 129 var bsdReturn = [true, '', null];130 if(this.multiDatesPicker.originalBeforeShowDay)131 bsdReturn = this.multiDatesPicker.originalBeforeShowDay.call(this, date);132 133 bsdReturn[1] = gotThisDate ? 'ui-state-highlight '+bsdReturn[1] : bsdReturn[1];134 bsdReturn[0] = bsdReturn[0] && !(isDisabledCalendar || isDisabledDate || (areAllSelected && !bsdReturn[1]));135 return bsdReturn;136 }137 };138 139 // value have to be extracted before datepicker is initiated140 if($this.val()) var inputDates = $this.val()141 142 if(options) {143 // value have to be extracted before datepicker is initiated144 //if(options.altField) var inputDates = $(options.altField).val();145 if(options.separator) this.multiDatesPicker.separator = options.separator;146 if(!this.multiDatesPicker.separator) this.multiDatesPicker.separator = ', ';147 148 this.multiDatesPicker.originalBeforeShow = options.beforeShow;149 this.multiDatesPicker.originalOnSelect = options.onSelect;150 this.multiDatesPicker.originalBeforeShowDay = options.beforeShowDay;151 this.multiDatesPicker.originalOnClose = options.onClose;152 153 // datepicker init154 $this.datepicker(options);155 156 this.multiDatesPicker.minDate = $.datepicker._determineDate(this, options.minDate, null);157 this.multiDatesPicker.maxDate = $.datepicker._determineDate(this, options.maxDate, null);158 if(options.addDates) methods.addDates.call(this, options.addDates);159 160 if(options.addDisabledDates)161 methods.addDates.call(this, options.addDisabledDates, 'disabled');162 163 methods.setMode.call(this, options);164 } else {165 $this.datepicker();166 }167 $this.datepicker('option', mdp_events);168 169 // adds any dates found in the input or alt field170 if(inputDates) $this.multiDatesPicker('value', inputDates);171 172 // generates the new string of added dates173 var inputs_values = $this.multiDatesPicker('value');174 175 // fills the input field back with all the dates in the calendar176 $this.val(inputs_values);177 178 // Fixes the altField filled with defaultDate by default179 var altFieldOption = $this.datepicker('option', 'altField');180 if (altFieldOption) $(altFieldOption).val(inputs_values);181 182 // Updates the calendar view183 $this.datepicker('refresh');184 },185 compareDates : function(date1, date2) {186 date1 = dateConvert.call(this, date1);187 date2 = dateConvert.call(this, date2);188 // return > 0 means date1 is later than date2 189 // return == 0 means date1 is the same day as date2 190 // return < 0 means date1 is earlier than date2 191 var diff = date1.getFullYear() - date2.getFullYear();192 if(!diff) {193 diff = date1.getMonth() - date2.getMonth();194 if(!diff) 195 diff = date1.getDate() - date2.getDate();196 }197 return diff;198 },199 sumDays : function( date, n_days ) {200 var origDateType = typeof date;201 obj_date = dateConvert.call(this, date);202 obj_date.setDate(obj_date.getDate() + n_days);203 return dateConvert.call(this, obj_date, origDateType);204 },205 dateConvert : function( date, desired_format, dateFormat ) {206 var from_format = typeof date;207 var $this = $(this);208 209 if(from_format == desired_format) {210 if(from_format == 'object') {211 try {212 date.getTime();213 } catch (e) {214 $.error('Received date is in a non supported format!');215 return false;216 }217 }218 return date;219 }220 221 if(typeof date == 'undefined') date = new Date(0);222 223 if(desired_format != 'string' && desired_format != 'object' && desired_format != 'number')224 $.error('Date format "'+ desired_format +'" not supported!');225 226 if(!dateFormat) {227 // thanks to bibendus83 -> http://sourceforge.net/tracker/index.php?func=detail&aid=3213174&group_id=358205&atid=1495382228 var dp_dateFormat = $this.datepicker('option', 'dateFormat');229 if (dp_dateFormat) {230 dateFormat = dp_dateFormat;231 } else {232 dateFormat = $.datepicker._defaults.dateFormat;233 }234 }235 236 // converts to object as a neutral format237 switch(from_format) {238 case 'object': break;239 case 'string': date = $.datepicker.parseDate(dateFormat, date); break;240 case 'number': date = new Date(date); break;241 default: $.error('Conversion from "'+ desired_format +'" format not allowed on jQuery.multiDatesPicker');242 }243 // then converts to the desired format244 switch(desired_format) {245 case 'object': return date;246 case 'string': return $.datepicker.formatDate(dateFormat, date);247 case 'number': return date.getTime();248 default: $.error('Conversion to "'+ desired_format +'" format not allowed on jQuery.multiDatesPicker');249 }250 return false;251 },252 gotDate : function( date, type ) {253 if(!type) type = 'picked';254 for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++) {255 if(methods.compareDates.call(this, this.multiDatesPicker.dates[type][i], date) === 0) {256 return i;257 }258 }259 return false;260 },261 value : function( value ) {262 if(value && typeof value == 'string') {263 methods.addDates.call(this, value.split(this.multiDatesPicker.separator));264 } else {265 var dates = methods.getDates.call(this, 'string');266 return dates.length267 ? dates.join(this.multiDatesPicker.separator)268 : "";269 }270 },271 getDates : function( format, type ) {272 if(!format) format = 'string';273 if(!type) type = 'picked';274 switch (format) {275 case 'object':276 return this.multiDatesPicker.dates[type];277 case 'string':278 case 'number':279 var o_dates = new Array();280 for(var i in this.multiDatesPicker.dates[type])281 o_dates.push(282 dateConvert.call(283 this, 284 this.multiDatesPicker.dates[type][i], 285 format286 )287 );288 return o_dates;289 290 default: $.error('Format "'+format+'" not supported!');291 }292 },293 addDates : function( dates, type ) {294 if(dates.length > 0) {295 if(!type) type = 'picked';296 switch(typeof dates) {297 case 'object':298 case 'array':299 if(dates.length) {300 for(var i = 0; i < dates.length; i++)301 addDate.call(this, dates[i], type, true);302 sortDates.call(this, type);303 break;304 } // else does the same as 'string'305 case 'string':306 case 'number':307 addDate.call(this, dates, type);308 break;309 default: 310 $.error('Date format "'+ typeof dates +'" not allowed on jQuery.multiDatesPicker');311 }312 //$(this).datepicker('refresh');313 } else {314 $.error('Empty array of dates received.');315 }316 },317 removeDates : function( dates, type ) {318 if(!type) type = 'picked';319 var removed = [];320 if (Object.prototype.toString.call(dates) === '[object Array]') {321 for(var i in dates.sort(function(a,b){return b-a})) {322 removed.push(removeDate.call(this, dates[i], type));323 }324 } else {325 removed.push(removeDate.call(this, dates, type));326 }327 return removed;328 },329 removeIndexes : function( indexes, type ) {330 if(!type) type = 'picked';331 var removed = [];332 if (Object.prototype.toString.call(indexes) === '[object Array]') {333 for(var i in indexes.sort(function(a,b){return b-a})) {334 removed.push(removeIndex.call(this, indexes[i], type));335 }336 } else {337 removed.push(removeIndex.call(this, indexes, type));338 }339 return removed;340 },341 resetDates : function ( type ) {342 if(!type) type = 'picked';343 this.multiDatesPicker.dates[type] = [];344 },345 toggleDate : function( date, type ) {346 if(!type) type = 'picked';347 348 switch(this.multiDatesPicker.mode) {349 case 'daysRange':350 this.multiDatesPicker.dates[type] = []; // deletes all picked/disabled dates351 var end = this.multiDatesPicker.autoselectRange[1];352 var begin = this.multiDatesPicker.autoselectRange[0];353 if(end < begin) { // switch354 end = this.multiDatesPicker.autoselectRange[0];355 begin = this.multiDatesPicker.autoselectRange[1];356 }357 for(var i = begin; i < end; i++) 358 methods.addDates.call(this, methods.sumDays.call(this,date, i), type);359 break;360 default:361 if(methods.gotDate.call(this, date) === false) // adds dates362 methods.addDates.call(this, date, type);363 else // removes dates364 methods.removeDates.call(this, date, type);365 break;366 }367 }, 368 setMode : function( options ) {369 var $this = $(this);370 if(options.mode) this.multiDatesPicker.mode = options.mode;371 372 switch(this.multiDatesPicker.mode) {373 case 'normal':374 for(option in options)375 switch(option) {376 case 'maxPicks':377 case 'minPicks':378 case 'pickableRange':379 case 'adjustRangeToDisabled':380 this.multiDatesPicker[option] = options[option];381 break;382 //default: $.error('Option ' + option + ' ignored for mode "'.options.mode.'".');383 }384 break;385 case 'daysRange':386 case 'weeksRange':387 var mandatory = 1;388 for(option in options)389 switch(option) {390 case 'autoselectRange':391 mandatory--;392 case 'pickableRange':393 case 'adjustRangeToDisabled':394 this.multiDatesPicker[option] = options[option];395 break;396 //default: $.error('Option ' + option + ' does not exist for setMode on jQuery.multiDatesPicker');397 }398 if(mandatory > 0) $.error('Some mandatory options not specified!');399 break;400 }401 402 /*403 if(options.pickableRange) {404 $this.datepicker("option", "maxDate", options.pickableRange);405 $this.datepicker("option", "minDate", this.multiDatesPicker.minDate);406 }407 */408 409 if(mdp_events.onSelect)410 mdp_events.onSelect();411 },412 destroy: function(){413 this.multiDatesPicker = null;414 $(this).datepicker('destroy');415 }416 };417 418 this.each(function() {419 var $this = $(this);420 if (!this.multiDatesPicker) {421 this.multiDatesPicker = {422 dates: {423 picked: [],424 disabled: []425 },426 mode: 'normal',427 adjustRangeToDisabled: true428 };429 }430 431 if(methods[method]) {432 var exec_result = methods[method].apply(this, Array.prototype.slice.call(mdp_arguments, 1));433 switch(method) {434 case 'removeDates':435 case 'removeIndexes':436 case 'resetDates':437 case 'toggleDate':438 case 'addDates':439 var altField = $this.datepicker('option', 'altField');440 // @todo: should use altFormat for altField441 var dates_string = methods.value.call(this);442 if (altField !== undefined && altField != "") {443 $(altField).val(dates_string);444 }445 $this.val(dates_string);446 447 $.datepicker._refreshDatepicker(this);448 }449 switch(method) {450 case 'removeDates':451 case 'getDates':452 case 'gotDate':453 case 'sumDays':454 case 'compareDates':455 case 'dateConvert':456 case 'value':457 ret = exec_result;458 }459 return exec_result;460 } else if( typeof method === 'object' || ! method ) {461 return methods.init.apply(this, mdp_arguments);462 } else {463 $.error('Method ' + method + ' does not exist on jQuery.multiDatesPicker');464 }465 return false;466 }); 467 468 return ret;469 };470 var PROP_NAME = 'multiDatesPicker';471 var dpuuid = new Date().getTime();472 var instActive;473 $.multiDatesPicker = {version: false};474 //$.multiDatesPicker = new MultiDatesPicker(); // singleton instance475 $.multiDatesPicker.initialized = false;476 $.multiDatesPicker.uuid = new Date().getTime();477 $.multiDatesPicker.version = $.ui.multiDatesPicker.version;478 479 // allows MDP not to hide everytime a date is picked480 $.multiDatesPicker._hideDatepicker = $.datepicker._hideDatepicker;481 $.datepicker._hideDatepicker = function(){482 var target = this._curInst.input[0];483 var mdp = target.multiDatesPicker;484 if(!mdp || (this._curInst.inline === false && !mdp.changed)) {485 return $.multiDatesPicker._hideDatepicker.apply(this, arguments);486 } else {487 mdp.changed = false;488 $.datepicker._refreshDatepicker(target);489 return;490 }491 };492 // Workaround for #4055493 // Add another global to avoid noConflict issues with inline event handlers494 window['DP_jQuery_' + dpuuid] = $;...
jquery-ui.multidatespicker.js
Source:jquery-ui.multidatespicker.js
1/*2 * MultiDatesPicker v1.6.63 * https://dubrox.github.io/Multiple-Dates-Picker-for-jQuery-UI4 * 5 * Copyright 2017, Luca Lauretta6 * Dual licensed under the MIT or GPL version 2 licenses.7 */8(function(factory) {9 if (typeof define === "function" && define.amd) {10 define(["jquery", "jquery-ui-dist"], factory);11 } else {12 factory(jQuery);13 }14}(function( $ ){15 $.extend($.ui, { multiDatesPicker: { version: "1.6.6" } });16 17 $.fn.multiDatesPicker = function(method) {18 var mdp_arguments = arguments;19 var ret = this;20 var today_date = new Date();21 var day_zero = new Date(0);22 var mdp_events = {};23 24 function removeDate(date, type) {25 if(!type) type = 'picked';26 date = dateConvert.call(this, date);27 for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++)28 if(!methods.compareDates(this.multiDatesPicker.dates[type][i], date))29 return this.multiDatesPicker.dates[type].splice(i, 1).pop();30 }31 function removeIndex(index, type) {32 if(!type) type = 'picked';33 return this.multiDatesPicker.dates[type].splice(index, 1).pop();34 }35 function addDate(date, type, no_sort) {36 if(!type) type = 'picked';37 date = dateConvert.call(this, date);38 39 // @todo: use jQuery UI datepicker method instead40 date.setHours(0);41 date.setMinutes(0);42 date.setSeconds(0);43 date.setMilliseconds(0);44 45 if (methods.gotDate.call(this, date, type) === false) {46 this.multiDatesPicker.dates[type].push(date);47 if(!no_sort) this.multiDatesPicker.dates[type].sort(methods.compareDates);48 } 49 }50 function sortDates(type) {51 if(!type) type = 'picked';52 this.multiDatesPicker.dates[type].sort(methods.compareDates);53 }54 function dateConvert(date, desired_type, date_format) {55 if(!desired_type) desired_type = 'object';/*56 if(!date_format && (typeof date == 'string')) {57 date_format = $(this).datepicker('option', 'dateFormat');58 if(!date_format) date_format = $.datepicker._defaults.dateFormat;59 }60 */61 return methods.dateConvert.call(this, date, desired_type, date_format);62 }63 64 var methods = {65 init : function( options ) {66 var $this = $(this);67 this.multiDatesPicker.changed = false;68 69 var mdp_events = {70 beforeShow: function(input, inst) {71 this.multiDatesPicker.changed = false;72 if(this.multiDatesPicker.originalBeforeShow) 73 this.multiDatesPicker.originalBeforeShow.call(this, input, inst);74 },75 onSelect : function(dateText, inst) {76 var $this = $(this);77 this.multiDatesPicker.changed = true;78 79 if (dateText) {80 $this.multiDatesPicker('toggleDate', dateText);81 this.multiDatesPicker.changed = true;82 // @todo: this will be optimized when I'll move methods to the singleton.83 }84 85 if (this.multiDatesPicker.mode == 'normal' && this.multiDatesPicker.pickableRange) {86 if(this.multiDatesPicker.dates.picked.length > 0) {87 var min_date = this.multiDatesPicker.dates.picked[0],88 max_date = new Date(min_date.getTime());89 90 methods.sumDays(max_date, this.multiDatesPicker.pickableRange-1);91 92 // counts the number of disabled dates in the range93 if(this.multiDatesPicker.adjustRangeToDisabled) {94 var c_disabled, 95 disabled = this.multiDatesPicker.dates.disabled.slice(0);96 do {97 c_disabled = 0;98 for(var i = 0; i < disabled.length; i++) {99 if(disabled[i].getTime() <= max_date.getTime()) {100 if((min_date.getTime() <= disabled[i].getTime()) && (disabled[i].getTime() <= max_date.getTime()) ) {101 c_disabled++;102 }103 disabled.splice(i, 1);104 i--;105 }106 }107 max_date.setDate(max_date.getDate() + c_disabled);108 } while(c_disabled != 0);109 }110 111 if(this.multiDatesPicker.maxDate && (max_date > this.multiDatesPicker.maxDate))112 max_date = this.multiDatesPicker.maxDate;113 114 $this115 .datepicker("option", "minDate", min_date)116 .datepicker("option", "maxDate", max_date);117 } else {118 $this119 .datepicker("option", "minDate", this.multiDatesPicker.minDate)120 .datepicker("option", "maxDate", this.multiDatesPicker.maxDate);121 }122 }123 124 if(this.multiDatesPicker.originalOnSelect && dateText)125 this.multiDatesPicker.originalOnSelect.call(this, dateText, inst);126 127 },128 beforeShowDay : function(date) {129 var $this = $(this),130 gotThisDate = $this.multiDatesPicker('gotDate', date) !== false,131 isDisabledCalendar = $this.datepicker('option', 'disabled'),132 isDisabledDate = $this.multiDatesPicker('gotDate', date, 'disabled') !== false,133 areAllSelected = this.multiDatesPicker.maxPicks <= this.multiDatesPicker.dates.picked.length;134 135 var bsdReturn = [true, '', null];136 if(this.multiDatesPicker.originalBeforeShowDay)137 bsdReturn = this.multiDatesPicker.originalBeforeShowDay.call(this, date);138 139 bsdReturn[1] = gotThisDate ? 'ui-state-highlight '+bsdReturn[1] : bsdReturn[1];140 bsdReturn[0] = bsdReturn[0] && !(isDisabledCalendar || isDisabledDate || (areAllSelected && !bsdReturn[1]));141 return bsdReturn;142 }143 };144 145 // value have to be extracted before datepicker is initiated146 if($this.val()) var inputDates = $this.val()147 148 if(options) {149 // value have to be extracted before datepicker is initiated150 //if(options.altField) var inputDates = $(options.altField).val();151 if(options.separator) this.multiDatesPicker.separator = options.separator;152 if(!this.multiDatesPicker.separator) this.multiDatesPicker.separator = ', ';153 154 this.multiDatesPicker.originalBeforeShow = options.beforeShow;155 this.multiDatesPicker.originalOnSelect = options.onSelect;156 this.multiDatesPicker.originalBeforeShowDay = options.beforeShowDay;157 this.multiDatesPicker.originalOnClose = options.onClose;158 159 // datepicker init160 $this.datepicker(options);161 162 this.multiDatesPicker.minDate = $.datepicker._determineDate(this, options.minDate, null);163 this.multiDatesPicker.maxDate = $.datepicker._determineDate(this, options.maxDate, null);164 if(options.addDates) methods.addDates.call(this, options.addDates);165 166 if(options.addDisabledDates)167 methods.addDates.call(this, options.addDisabledDates, 'disabled');168 169 methods.setMode.call(this, options);170 } else {171 $this.datepicker();172 }173 $this.datepicker('option', mdp_events);174 175 // adds any dates found in the input or alt field176 if(inputDates) $this.multiDatesPicker('value', inputDates);177 178 // generates the new string of added dates179 var inputs_values = $this.multiDatesPicker('value');180 181 // fills the input field back with all the dates in the calendar182 $this.val(inputs_values);183 184 // Fixes the altField filled with defaultDate by default185 var altFieldOption = $this.datepicker('option', 'altField');186 if (altFieldOption) $(altFieldOption).val(inputs_values);187 188 // Updates the calendar view189 $this.datepicker('refresh');190 },191 compareDates : function(date1, date2) {192 date1 = dateConvert.call(this, date1);193 date2 = dateConvert.call(this, date2);194 // return > 0 means date1 is later than date2 195 // return == 0 means date1 is the same day as date2 196 // return < 0 means date1 is earlier than date2 197 var diff = date1.getFullYear() - date2.getFullYear();198 if(!diff) {199 diff = date1.getMonth() - date2.getMonth();200 if(!diff) 201 diff = date1.getDate() - date2.getDate();202 }203 return diff;204 },205 sumDays : function( date, n_days ) {206 var origDateType = typeof date;207 obj_date = dateConvert.call(this, date);208 obj_date.setDate(obj_date.getDate() + n_days);209 return dateConvert.call(this, obj_date, origDateType);210 },211 dateConvert : function( date, desired_format, dateFormat ) {212 var from_format = typeof date;213 var $this = $(this);214 215 if(from_format == desired_format) {216 if(from_format == 'object') {217 try {218 date.getTime();219 } catch (e) {220 $.error('Received date is in a non supported format!');221 return false;222 }223 }224 return date;225 }226 227 if(typeof date == 'undefined') date = new Date(0);228 229 if(desired_format != 'string' && desired_format != 'object' && desired_format != 'number')230 $.error('Date format "'+ desired_format +'" not supported!');231 232 if(!dateFormat) {233 // thanks to bibendus83 -> http://sourceforge.net/tracker/index.php?func=detail&aid=3213174&group_id=358205&atid=1495382234 var dp_dateFormat = $this.datepicker('option', 'dateFormat');235 if (dp_dateFormat) {236 dateFormat = dp_dateFormat;237 } else {238 dateFormat = $.datepicker._defaults.dateFormat;239 }240 }241 242 // converts to object as a neutral format243 switch(from_format) {244 case 'object': break;245 case 'string': date = $.datepicker.parseDate(dateFormat, date); break;246 case 'number': date = new Date(date); break;247 default: $.error('Conversion from "'+ from_format +'" format not allowed on jQuery.multiDatesPicker');248 }249 // then converts to the desired format250 switch(desired_format) {251 case 'object': return date;252 case 'string': return $.datepicker.formatDate(dateFormat, date);253 case 'number': return date.getTime();254 default: $.error('Conversion to "'+ desired_format +'" format not allowed on jQuery.multiDatesPicker');255 }256 return false;257 },258 gotDate : function( date, type ) {259 if(!type) type = 'picked';260 for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++) {261 if(methods.compareDates.call(this, this.multiDatesPicker.dates[type][i], date) === 0) {262 return i;263 }264 }265 return false;266 },267 value : function( value ) {268 if(value && typeof value == 'string') {269 methods.addDates.call(this, value.split(this.multiDatesPicker.separator));270 } else {271 var dates = methods.getDates.call(this, 'string');272 return dates.length273 ? dates.join(this.multiDatesPicker.separator)274 : "";275 }276 },277 getDates : function( format, type ) {278 if(!format) format = 'string';279 if(!type) type = 'picked';280 switch (format) {281 case 'object':282 return this.multiDatesPicker.dates[type];283 case 'string':284 case 'number':285 var o_dates = [];286 for(var i = 0; i < this.multiDatesPicker.dates[type].length; i++)287 o_dates.push(288 dateConvert.call(289 this, 290 this.multiDatesPicker.dates[type][i], 291 format292 )293 );294 return o_dates;295 296 default: $.error('Format "'+format+'" not supported!');297 }298 },299 addDates : function( dates, type ) {300 if(dates.length > 0) {301 if(!type) type = 'picked';302 switch(typeof dates) {303 case 'object':304 case 'array':305 if(dates.length) {306 for(var i = 0; i < dates.length; i++)307 addDate.call(this, dates[i], type, true);308 sortDates.call(this, type);309 break;310 } // else does the same as 'string'311 case 'string':312 case 'number':313 addDate.call(this, dates, type);314 break;315 default: 316 $.error('Date format "'+ typeof dates +'" not allowed on jQuery.multiDatesPicker');317 }318 //$(this).datepicker('refresh');319 } else {320 $.error('Empty array of dates received.');321 }322 },323 removeDates : function( dates, type ) {324 if(!type) type = 'picked';325 var removed = [];326 if (Object.prototype.toString.call(dates) === '[object Array]') {327 dates.sort(function(a,b){return b-a});328 for(var i = 0; i < dates.length; i++) {329 removed.push(removeDate.call(this, dates[i], type));330 }331 } else {332 removed.push(removeDate.call(this, dates, type));333 }334 return removed;335 },336 removeIndexes : function( indexes, type ) {337 if(!type) type = 'picked';338 var removed = [];339 if (Object.prototype.toString.call(indexes) === '[object Array]') {340 indexes.sort(function(a,b){return b-a});341 for(var i = 0; i < indexes.length; i++) {342 removed.push(removeIndex.call(this, indexes[i], type));343 }344 } else {345 removed.push(removeIndex.call(this, indexes, type));346 }347 return removed;348 },349 resetDates : function ( type ) {350 if(!type) type = 'picked';351 this.multiDatesPicker.dates[type] = [];352 },353 toggleDate : function( date, type ) {354 if(!type) type = 'picked';355 356 switch(this.multiDatesPicker.mode) {357 case 'daysRange':358 this.multiDatesPicker.dates[type] = []; // deletes all picked/disabled dates359 var end = this.multiDatesPicker.autoselectRange[1];360 var begin = this.multiDatesPicker.autoselectRange[0];361 if(end < begin) { // switch362 end = this.multiDatesPicker.autoselectRange[0];363 begin = this.multiDatesPicker.autoselectRange[1];364 }365 for(var i = begin; i < end; i++) 366 methods.addDates.call(this, methods.sumDays.call(this,date, i), type);367 break;368 default:369 if(methods.gotDate.call(this, date) === false) // adds dates370 methods.addDates.call(this, date, type);371 else // removes dates372 methods.removeDates.call(this, date, type);373 break;374 }375 }, 376 setMode : function( options ) {377 var $this = $(this);378 if(options.mode) this.multiDatesPicker.mode = options.mode;379 380 switch(this.multiDatesPicker.mode) {381 case 'normal':382 for(var option in options)383 switch(option) {384 case 'maxPicks':385 case 'minPicks':386 case 'pickableRange':387 case 'adjustRangeToDisabled':388 this.multiDatesPicker[option] = options[option];389 break;390 //default: $.error('Option ' + option + ' ignored for mode "'.options.mode.'".');391 }392 break;393 case 'daysRange':394 case 'weeksRange':395 var mandatory = 1;396 for(option in options)397 switch(option) {398 case 'autoselectRange':399 mandatory--;400 case 'pickableRange':401 case 'adjustRangeToDisabled':402 this.multiDatesPicker[option] = options[option];403 break;404 //default: $.error('Option ' + option + ' does not exist for setMode on jQuery.multiDatesPicker');405 }406 if(mandatory > 0) $.error('Some mandatory options not specified!');407 break;408 }409 410 /*411 if(options.pickableRange) {412 $this.datepicker("option", "maxDate", options.pickableRange);413 $this.datepicker("option", "minDate", this.multiDatesPicker.minDate);414 }415 */416 417 if(mdp_events.onSelect)418 mdp_events.onSelect();419 },420 destroy: function(){421 this.multiDatesPicker = null;422 $(this).datepicker('destroy');423 }424 };425 426 this.each(function() {427 var $this = $(this);428 if (!this.multiDatesPicker) {429 this.multiDatesPicker = {430 dates: {431 picked: [],432 disabled: []433 },434 mode: 'normal',435 adjustRangeToDisabled: true436 };437 }438 439 if(methods[method]) {440 var exec_result = methods[method].apply(this, Array.prototype.slice.call(mdp_arguments, 1));441 switch(method) {442 case 'removeDates':443 case 'removeIndexes':444 case 'resetDates':445 case 'toggleDate':446 case 'addDates':447 var altField = $this.datepicker('option', 'altField');448 // @todo: should use altFormat for altField449 var dates_string = methods.value.call(this);450 if (altField !== undefined && altField != "") {451 $(altField).val(dates_string);452 }453 $this.val(dates_string);454 455 $.datepicker._refreshDatepicker(this);456 }457 switch(method) {458 case 'removeDates':459 case 'getDates':460 case 'gotDate':461 case 'sumDays':462 case 'compareDates':463 case 'dateConvert':464 case 'value':465 ret = exec_result;466 }467 return exec_result;468 } else if( typeof method === 'object' || ! method ) {469 return methods.init.apply(this, mdp_arguments);470 } else {471 $.error('Method ' + method + ' does not exist on jQuery.multiDatesPicker');472 }473 return false;474 }); 475 476 return ret;477 };478 var PROP_NAME = 'multiDatesPicker';479 var dpuuid = new Date().getTime();480 var instActive;481 $.multiDatesPicker = {version: false};482 //$.multiDatesPicker = new MultiDatesPicker(); // singleton instance483 $.multiDatesPicker.initialized = false;484 $.multiDatesPicker.uuid = new Date().getTime();485 $.multiDatesPicker.version = $.ui.multiDatesPicker.version;486 487 // allows MDP not to hide everytime a date is picked488 $.multiDatesPicker._hideDatepicker = $.datepicker._hideDatepicker;489 $.datepicker._hideDatepicker = function(){490 var target = this._curInst.input[0];491 var mdp = target.multiDatesPicker;492 if(!mdp || (this._curInst.inline === false && !mdp.changed)) {493 return $.multiDatesPicker._hideDatepicker.apply(this, arguments);494 } else {495 mdp.changed = false;496 $.datepicker._refreshDatepicker(target);497 return;498 }499 };500 // Workaround for #4055501 // Add another global to avoid noConflict issues with inline event handlers502 window['DP_jQuery_' + dpuuid] = $;...
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2var dates = devicefarmer.dates;3var date = new Date();4var date1 = new Date();5date1.setHours(date.getHours() + 1);6var date2 = new Date();7date2.setHours(date.getHours() + 2);8var date3 = new Date();9date3.setHours(date.getHours() + 3);10var date4 = new Date();11date4.setHours(date.getHours() + 4);12var date5 = new Date();13date5.setHours(date.getHours() + 5);14var date6 = new Date();15date6.setHours(date.getHours() + 6);16var date7 = new Date();17date7.setHours(date.getHours() + 7);18var date8 = new Date();19date8.setHours(date.getHours() + 8);20var date9 = new Date();21date9.setHours(date.getHours() + 9);22var date10 = new Date();23date10.setHours(date.getHours() + 10);24var date11 = new Date();25date11.setHours(date.getHours() + 11);26var date12 = new Date();27date12.setHours(date.getHours() + 12);28var date13 = new Date();29date13.setHours(date.getHours() + 13);30var date14 = new Date();31date14.setHours(date.getHours() + 14);32var date15 = new Date();33date15.setHours(date.getHours() + 15);34var date16 = new Date();35date16.setHours(date.getHours() + 16);36var date17 = new Date();37date17.setHours(date.getHours() + 17);38var date18 = new Date();39date18.setHours(date.getHours() + 18);40var date19 = new Date();41date19.setHours(date.getHours() + 19);42var date20 = new Date();43date20.setHours(date.getHours() + 20);44var date21 = new Date();45date21.setHours(date.getHours() + 21);46var date22 = new Date();47date22.setHours(date.getHours() + 22);48var date23 = new Date();49date23.setHours(date.getHours() + 23);50var date24 = new Date();51date24.setHours(date.getHours() + 24);52var date25 = new Date();53date25.setHours(date.getHours() + 25);54var date26 = new Date();55date26.setHours(date.getHours() + 26);
Using AI Code Generation
1var stf = require('devicefarmer-stf-client');2var dates = stf.dates;3var date = new Date();4console.log(dates.format(date));5var stf = require('devicefarmer-stf-client');6var devices = stf.devices;7var device = new devices.Device();8console.log(device);9var stf = require('devicefarmer-stf-client');10var groups = stf.groups;11var group = new groups.Group();12console.log(group);13var stf = require('devicefarmer-stf-client');14var keys = stf.keys;15var key = new keys.Key();16console.log(key);17var stf = require('devicefarmer-stf-client');18var logcat = stf.logcat;19var log = new logcat.Logcat();20console.log(log);21var stf = require('devicefarmer-stf-client');22var minicap = stf.minicap;23var minicap = new minicap.Minicap();24console.log(minicap);25var stf = require('devicefarmer-stf-client');26var minitouch = stf.minitouch;27var minitouch = new minitouch.Minitouch();28console.log(minitouch);29var stf = require('devicefarmer-stf-client');30var providers = stf.providers;31var provider = new providers.Provider();32console.log(provider);33var stf = require('devicefarmer-stf-client');34var remoteapps = stf.remoteapps;35var remoteapp = new remoteapps.Remoteapp();36console.log(remoteapp);
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2var dates = devicefarmer.dates;3var date = new dates();4date.getDates(function(err, data) {5 if (err) {6 console.log('error occured while getting dates');7 } else {8 console.log(data);9 }10});11var devicefarmer = require('devicefarmer-stf');12var devices = devicefarmer.devices;13var device = new devices();14device.getDevices(function(err, data) {15 if (err) {16 console.log('error occured while getting devices');17 } else {18 console.log(data);19 }20});21var devicefarmer = require('devicefarmer-stf');22var device = devicefarmer.device;23var deviceId = 'device id';24device.getDevice(deviceId, function(err, data) {25 if (err) {26 console.log('error occured while getting device');27 } else {28 console.log(data);29 }30});31var devicefarmer = require('devicefarmer-stf');32var device = devicefarmer.device;33var deviceId = 'device id';34device.getDevice(deviceId, function(err, data) {35 if (err) {36 console.log('error occured while getting device');37 } else {38 console.log(data);39 }40});41var devicefarmer = require('devicefarmer-stf');42var device = devicefarmer.device;43var deviceId = 'device id';44device.getDevice(deviceId, function(err, data) {45 if (err) {46 console.log('error occured while getting device');47 } else {48 console.log(data);49 }50});51var devicefarmer = require('devicefarmer-stf');52var device = devicefarmer.device;53var deviceId = 'device id';54device.getDevice(deviceId, function(err, data) {55 if (err) {56 console.log('error occured while getting device');57 } else {58 console.log(data
Using AI Code Generation
1var dates = require('devicefarmer-stf').dates;2console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS'));3console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss'));4console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD'));5console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'HH:mm:ss.SSS'));6console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'HH:mm:ss'));7console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS Z'));8console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS ZZ'));9console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS ZZZ'));10console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS [Z]'));11console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS [ZZ]'));12console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS [ZZZ]'));13console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS [Z]'));14console.log(dates.format(new Date(2016, 11, 25, 10, 30, 15), 'YYYY-MM-DD HH:mm:ss.SSS [ZZ]'));15console.log(dates.format(new Date(2016,
Using AI Code Generation
1var deviceFarmer = require('devicefarmer-stf');2var dates = deviceFarmer.dates;3var date = new dates();4var date1 = date.getFutureDate(3);5console.log(date1);6var date2 = date.getFutureDate(3, 'YYYY-MM-DD');7console.log(date2);8var date3 = date.getFutureDate(3, 'YYYY-MM-DD HH:mm:ss');9console.log(date3);10var date4 = date.getFutureDate(3, 'YYYY-MM-DD HH:mm:ss:SSS');11console.log(date4);12var deviceFarmer = require('devicefarmer-stf');13var device = deviceFarmer.device;14var device1 = new device();15device1.getDevice().then(function (device) {16 console.log(device);17});18var deviceFarmer = require('devicefarmer-stf');19var devicefarm = deviceFarmer.devicefarm;20var devicefarm1 = new devicefarm();21devicefarm1.getDeviceFarm().then(function (devicefarm) {22 console.log(devicefarm);23});24var deviceFarmer = require('devicefarmer-stf');25var devicefarm = deviceFarmer.devicefarm;26var devicefarm1 = new devicefarm();27devicefarm1.getDeviceFarm().then(function (devicefarm) {28 console.log(devicefarm);29});30var deviceFarmer = require('devicefarmer-stf');31var devicefarm = deviceFarmer.devicefarm;32var devicefarm1 = new devicefarm();33devicefarm1.getDeviceFarm().then(function (devicefarm) {34 console.log(devicefarm);35});36var deviceFarmer = require('devicefarmer-stf');37var devicefarm = deviceFarmer.devicefarm;38var devicefarm1 = new devicefarm();39devicefarm1.getDeviceFarm().then(function (devicefarm) {40 console.log(devicefarm);41});42var deviceFarmer = require('devicefarmer-stf');43var devicefarm = deviceFarmer.devicefarm;44var devicefarm1 = new devicefarm();45devicefarm1.getDeviceFarm().then(function (
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2var date = devicefarmer.dates.getDate();3var devicefarmer = require('devicefarmer-stf');4var device = devicefarmer.device.getDevice();5var devicefarmer = require('devicefarmer-stf');6var device = devicefarmer.device.getDevice();7var devicefarmer = require('devicefarmer-stf');8var device = devicefarmer.device.getDevice();9var devicefarmer = require('devicefarmer-stf');10var device = devicefarmer.device.getDevice();11var devicefarmer = require('devicefarmer-stf');12var device = devicefarmer.device.getDevice();13var devicefarmer = require('devicefarmer-stf');14var device = devicefarmer.device.getDevice();15var devicefarmer = require('devicefarmer-stf');16var device = devicefarmer.device.getDevice();17var devicefarmer = require('devicefarmer-stf');18var device = devicefarmer.device.getDevice();19var devicefarmer = require('devicefarmer-stf');20var device = devicefarmer.device.getDevice();21var devicefarmer = require('devicefarmer-stf');22var device = devicefarmer.device.getDevice();23var devicefarmer = require('devicefarmer-stf');24var device = devicefarmer.device.getDevice();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!