Calendar.js 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  1. o2.widget = o2.widget || {};
  2. o2.widget.Calendar = o2.Calendar = new Class({
  3. Implements: [Options, Events],
  4. Extends: o2.widget.Common,
  5. options: {
  6. "style": "default",
  7. "path": o2.session.path+"/widget/$Calendar/" ,
  8. "defaultView": "day", //day, month, year
  9. "baseDate": new Date(),
  10. "secondEnable" : false,
  11. "timeSelectType" : "slider",
  12. "isTime": false,
  13. "isMulti": false,
  14. "before": null,
  15. "after": null,
  16. "timeOnly": false,
  17. "yearOnly" : false,
  18. "monthOnly": false,
  19. "defaultDate": null,
  20. "beforeCurrent": true,
  21. "clearEnable": true,
  22. "weekBegin": 0, //0表示周日,1表示周一
  23. "range": false,
  24. "rangeNodes": [],
  25. "rangeRule": "asc", //asc + , des -
  26. "target": null,
  27. // "enableDate": function (date) { //一个function,参数为日期,return true不可选
  28. // return date > new Date();
  29. // },
  30. // "enableHours": function (date) {
  31. // if( date > new Date() ){
  32. // return [];
  33. // }else{
  34. // return [[0, 8], [16, 24]];
  35. // }
  36. //}, //一个function, 参数为日期
  37. // "enableMinutes": null, //一个function, 参数为日期,hour
  38. // "enableSeconds": null, //一个function, 参数为日期,hour,minutes
  39. },
  40. initialize: function(node, options){
  41. Locale.use("zh-CHS");
  42. this.setOptions(options);
  43. this.options.defaultTime = ""+this.options.baseDate.getHours()+":"+this.options.baseDate.getMinutes()+":"+this.options.baseDate.getSeconds();
  44. this.path = o2.session.path+"/widget/$Calendar/";
  45. this.cssPath = o2.session.path+"/widget/$Calendar/"+this.options.style+"/css.wcss";
  46. this._loadCss();
  47. // this.options.containerPath = this.path+this.style+"/container.html";
  48. // this.options.dayPath = this.path+this.style+"/day.html";
  49. // this.options.monthPath = this.path+this.style+"/month.html";
  50. // this.options.yearPath = this.path+this.style+"/year.html";
  51. // this.options.timePath = this.path+this.style+"/time.html";
  52. if (!this.options.format){
  53. if (this.options.isTime){
  54. //this.options.format = Locale.get("Date").shortDate + " " + Locale.get("Date").shortTime;
  55. if(this.options.timeOnly){
  56. this.options.format="%H:%M";
  57. }
  58. else{
  59. this.options.format = Locale.get("Date").shortDate + " " + "%H:%M";
  60. }
  61. }else{
  62. this.options.format = Locale.get("Date").shortDate;
  63. }
  64. }
  65. this.options.containerPath = this.options.path+this.options.style+"/container.html";
  66. this.options.dayPath = this.options.path+this.options.style+"/day.html";
  67. this.options.monthPath = this.options.path+this.options.style+"/month.html";
  68. this.options.yearPath = this.options.path+this.options.style+"/year.html";
  69. this.options.timePath = this.options.path+this.options.style+"/time.html";
  70. this.today = new Date();
  71. this.currentView = this.options.defaultView;
  72. this.node = $(node);
  73. this.visible = false;
  74. this.setRange();
  75. this.container = this.createContainer();
  76. this.container.inject((this.options.target) || $(document.body));
  77. this.contentTable = this.createContentTable();
  78. this.contentTable.inject(this.contentDateNode);
  79. this.addEvents();
  80. this.container.set({
  81. styles: {
  82. "display": "none",
  83. "opacity": 1
  84. }
  85. });
  86. this.fireEvent("init");
  87. //this.move = true;
  88. //this.containerDrag = new Drag.Move(this.container);
  89. if( layout.mobile ){
  90. this.maskNode = new Element("div.maskNode",{
  91. styles : {
  92. "width": "100%",
  93. "height": "100%",
  94. "opacity": 0.6,
  95. "position": "absolute",
  96. "background-color": "#CCC",
  97. "top": "0px",
  98. "left": "0px",
  99. "z-index" : 150,
  100. "-webkit-user-select": "none",
  101. "-moz-user-select": "none",
  102. "user-select" : "none"
  103. },
  104. events : {
  105. "touchmove" : function(ev){
  106. ev.stopPropagation();
  107. ev.preventDefault();
  108. }
  109. }
  110. }).inject((this.options.target) || $(document.body));
  111. this.container.addEvents({
  112. "touchmove" : function(ev){
  113. ev.stopPropagation();
  114. ev.preventDefault();
  115. },
  116. "touchend" : function(ev){
  117. ev.stopPropagation();
  118. //ev.preventDefault();
  119. }
  120. })
  121. }
  122. },
  123. addEvents: function(){
  124. this.node.addEvent("focus", function(){
  125. this.show();
  126. }.bind(this));
  127. this.node.addEvent("click", function(){
  128. this.show();
  129. }.bind(this));
  130. this.prevNode.addEvent("click", function(){
  131. this.getPrev();
  132. }.bind(this));
  133. this.nextNode.addEvent("click", function(){
  134. this.getNext();
  135. }.bind(this));
  136. this.currentTextNode.addEvent("click", function(){
  137. this.changeView();
  138. }.bind(this));
  139. if( !layout.mobile ){
  140. this.titleNode.addEvent("mousedown", function(){
  141. this.move();
  142. }.bind(this));
  143. this.titleNode.addEvent("mouseup", function(){
  144. this.unmove();
  145. }.bind(this));
  146. }
  147. document.addEvent('mousedown', this.outsideClick.bind(this));
  148. },
  149. move: function(){
  150. this.containerDrag = new Drag.Move(this.container, {
  151. "onDrag": function(e){
  152. if (this.iframe){
  153. var p = this.container.getPosition();
  154. this.iframe.setStyles({
  155. "top": ""+p.y+"px",
  156. "left": ""+p.x+"px"
  157. });
  158. }
  159. }.bind(this)
  160. });
  161. },
  162. unmove: function(){
  163. this.container.removeEvents("mousedown");
  164. this.titleNode.addEvent("mousedown", function(){
  165. this.move();
  166. }.bind(this));
  167. },
  168. changeView: function(){
  169. var view = "day";
  170. switch (this.currentView) {
  171. case "day" :
  172. this.changeViewToMonth();
  173. break;
  174. case "month" :
  175. this.changeViewToYear();
  176. break;
  177. case "year" :
  178. if( this.options.yearOnly ){
  179. break;
  180. }else if( this.options.monthOnly ){
  181. this.changeViewToMonth();
  182. }else{
  183. this.changeViewToDay();
  184. }
  185. break;
  186. case "time" :
  187. this.changeViewToDay();
  188. //this.changeViewToDay();
  189. break;
  190. default :
  191. //nothing;
  192. }
  193. },
  194. changeViewToMonth: function(year){
  195. this.currentView = "month";
  196. if (!this.contentMonthTable){
  197. this.contentMonthTable = this.createContentTable();
  198. this.contentMonthTable.inject(this.contentDateNode);
  199. }
  200. if (this.contentTable) this.contentTable.setStyle("display", "none");
  201. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  202. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  203. // if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "block");
  204. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "table");
  205. var year = (year!=undefined) ? year : this.currentTextNode.retrieve("year");
  206. var month = this.currentTextNode.retrieve("month");
  207. this.showMonth(year, month);
  208. this.fireEvent("changeViewToMonth");
  209. },
  210. changeViewToYear: function(year){
  211. this.currentView = "year";
  212. if (!this.contentYearTable){
  213. this.contentYearTable = this.createContentTable();
  214. this.contentYearTable.inject(this.contentDateNode);
  215. }
  216. if (this.contentTable) this.contentTable.setStyle("display", "none");
  217. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  218. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  219. // if (this.contentYearTable) this.contentYearTable.setStyle("display", "block");
  220. if (this.contentYearTable) this.contentYearTable.setStyle("display", "table");
  221. this.showYear(year);
  222. this.fireEvent("changeViewToYear");
  223. },
  224. changeViewToDay: function(year, month){
  225. this.currentView = "day";
  226. if (!this.contentTable){
  227. this.contentTable = this.createContentTable();
  228. this.contentTable.inject(this.contentDateNode);
  229. }
  230. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  231. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  232. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "none");
  233. // if (this.contentTable) this.contentTable.setStyle("display", "block");
  234. if (this.contentTable) this.contentTable.setStyle("display", "table");
  235. this.showDay(year, month);
  236. this.showMonthYearButton();
  237. this.fireEvent("changeViewToDay");
  238. },
  239. hideMonthYearButton : function(){
  240. if(this.clearButton_month){
  241. this.clearButton_month.hide();
  242. }
  243. },
  244. showMonthYearButton : function(){
  245. if( this.options.clearEnable && this.buttonArea && !this.clearButton_month ){
  246. this.container.setStyle("height","auto");
  247. this.clearButton_month = new Element("div", {"text": o2.LP.widget.clear }).inject(this.buttonArea);
  248. this.clearButton_month.addEvent("click", function(){
  249. var t = this.node.get("value");
  250. this.node.set("value", "");
  251. if( t )this.fireEvent("change");
  252. this.fireEvent("clear");
  253. this.hide();
  254. }.bind(this));
  255. this.clearButton_month.setStyles(this.css.calendarMonthActionButton);
  256. }
  257. if(this.clearButton_month){
  258. this.clearButton_month.show();
  259. }
  260. },
  261. getNext: function(){
  262. switch (this.currentView) {
  263. case "time" :
  264. this.getNextDate();
  265. break;
  266. case "day" :
  267. this.getNextDay();
  268. break;
  269. case "month" :
  270. this.getNextMonth();
  271. break;
  272. case "year" :
  273. this.getNextYear();
  274. break;
  275. default :
  276. //nothing
  277. }
  278. },
  279. getPrev: function(){
  280. switch (this.currentView) {
  281. case "time" :
  282. this.getPrevDate();
  283. break;
  284. case "day" :
  285. this.getPrevDay();
  286. break;
  287. case "month" :
  288. this.getPrevMonth();
  289. break;
  290. case "year" :
  291. this.getPrevYear();
  292. break;
  293. default :
  294. //nothing
  295. }
  296. },
  297. getNextDate: function(){
  298. var date = this.currentTextNode.retrieve("date");
  299. if( this.currentView === "time" && this.options.enableDate ){
  300. var d = date.clone().increment("day", 1);
  301. if( !this.isEnableDate(d) )return;
  302. }
  303. // var year = this.currentTextNode.retrieve("year");
  304. // var month = this.currentTextNode.retrieve("month");
  305. // month--;
  306. // var day = this.currentTextNode.retrieve("day");
  307. // var date = new Date(year, month, day);
  308. date.increment("day", 1);
  309. this._setTimeTitle(null, date);
  310. if( this.options.enableHours || this.options.enableMinutes || this.options.enableSeconds ){
  311. this._resetTimeDate();
  312. }
  313. },
  314. getPrevDate: function(){
  315. var date = this.currentTextNode.retrieve("date");
  316. if( this.currentView === "time" && this.options.enableDate ){
  317. var d = date.clone().increment("day", -1);
  318. if( !this.isEnableDate(d) )return;
  319. }
  320. date.increment("day", -1);
  321. this._setTimeTitle(null, date);
  322. if( this.options.enableHours || this.options.enableMinutes || this.options.enableSeconds ){
  323. this._resetTimeDate();
  324. }
  325. },
  326. getNextDay: function(){
  327. var year = this.currentTextNode.retrieve("year");
  328. var month = this.currentTextNode.retrieve("month");
  329. month--;
  330. var date = new Date(year, month, 1);
  331. date.increment("month", 1);
  332. var thisYear = date.getFullYear();
  333. var thisMonth = date.getMonth();
  334. this._setDayTitle(null, thisYear, thisMonth);
  335. this._setDayDate(null,thisYear, thisMonth);
  336. this.fireEvent("changeViewToDay");
  337. },
  338. getPrevDay: function(){
  339. var year = this.currentTextNode.retrieve("year");
  340. var month = this.currentTextNode.retrieve("month");
  341. month--;
  342. var date = new Date(year, month, 1);
  343. date.increment("month", -1)
  344. var thisYear = date.getFullYear();
  345. var thisMonth = date.getMonth();
  346. this._setDayTitle(null, thisYear, thisMonth);
  347. this._setDayDate(null,thisYear, thisMonth);
  348. this.fireEvent("changeViewToDay");
  349. },
  350. getNextMonth: function(){
  351. var year = this.currentTextNode.retrieve("year");
  352. var date = new Date(year, 1, 1);
  353. date.increment("year", 1)
  354. var thisYear = date.getFullYear();
  355. this.showMonth(thisYear);
  356. this.fireEvent("changeViewToMonth");
  357. },
  358. getPrevMonth: function(){
  359. var year = this.currentTextNode.retrieve("year");
  360. var date = new Date(year, 1, 1);
  361. date.increment("year", -1)
  362. var thisYear = date.getFullYear();
  363. this.showMonth(thisYear);
  364. this.fireEvent("changeViewToMonth");
  365. },
  366. getNextYear: function(){
  367. var year = this.currentTextNode.retrieve("year");
  368. var date = new Date(year, 1, 1);
  369. date.increment("year", this.yearLength)
  370. var thisYear = date.getFullYear();
  371. this.showYear(thisYear);
  372. this.fireEvent("changeViewToYear");
  373. },
  374. getPrevYear: function(){
  375. var year = this.currentTextNode.retrieve("year");
  376. var date = new Date(year, 1, 1);
  377. date.increment("year", 0-this.yearLength)
  378. var thisYear = date.getFullYear();
  379. this.showYear(thisYear);
  380. this.fireEvent("changeViewToYear");
  381. },
  382. outsideClick: function(e) {
  383. if(this.visible) {
  384. var elementCoords, targetCoords, page;
  385. if ( !layout.inBrowser && layout.userLayout && layout.userLayout.scale && layout.userLayout.scale!==1){
  386. elementCoords = this.container.getCoordinates( this.options.target );
  387. targetCoords = this.node.getCoordinates( this.options.target );
  388. var containerSize = this.options.target.getPosition();
  389. page = e.page;
  390. // elementCoords.left = elementCoords.left * layout.userLayout.scale;
  391. // elementCoords.top = elementCoords.top * layout.userLayout.scale;
  392. //
  393. // targetCoords.left = targetCoords.left * layout.userLayout.scale;
  394. // targetCoords.top = targetCoords.top * layout.userLayout.scale;
  395. //
  396. // containerSize.x = containerSize.x * layout.userLayout.scale;
  397. // containerSize.y = containerSize.y * layout.userLayout.scale;
  398. page.x = page.x / layout.userLayout.scale - containerSize.x;
  399. page.y = page.y / layout.userLayout.scale - containerSize.y;
  400. }else{
  401. elementCoords = this.container.getCoordinates();
  402. targetCoords = this.node.getCoordinates();
  403. page = e.page;
  404. }
  405. if(((page.x < elementCoords.left || page.x > (elementCoords.left + elementCoords.width)) ||
  406. (page.y < elementCoords.top || page.y > (elementCoords.top + elementCoords.height))) &&
  407. ((page.x < targetCoords.left || page.x > (targetCoords.left + targetCoords.width)) ||
  408. (page.y < targetCoords.top || page.y > (targetCoords.top + targetCoords.height))) ) this.hide();
  409. }
  410. },
  411. hide: function(){
  412. if (this.visible){
  413. // if (!this.morph){
  414. // this.morph = new Fx.Morph(this.container, {"duration": 200});
  415. // }
  416. this.visible = false;
  417. // this.changeViewToDay();
  418. // this.morph.start({"opacity": 0}).chain(function(){
  419. this.container.setStyle("display", "none");
  420. if (this.iframe) this.iframe.destroy();
  421. if (layout.desktop.offices){
  422. Object.each(layout.desktop.offices, function(office){
  423. office.show();
  424. });
  425. }
  426. if( this.maskNode ){
  427. this.maskNode.hide();
  428. }
  429. // }.bind(this));
  430. this.fireEvent("hide");
  431. }
  432. },
  433. show: function(){
  434. if (!this.visible){
  435. var dStr = this.node.get("value");
  436. if (dStr && Date.isValid(dStr)){
  437. this.options.baseDate = Date.parse(dStr.substr(0,10));
  438. }
  439. if(this.options.timeOnly){
  440. this.currentView = "time";
  441. }
  442. else{
  443. this.currentView = this.options.defaultView;
  444. }
  445. switch (this.currentView) {
  446. case "day" :
  447. this.changeViewToDay();
  448. break;
  449. case "month" :
  450. //this.showMonth();
  451. this.changeViewToMonth();
  452. break;
  453. case "year" :
  454. if( this.options.yearOnly ){
  455. this.changeViewToYear()
  456. }else{
  457. this.showYear();
  458. }
  459. break;
  460. case "time" :
  461. //this.showTime(this.options.baseDate);
  462. this.changeViewToTime(this.options.defaultDate);
  463. //this.changeViewToTime(this.options.baseDate);
  464. break;
  465. default :
  466. this.showDay();
  467. }
  468. // if (!this.morph){
  469. // this.morph = new Fx.Morph(this.container, {"duration": 200});
  470. // }
  471. this.container.setStyle("display", "block");
  472. this.setPosition();
  473. // var p = this.container.getPosition();
  474. // var s = this.container.getSize();
  475. // var zidx = this.container.getStyle("z-index");
  476. // this.iframe = new Element("iframe", {"styles":{
  477. // "border": "0px",
  478. // "margin": "0px",
  479. // "padding": "0px",
  480. // "opacity": 0,
  481. // "z-index": (zidx) ? zidx-1 : 0,
  482. // "top": ""+p.y+"px",
  483. // "left": ""+p.x+"px",
  484. // "width": ""+s.x+"px",
  485. // "height": ""+s.y+"px",
  486. // "position": "absolute"
  487. // }}).inject(this.container, "before");
  488. if (layout.desktop.offices){
  489. Object.each(layout.desktop.offices, function(office){
  490. if (this.container.isOverlap(office.officeNode)){
  491. office.hide();
  492. }
  493. }.bind(this));
  494. }
  495. if( this.maskNode ){
  496. this.maskNode.show();
  497. }
  498. // this.morph.start({"opacity": 1}).chain(function(){
  499. this.visible = true;
  500. // }.bind(this));
  501. this.fireEvent("show");
  502. }
  503. },
  504. setPosition: function(){
  505. //if (this.container.position && (!layout || !layout.userLayout || !layout.userLayout.scale || layout.userLayout.scale===1) ){
  506. var postY = "bottom";
  507. var postX = "left";
  508. this.container.position({
  509. relativeTo: this.node,
  510. position: 'bottomLeft',
  511. edge: 'upperLeft'
  512. });
  513. // var offsetPNode = this.node.getOffsetParent();
  514. var cp = this.container.getPosition(this.options.target || null);
  515. var cSize = this.container.getSize();
  516. //var fp = (this.options.target) ? this.options.target.getPosition() : $(document.body).getPosition()
  517. var fsize = (this.options.target) ? this.options.target.getSize() : $(document.body).getSize();
  518. //if (cp.y+cSize.y>fsize.y+fp.y){
  519. if (cp.y+cSize.y>fsize.y){
  520. // this.container.position({
  521. // relativeTo: this.node,
  522. // position: 'upperLeft',
  523. // edge: 'bottomLeft'
  524. // });
  525. postY = "upper";
  526. }
  527. if( cp.x+cSize.x>fsize.x ){
  528. postX = "right";
  529. }
  530. if( postY === "upper" && postX === "left" ){
  531. this.container.position({
  532. relativeTo: this.node,
  533. position: 'upperLeft',
  534. edge: 'bottomLeft'
  535. });
  536. }else if( postX === "right" ){
  537. if( postY === "bottom" ){
  538. this.container.position({
  539. relativeTo: this.node,
  540. position: 'bottomRight',
  541. edge: 'upperRight'
  542. });
  543. }else{
  544. this.container.position({
  545. relativeTo: this.node,
  546. position: 'upperRight',
  547. edge: 'bottomRight'
  548. });
  549. }
  550. }
  551. this.postY = postY;
  552. this.postX = postX;
  553. // }else{
  554. // var p = this.node.getPosition(this.options.target || null);
  555. // var size = this.node.getSize();
  556. // var containerSize = this.container.getSize();
  557. // var bodySize = (this.options.target) ? this.options.target.getSize() : $(document.body).getSize(); //$(document.body).getSize();
  558. //
  559. // bodySize.x = bodySize.x * layout.userLayout.scale;
  560. // bodySize.y = bodySize.y * layout.userLayout.scale;
  561. //
  562. // var left = p.x;
  563. // left = left * layout.userLayout.scale;
  564. // if ((left + containerSize.x + 40) > bodySize.x){
  565. // left = bodySize.x - containerSize.x - 40;
  566. // }
  567. //
  568. // var top = p.y+size.y+2;
  569. // top = top * layout.userLayout.scale;
  570. // if( top + containerSize.y > bodySize.y ){
  571. // top = bodySize.y - containerSize.y ;
  572. // }
  573. //
  574. //
  575. // this.container.setStyle("top", top);
  576. // this.container.setStyle("left", left);
  577. // }
  578. },
  579. showYear: function(year){
  580. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  581. var date = new Date(thisYear, 1, 1);
  582. date.increment("year", -2);
  583. var beginYear = date.getFullYear();
  584. date.increment("year", this.yearLength-1);
  585. var endYear = date.getFullYear();
  586. this._setYearTitle(null, beginYear, endYear, thisYear);
  587. this._setYearDate(null, beginYear, endYear, thisYear);
  588. this.showMonthYearButton();
  589. // if (!this.move){
  590. // this.move = true;
  591. // this.containerDrag = new Drag.Move(this.container);
  592. // }
  593. },
  594. _setYearTitle:function(node, beginYear, endYear, thisYear){
  595. var thisNode = node || this.currentTextNode;
  596. thisNode.set("text", beginYear+"-"+endYear);
  597. thisNode.store("year", thisYear);
  598. this.setTitleStyle();
  599. },
  600. _setYearDate: function(table, beginYear, endYear, year){
  601. var yearTable = table || this.contentYearTable;
  602. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  603. var tbody = yearTable.getElement("tbody");
  604. var tds = tbody.getElements("td");
  605. tds.each(function(item, idx){
  606. var y = beginYear+idx;
  607. item.set("text", y);
  608. item.store("year", y);
  609. if (y==this.options.baseDate.getFullYear()){
  610. item.addClass("current_"+this.options.style);
  611. }else{
  612. item.removeClass("current_"+this.options.style);
  613. }
  614. if( this.options.enableDate ){
  615. if( !this.isEnableDate(y+"-01-01") && !this.isEnableDate(y+"-12-12") ){
  616. // item.addClass("disable_"+this.options.style);
  617. item.setStyles(this.css["disable_"+this.options.style]);
  618. }else{
  619. // item.removeClass("disable_"+this.options.style);
  620. item.setStyles(this.css["notdisable_"+this.options.style]);
  621. }
  622. }
  623. }.bind(this));
  624. },
  625. showMonth: function(year, month){
  626. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  627. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  628. this._setMonthTitle(null, thisYear, thisMonth);
  629. this._setMonthDate(null, thisYear, thisMonth);
  630. this.showMonthYearButton();
  631. // if (!this.move){
  632. // this.move = true;
  633. // this.containerDrag = new Drag.Move(this.container);
  634. // }
  635. },
  636. _setMonthTitle:function(node, year){
  637. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  638. var thisNode = node || this.currentTextNode;
  639. thisNode.set("text", thisYear);
  640. thisNode.store("year", thisYear);
  641. this.setTitleStyle();
  642. },
  643. _setMonthDate: function(table, year, month){
  644. //var months = Locale.get("Date").months;
  645. var months = o2.LP.widget.months;
  646. var monthTable = table || this.contentMonthTable;
  647. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  648. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  649. var tbody = monthTable.getElement("tbody");
  650. var tds = tbody.getElements("td");
  651. tds.each(function(item, idx){
  652. item.set("text", months[idx].substr(0,2));
  653. item.store("year", thisYear);
  654. item.store("month", idx);
  655. if ((thisYear==this.options.baseDate.getFullYear()) && (idx==this.options.baseDate.getMonth())){
  656. item.addClass("current_"+this.options.style);
  657. }else{
  658. item.removeClass("current_"+this.options.style);
  659. }
  660. if( this.options.enableDate ){
  661. var flag = false;
  662. var m = this.addZero(idx + 1, 2 );
  663. if (this.isEnableDate(thisYear+"-"+m+"-01")){
  664. flag = true;
  665. }else if(this.isEnableDate(thisYear+"-"+m+"-"+new Date(thisYear+"-"+m+"-01").get('lastdayofmonth'))){
  666. flag = true;
  667. }
  668. if( !flag ){
  669. // item.addClass("disable_"+this.options.style);
  670. item.setStyles(this.css["disable_"+this.options.style]);
  671. }else{
  672. // item.removeClass("disable_"+this.options.style);
  673. item.setStyles(this.css["notdisable_"+this.options.style]);
  674. }
  675. }
  676. }.bind(this));
  677. },
  678. showDay: function(year, month){
  679. this._setDayTitle(null, year, month);
  680. this._setDayWeekTitleTh();
  681. this._setDayDate(null, year, month);
  682. // if (!this.move){
  683. // this.move = true;
  684. // this.containerDrag = new Drag.Move(this.container);
  685. // }
  686. },
  687. _setDayTitle: function(node, year, month){
  688. var thisYear = (year!=undefined) ? year : this.options.baseDate.getFullYear();
  689. var thisMonth = (month!=undefined) ? month : this.options.baseDate.getMonth();
  690. thisMonth++;
  691. var text = thisYear+ o2.LP.widget.year +thisMonth+ o2.LP.widget.month;
  692. var thisNode = node || this.currentTextNode;
  693. thisNode.set("text", text);
  694. thisNode.store("year", thisYear);
  695. thisNode.store("month", thisMonth);
  696. this.setTitleStyle();
  697. },
  698. _setDayDate: function(table, year, month){
  699. var dayTable = table || this.contentTable;
  700. var baseDate = this.options.baseDate;
  701. if ((year!=undefined) && (month!=undefined)){
  702. baseDate = new Date();
  703. baseDate.setDate(1);
  704. baseDate.setFullYear(year);
  705. baseDate.setMonth(month);
  706. baseDate.setHours( this.options.baseDate.getHours() );
  707. baseDate.setMinutes( this.options.baseDate.getMinutes() );
  708. baseDate.setSeconds( this.options.baseDate.getSeconds() );
  709. }
  710. var tbody = dayTable.getElement("tbody");
  711. var tds = tbody.getElements("td");
  712. var firstDate = baseDate.clone();
  713. firstDate.setDate(1);
  714. // var day = firstDate.getDay();
  715. var day = (7 + firstDate.getDay() - this.options.weekBegin) % 7;
  716. var tmpDate = firstDate.clone();
  717. for (var i=day-1; i>=0; i--){
  718. if( this.options.enableDate ){
  719. if( !this.isEnableDate(firstDate) ){
  720. tds[i].addClass("disable_"+this.options.style);
  721. tds[i].setStyles(this.css["disable_"+this.options.style]);
  722. }else{
  723. tds[i].removeClass("disable_"+this.options.style);
  724. tds[i].setStyles(this.css["notdisable_"+this.options.style]);
  725. }
  726. }
  727. tmpDate.increment("day", -1);
  728. tds[i].set("text", tmpDate.getDate());
  729. tds[i].addClass("gray_"+this.options.style);
  730. tds[i].setStyles(this.css["gray_"+this.options.style]);
  731. tds[i].store("dateValue", tmpDate.toString());
  732. tds[i].removeClass("today_"+this.options.style);
  733. tds[i].removeClass("current_"+this.options.style);
  734. tds[i].removeClass("past_"+this.options.style);
  735. if( this.options.todayClass ){
  736. tds[i].removeClass( this.options.todayClass );
  737. }
  738. }
  739. for (var i=day; i<tds.length; i++){
  740. tds[i].set("text", firstDate.getDate());
  741. if( this.options.enableDate ){
  742. if( !this.isEnableDate(firstDate) ){
  743. // tds[i].addClass("disable_"+this.options.style);
  744. tds[i].setStyles(this.css["disable_"+this.options.style]);
  745. }else{
  746. // tds[i].removeClass("disable_"+this.options.style);
  747. tds[i].setStyles(this.css["notdisable_"+this.options.style]);
  748. }
  749. }
  750. if (firstDate.toString() == this.options.baseDate.toString()){
  751. tds[i].addClass("current_"+this.options.style);
  752. tds[i].setStyles(this.css["current_"+this.options.style]);
  753. tds[i].removeClass("past_"+this.options.style);
  754. tds[i].removeClass("gray_"+this.options.style);
  755. tds[i].setStyle("border", "1px solid #FFF");
  756. }else if (firstDate.getMonth()!=baseDate.getMonth()){
  757. tds[i].addClass("gray_"+this.options.style);
  758. tds[i].setStyles(this.css["gray_"+this.options.style]);
  759. tds[i].removeClass("current_"+this.options.style);
  760. tds[i].removeClass("past_"+this.options.style);
  761. tds[i].setStyle("border", "1px solid #FFF");
  762. }else{
  763. tds[i].setStyles(this.css["normal_"+this.options.style]);
  764. tds[i].removeClass("current_"+this.options.style);
  765. tds[i].removeClass("gray_"+this.options.style);
  766. tds[i].removeClass("past_"+this.options.style);
  767. tds[i].setStyle("border", "1px solid #FFF");
  768. }
  769. var tmp = firstDate.clone();
  770. if (tmp.clearTime().toString() == this.today.clearTime().toString()){
  771. tds[i].addClass("today_"+this.options.style);
  772. tds[i].removeClass("past_"+this.options.style);
  773. tds[i].setStyles(this.css["today_"+this.options.style]);
  774. tds[i].setStyle("border", "0px solid #AAA");
  775. if( this.options.todayClass ){
  776. tds[i].addClass( this.options.todayClass );
  777. }
  778. }else{
  779. tds[i].removeClass("today_"+this.options.style);
  780. if( this.options.todayClass ){
  781. tds[i].removeClass( this.options.todayClass );
  782. }
  783. }
  784. if (tmp.diff(this.today)>0){
  785. if (this.css["past_"+this.options.style]) tds[i].setStyles(this.css["past_"+this.options.style]);
  786. tds[i].addClass("past_"+this.options.style);
  787. }
  788. tds[i].store("dateValue", firstDate.toString());
  789. firstDate.increment("day", 1);
  790. }
  791. },
  792. changeViewToTime: function(date){
  793. this.currentView = "time";
  794. if (!this.contentTimeTable){
  795. this.contentTimeTable = this.createContentTable();
  796. this.contentTimeTable.inject(this.contentDateNode);
  797. }
  798. if (this.contentTable) this.contentTable.setStyle("display", "none");
  799. if (this.contentYearTable) this.contentYearTable.setStyle("display", "none");
  800. if (this.contentMonthTable) this.contentMonthTable.setStyle("display", "none");
  801. if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "block");
  802. // if (this.contentTimeTable) this.contentTimeTable.setStyle("display", "table");
  803. var thisDate = date || this.options.baseDate;
  804. this.showTime(thisDate);
  805. this.hideMonthYearButton();
  806. },
  807. showTime: function(date){
  808. var thisHour = (date || this.options.baseDate).getHours();
  809. var thisMinutes = (date || this.options.baseDate).getMinutes();
  810. var thisSeconds = (date || this.options.baseDate).getSeconds();
  811. // var times = this.options.defaultTime.split(":");
  812. //
  813. // var thisHour = (times[0]) ? times[0] : "0";
  814. // var thisMinutes = (times[1]) ? times[1] : "0";
  815. // var thisSeconds = (times[2]) ? times[2] : "0";
  816. this._setTimeTitle(null, date);
  817. if( this.options.style.indexOf("mobile") > -1 ){
  818. this._setTimeDate_mobile(null, thisHour, thisMinutes, thisSeconds);
  819. }else{
  820. this._setTimeDate(null, thisHour, thisMinutes, thisSeconds);
  821. }
  822. // if (this.move){
  823. // this.move = false;
  824. // this.container.removeEvents("mousedown");
  825. // }
  826. },
  827. _setTimeTitle: function(node, date){
  828. var thisDate = date || this.options.baseDate;
  829. var thisNode = node || this.currentTextNode;
  830. var y = thisDate.getFullYear();
  831. var m = thisDate.getMonth()+1;
  832. var d = thisDate.getDate();
  833. var text = "" + y + o2.LP.widget.year + m + o2.LP.widget.month + d + o2.LP.widget.date;
  834. if (this.options.timeOnly){
  835. thisNode.hide();
  836. if (this.prevNode) this.prevNode.hide();
  837. if (this.nextNode) this.nextNode.hide();
  838. }
  839. thisNode.set("text", text);
  840. thisNode.store("date", date);
  841. this.cDate = date;
  842. this.setTitleStyle();
  843. },
  844. _setTimeDate_mobile: function(node, h, m, s){
  845. var _self = this;
  846. this.itmeHNode = this.contentTimeTable.getElement(".MWF_calendar_time_h");
  847. this.itmeMNode = this.contentTimeTable.getElement(".MWF_calendar_time_m");
  848. this.itmeSNode = this.contentTimeTable.getElement(".MWF_calendar_time_s");
  849. this.showActionNode = this.contentTimeTable.getElement(".MWF_calendar_action_show");
  850. var calendar = this;
  851. var div, items;
  852. this.calculateCurrentHour(h);
  853. if( this.hMobileSelect ){
  854. this.itmeHNode.empty();
  855. this.hMobileSelect = null;
  856. }
  857. items = [];
  858. for( var i=0; i<24; i++ ) {
  859. div = new Element("div.hselect", {
  860. "text": this.addZero(i, 2),
  861. "styles": this.css.calendarTimeSelectItem_mobile
  862. }).inject(this.itmeHNode);
  863. div.store("d", i);
  864. if (!this.isEnableHour(this.cDate, i)){
  865. div.hide();
  866. }else{
  867. items.push(i);
  868. }
  869. }
  870. this.selectedHour = this.cHour; //this.addZero(h, 2 );
  871. this.hMobileSelect = new o2.Calendar.MobileSelect( this.itmeHNode.getParent(), {
  872. "lineHeight" : 40,
  873. "items" : items, //24,
  874. "currentItem" : parseInt( this.cHour ),
  875. "onChange": function(value){
  876. this.selectedHour = value; //this.addZero(value, 2 );
  877. this.cHour = this.selectedHour;
  878. if( this.options.enableMinutes )this._resetMinuteSelect_mobile();
  879. if( this.options.enableSeconds )this._resetSecondSelect_mobile();
  880. //this.showHNode.set("text", this.addZero(i, 2 ));
  881. //this.itmeHNode.getFirst().set("text", this.addZero(i, 2 ));
  882. }.bind(this)
  883. });
  884. this.hMobileSelect.load();
  885. this.calculateCurrentMinute(m);
  886. if( this.mMobileSelect ){
  887. this.itmeMNode.empty();
  888. this.mMobileSelect = null;
  889. }
  890. items = [];
  891. for (var i = 0; i < 60; i++) {
  892. div = new Element("div.mselect", {
  893. "text": this.addZero(i, 2),
  894. "styles": this.css.calendarTimeSelectItem_mobile
  895. }).inject(this.itmeMNode);
  896. div.store("d", i);
  897. if (!this.isEnableMinute(this.cDate, this.cHour, i)) {
  898. div.hide();
  899. }else{
  900. items.push(i);
  901. }
  902. }
  903. this.selectedMinute = this.cMinute; //this.addZero(m, 2);
  904. this.mMobileSelect = new o2.Calendar.MobileSelect(this.itmeMNode.getParent(), {
  905. "lineHeight": 40,
  906. "items": items, //60,
  907. "currentItem": parseInt(this.cMinute),
  908. "onChange": function (value) {
  909. this.selectedMinute = value; //this.addZero(value, 2);
  910. //this.showHNode.set("text", this.addZero(i, 2 ));
  911. //this.itmeHNode.getFirst().set("text", this.addZero(i, 2 ));
  912. this.cMinute = this.selectedMinute;
  913. if( this.options.enableSeconds )this._resetSecondSelect_mobile();
  914. }.bind(this)
  915. });
  916. this.mMobileSelect.load();
  917. if(this.options.secondEnable ){
  918. this.calculateCurrentSecond(s);
  919. if( this.sMobileSelect ){
  920. this.itmeSNode.empty();
  921. this.sMobileSelect = null;
  922. }
  923. items = [];
  924. for( var i=0; i<60; i++ ){
  925. div = new Element("div.sselect",{
  926. "text" : this.addZero(i, 2 ),
  927. "styles" : this.css.calendarTimeSelectItem_mobile
  928. }).inject( this.itmeSNode );
  929. div.store("d", i);
  930. if (!this.isEnableMinute(this.cDate, this.cHour, this.cMinute, i)) {
  931. div.hide();
  932. }else{
  933. items.push(i);
  934. }
  935. }
  936. this.selectedSecond = this.cSecond; //this.addZero(s, 2 );
  937. this.sMobileSelect = new o2.Calendar.MobileSelect( this.itmeSNode.getParent(), {
  938. "lineHeight" : 40,
  939. "items" : items, //60,
  940. "currentItem" : parseInt(this.cSecond),
  941. "onChange": function(value){
  942. this.selectedSecond = value; //this.addZero(value, 2 );
  943. //this.showHNode.set("text", this.addZero(i, 2 ));
  944. //this.itmeHNode.getFirst().set("text", this.addZero(i, 2 ));
  945. this.cSecond = this.selectedSecond;
  946. }.bind(this)
  947. });
  948. this.sMobileSelect.load();
  949. }else{
  950. this.itmeSNode.hide();
  951. }
  952. if( this.options.secondEnable ){
  953. this.contentTimeTable.getElements(".calendarTimeWheel_mobile").setStyle("width","33.3%");
  954. }else{
  955. this.contentTimeTable.getElements(".calendarTimeWheel_mobile").setStyle("width","50%");
  956. }
  957. if( this.options.secondEnable && this.showSNode ){
  958. this.showSNode.set("text", this.addZero( s.toInt(), 2) );
  959. }
  960. if (!this.okButton){
  961. this.okButton = new Element("button.mainColor_bg", {"text": o2.LP.widget.ok }).inject(this.showActionNode);
  962. this.okButton.addEvent("click", function(){
  963. this._selectTime();
  964. this.hide();
  965. }.bind(this));
  966. this.okButton.setStyles(this.css.calendarActionShowButton_mobile_ok);
  967. }
  968. if (!this.clearButton && this.options.clearEnable){
  969. this.clearButton = new Element("button", {"text": o2.LP.widget.clear }).inject(this.showActionNode);
  970. this.clearButton.addEvent("click", function(){
  971. var t = this.node.get("value");
  972. this.node.set("value", "");
  973. if( t )this.fireEvent("change");
  974. this.fireEvent("clear");
  975. this.hide();
  976. }.bind(this));
  977. this.clearButton.setStyles(this.css.calendarActionShowButton_mobile_cancel);
  978. }
  979. },
  980. _setTimeDate: function(node, h, m, s){
  981. if( !this.options.secondEnable ){
  982. var div = this.contentTimeTable.getElement(".MWF_calendar_time_s");
  983. if( div )div.hide();
  984. div = this.contentTimeTable.getElement(".MWF_calendar_time_show_s");
  985. if( div )div.hide();
  986. }
  987. this.itmeHNode = this.contentTimeTable.getElement(".MWF_calendar_time_h_slider");
  988. this.itmeMNode = this.contentTimeTable.getElement(".MWF_calendar_time_m_slider");
  989. this.itmeSNode = this.contentTimeTable.getElement(".MWF_calendar_time_s_slider");
  990. this.timeShowNode = this.contentTimeTable.getElement(".MWF_calendar_time_show");
  991. this.timeShowNode.addEvent("click", function(){
  992. this._selectTime();
  993. }.bind(this));
  994. this.showHNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_h");
  995. this.showMNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_m");
  996. this.showSNode = this.contentTimeTable.getElement(".MWF_calendar_time_show_s");
  997. this.showActionNode = this.contentTimeTable.getElement(".MWF_calendar_action_show");
  998. var calendar = this;
  999. if ( COMMON.Browser.Platform.isMobile ){
  1000. this.showHNode.set("text", this.addZero( h.toInt(), 2) );
  1001. this.showMNode.set("text", this.addZero( m.toInt(), 2));
  1002. if( this.options.secondEnable && this.showSNode ){
  1003. this.showSNode.set("text", this.addZero( s.toInt(), 2) );
  1004. }
  1005. }else{
  1006. if(this.options.timeSelectType === "select"){
  1007. this.loadHourSelect(h);
  1008. this.loadMinuteSelect(m);
  1009. this.loadSecondSelect(s);
  1010. }else {
  1011. this.calculateCurrentHour(h);
  1012. this.createDisabledNodes(this.itmeHNode, 24, "h");
  1013. var hourChangehandler = o2.Calendar.antiShake( function ( value ){
  1014. var value1 = Math.round(value);
  1015. var nearValue = this.isEnableHour(this.cDate, value1, true);
  1016. var v = nearValue === true ? value1 : nearValue;
  1017. if( this.selectedHour === v )return;
  1018. this.selectedHour = v;
  1019. this.cHour = v;
  1020. this.showHNode.set("text", this.addZero(v, 2));
  1021. this.itmeHNode.getFirst().set("text", this.addZero(v, 2));
  1022. if( this.options.enableMinutes ){
  1023. this.calculateCurrentMinute();
  1024. this.createDisabledNodes(this.itmeMNode, 60, "m");
  1025. this.mSlider.set( this.cMinute );
  1026. this.itmeMNode.getFirst().set("text", this.addZero( this.cMinute, 2));
  1027. this.showMNode.set("text", this.addZero( this.cMinute, 2));
  1028. }
  1029. if( this.options.enableSeconds && this.sSlider ){
  1030. this.calculateCurrentSecond();
  1031. this.createDisabledNodes(this.itmeSNode, 60, "s");
  1032. this.sSlider.set( this.cSecond );
  1033. this.itmeSNode.getFirst().set("text", this.addZero( this.cSecond, 2));
  1034. this.showSNode.set("text", this.addZero( this.cSecond, 2) );
  1035. }
  1036. }.bind(this), 200, this);
  1037. this.hSlider = new Slider(this.itmeHNode, this.itmeHNode.getFirst(), {
  1038. range: [0, 23],
  1039. initialStep: this.cHour,
  1040. onChange: function(value){
  1041. hourChangehandler(value);
  1042. }.bind(this)
  1043. });
  1044. // if( this.options.enableHours ){
  1045. // this.hSlider.setRange( this.getEnableHourStartEnd(this.cDate) );
  1046. // }
  1047. this.itmeHNode.getFirst().set("text", this.addZero( this.cHour, 2));
  1048. this.showHNode.set("text", this.addZero( this.cHour, 2) );
  1049. this.calculateCurrentMinute(m);
  1050. this.createDisabledNodes(this.itmeMNode, 60, "m");
  1051. var minuteChangehandler = o2.Calendar.antiShake( function(value){
  1052. var value1 = Math.round(value);
  1053. var nearValue = this.isEnableMinute(this.cDate, this.cHour, value1, true);
  1054. var v = nearValue === true ? value1 : nearValue;
  1055. if( this.selectedMinute === v )return;
  1056. this.selectedMinute = v;
  1057. this.cMinute = v;
  1058. this.showMNode.set("text", this.addZero( v, 2));
  1059. this.itmeMNode.getFirst().set("text", this.addZero( v, 2));
  1060. if( this.options.enableSeconds && this.sSlider ){
  1061. this.calculateCurrentSecond();
  1062. this.createDisabledNodes(this.itmeSNode, 60, "s");
  1063. this.sSlider.set( this.cSecond );
  1064. this.itmeSNode.getFirst().set("text", this.addZero( this.cSecond, 2));
  1065. this.showSNode.set("text", this.addZero( this.cSecond, 2) );
  1066. }
  1067. }.bind(this), 200, this);
  1068. this.mSlider = new Slider(this.itmeMNode, this.itmeMNode.getFirst(), {
  1069. range: [0, 59],
  1070. initialStep: this.cMinute,
  1071. onChange: function(value){
  1072. minuteChangehandler(value);
  1073. }.bind(this)
  1074. });
  1075. this.itmeMNode.getFirst().set("text", this.addZero( this.cMinute, 2));
  1076. this.showMNode.set("text", this.addZero( this.cMinute, 2));
  1077. if( this.options.secondEnable && this.itmeSNode ){
  1078. this.calculateCurrentSecond(s);
  1079. this.createDisabledNodes(this.itmeSNode, 60, "s");
  1080. var secondChangehandler = o2.Calendar.antiShake( function(value){
  1081. var value1 = Math.round(value);
  1082. var nearValue = this.isEnableSecond(this.cDate, this.cHour, this.cMinute, value1, true);
  1083. var v = nearValue === true ? value1 : nearValue;
  1084. if( this.selectedSecond === v )return;
  1085. this.selectedSecond = v;
  1086. this.cSecond = v;
  1087. this.showSNode.set("text", this.addZero( v, 2));
  1088. this.itmeSNode.getFirst().set("text", this.addZero( v, 2));
  1089. }.bind(this), 200, this);
  1090. this.sSlider = new Slider(this.itmeSNode, this.itmeSNode.getFirst(), {
  1091. range: [0, 59],
  1092. initialStep: this.cSecond,
  1093. onChange: function(value){
  1094. secondChangehandler(value);
  1095. }.bind(this)
  1096. });
  1097. this.itmeSNode.getFirst().set("text", this.addZero( this.cSecond, 2));
  1098. this.showSNode.set("text", this.addZero( this.cSecond, 2) );
  1099. }
  1100. // this.showHNode.set("text", this.addZero( h.toInt(), 2) );
  1101. // this.showMNode.set("text", this.addZero( m.toInt(), 2));
  1102. // if( this.options.secondEnable && this.showSNode ){
  1103. // this.showSNode.set("text", this.addZero( s.toInt(), 2) );
  1104. // }
  1105. }
  1106. }
  1107. if (!this.okButton){
  1108. this.okButton = new Element("button", {"text": o2.LP.widget.ok}).inject(this.showActionNode);
  1109. this.okButton.addEvent("click", function(){
  1110. this._selectTime();
  1111. this.hide();
  1112. }.bind(this));
  1113. this.okButton.setStyles(this.css.calendarActionShowButton);
  1114. }
  1115. if (!this.clearButton && this.options.clearEnable){
  1116. this.clearButton = new Element("button", {"text": o2.LP.widget.clear }).inject(this.showActionNode);
  1117. this.clearButton.addEvent("click", function(){
  1118. var t = this.node.get("value");
  1119. this.node.set("value", "");
  1120. if( t )this.fireEvent("change");
  1121. this.fireEvent("clear");
  1122. this.hide();
  1123. }.bind(this));
  1124. this.clearButton.setStyles(this.css.calendarActionShowButton);
  1125. }
  1126. },
  1127. _resetTimeDate: function(){
  1128. if( this.options.style.indexOf("mobile") > -1 ){
  1129. if( this.options.enableHours )this._resetHourSelect_mobile();
  1130. if( this.options.enableMinutes )this._resetMinuteSelect_mobile();
  1131. if( this.options.enableSeconds )this._resetSecondSelect_mobile();
  1132. }else{
  1133. if(this.options.timeSelectType === "select"){
  1134. if( this.options.enableHours )this.loadHourSelect();
  1135. if( this.options.enableMinutes )this.loadMinuteSelect();
  1136. if( this.options.enableSeconds )this.loadSecondSelect();
  1137. }else {
  1138. if( this.options.enableHours ){
  1139. this.calculateCurrentHour();
  1140. this.createDisabledNodes(this.itmeHNode, 24, "h");
  1141. this.hSlider.set( this.cHour );
  1142. this.itmeHNode.getFirst().set("text", this.addZero( this.cHour, 2));
  1143. this.showHNode.set("text", this.addZero( this.cHour, 2) );
  1144. }
  1145. if( this.options.enableMinutes ){
  1146. this.calculateCurrentMinute();
  1147. this.createDisabledNodes(this.itmeMNode, 60, "m");
  1148. this.mSlider.set( this.cMinute );
  1149. this.itmeMNode.getFirst().set("text", this.addZero( this.cMinute, 2));
  1150. this.showMNode.set("text", this.addZero( this.cMinute, 2));
  1151. }
  1152. if( this.options.enableSeconds && this.sSlider ){
  1153. this.calculateCurrentSecond();
  1154. this.createDisabledNodes(this.itmeSNode, 60, "s");
  1155. this.sSlider.set( this.cSecond );
  1156. this.itmeSNode.getFirst().set("text", this.addZero( this.cSecond, 2));
  1157. this.showSNode.set("text", this.addZero( this.cSecond, 2) );
  1158. }
  1159. }
  1160. }
  1161. },
  1162. _resetHourSelect_mobile: function(){
  1163. if( this.options.enableHours ){
  1164. this.calculateCurrentHour();
  1165. var items = [];
  1166. this.itmeHNode.getElements(".hselect").each(function(div, i){
  1167. if( this.isEnableHour(this.cDate, div.retrieve("d")) ){
  1168. items.push(i);
  1169. div.show();
  1170. }else{
  1171. div.hide();
  1172. }
  1173. }.bind(this));
  1174. this.selectedHour = this.cHour;
  1175. this.hMobileSelect.resetItems(items, parseInt(this.cHour));
  1176. }
  1177. },
  1178. _resetMinuteSelect_mobile: function(){
  1179. if( this.options.enableMinutes ) {
  1180. this.calculateCurrentMinute();
  1181. var items = [];
  1182. this.itmeMNode.getElements(".mselect").each(function(div, i){
  1183. if( this.isEnableMinute(this.cDate, this.cHour, div.retrieve("d")) ){
  1184. items.push(i);
  1185. div.show();
  1186. }else{
  1187. div.hide();
  1188. }
  1189. }.bind(this));
  1190. this.selectedMinute = this.cMinute;
  1191. this.mMobileSelect.resetItems(items, parseInt(this.cMinute));
  1192. }
  1193. },
  1194. _resetSecondSelect_mobile: function(){
  1195. if( this.options.enableSeconds && this.options.secondEnable ){
  1196. this.calculateCurrentSecond();
  1197. var items = [];
  1198. this.itmeSNode.getElements(".sselect").each(function(div, i){
  1199. if( this.isEnableSecond(this.cDate, this.cHour, this.cMinute, div.retrieve("d")) ){
  1200. items.push(i);
  1201. div.show();
  1202. }else{
  1203. div.hide();
  1204. }
  1205. }.bind(this));
  1206. this.selectedSecond = this.cSecond;
  1207. if(this.sMobileSelect)this.sMobileSelect.resetItems(items, parseInt(this.cSecond));
  1208. }
  1209. },
  1210. loadHourSelect: function (h) {
  1211. this.itmeHNode = this.contentTimeTable.getElement(".MWF_calendar_time_h").empty();
  1212. new Element("span",{"text": o2.LP.widget.hour + ":"}).inject(this.itmeHNode);
  1213. this.itmeSelectHNode = new Element("select").inject(this.itmeHNode);
  1214. this.calculateCurrentHour(h);
  1215. for( var i=0; i<24; i++ ){
  1216. if( this.isEnableHour(this.cDate, i) ){
  1217. var opt = new Element("option",{
  1218. "text" : this.addZero(i, 2 ),
  1219. "value" : this.addZero(i, 2 ),
  1220. "styles" : this.css.calendarTimeSelectItem_mobile
  1221. }).inject( this.itmeSelectHNode );
  1222. if (Browser.name === "firefox") {
  1223. opt.addEvent( "mousedown", function(e){ e.stopPropagation(); });
  1224. }
  1225. if( i === this.cHour )opt.set("selected", true);
  1226. }
  1227. }
  1228. // this.itmeSelectHNode.set("value",this.addZero( this.cHour, 2));
  1229. this.showHNode.set("text", this.addZero( this.cHour, 2) );
  1230. this.itmeSelectHNode.addEvent("change",function(){
  1231. this.cHour = this.itmeSelectHNode.get("value").toInt();
  1232. this.selectedHour = this.cHour;
  1233. this.showHNode.set("text", this.itmeSelectHNode.get("value") );
  1234. if( this.options.enableMinutes )this.loadMinuteSelect();
  1235. if( this.options.enableSeconds )this.loadSecondSelect();
  1236. }.bind(this));
  1237. },
  1238. loadMinuteSelect: function (m) {
  1239. this.itmeMNode = this.contentTimeTable.getElement(".MWF_calendar_time_m").empty();
  1240. new Element("span", {"text": o2.LP.widget.minute + ":"}).inject(this.itmeMNode);
  1241. this.itmeSelectMNode = new Element("select").inject(this.itmeMNode);
  1242. this.calculateCurrentMinute(m);
  1243. for (var i = 0; i < 60; i++) {
  1244. if (this.isEnableMinute(this.cDate, this.cHour || 0, i)) {
  1245. var opt = new Element("option", {
  1246. "text": this.addZero(i, 2),
  1247. "value": this.addZero(i, 2),
  1248. "styles": this.css.calendarTimeSelectItem_mobile
  1249. }).inject(this.itmeSelectMNode);
  1250. if (Browser.name === "firefox") {
  1251. opt.addEvent( "mousedown", function(e){ e.stopPropagation(); });
  1252. }
  1253. if( i === this.cMinute )opt.set("selected", true);
  1254. }
  1255. }
  1256. // this.itmeSelectMNode.set("value", this.addZero(this.cMinute, 2));
  1257. this.showMNode.set("text", this.addZero( this.cMinute, 2) );
  1258. this.itmeSelectMNode.addEvent("change", function () {
  1259. this.cMinute = this.itmeSelectMNode.get("value").toInt();
  1260. this.selectedMinute = this.cMinute;
  1261. this.showMNode.set("text", this.itmeSelectMNode.get("value"));
  1262. if( this.options.enableSeconds )this.loadSecondSelect();
  1263. }.bind(this));
  1264. },
  1265. loadSecondSelect: function (s) {
  1266. this.itmeSNode = this.contentTimeTable.getElement(".MWF_calendar_time_s").empty();
  1267. this.cSecond = typeOf(s) !== "null" ? s.toInt() : 0;
  1268. if( this.options.secondEnable && this.itmeSNode ){
  1269. new Element("span",{"text":o2.LP.widget.second + ":"}).inject(this.itmeSNode);
  1270. this.itmeSelectSNode = new Element("select").inject(this.itmeSNode);
  1271. this.calculateCurrentSecond(s);
  1272. for( var i=0; i<60; i++ ){
  1273. if( this.isEnableSecond(this.cDate, this.cHour || 0, this.cMinute || 0, i) ){
  1274. var opt = new Element("option",{
  1275. "text" : this.addZero(i, 2 ),
  1276. "value" : this.addZero(i, 2 ),
  1277. "styles" : this.css.calendarTimeSelectItem_mobile,
  1278. "events": { click: function(e){ e.stopPropagation(); e.preventDefault(); } }
  1279. }).inject( this.itmeSelectSNode );
  1280. if (Browser.name === "firefox") {
  1281. opt.addEvent( "mousedown", function(e){ e.stopPropagation(); });
  1282. }
  1283. if( i === this.cSecond )opt.set("selected", true);
  1284. }
  1285. }
  1286. // this.itmeSelectSNode.set("value",this.addZero( this.cSecond, 2));
  1287. this.showSNode.set("text", this.addZero( this.cSecond, 2) );
  1288. this.itmeSelectSNode.addEvent("change",function(){
  1289. this.cSecond = this.itmeSelectSNode.get("value").toInt();
  1290. this.selectedSecond = this.cSecond;
  1291. this.showSNode.set("text", this.itmeSelectSNode.get("value") );
  1292. }.bind(this));
  1293. }
  1294. },
  1295. calculateCurrentHour: function(h){
  1296. if( typeOf(h) !== "null" ){
  1297. this.cHour = h.toInt();
  1298. }else if( typeOf(this.selectedHour) !== "null" ){
  1299. this.cHour = this.selectedHour;
  1300. }else{
  1301. this.cHour = 0;
  1302. }
  1303. if( !this.isEnableHour(this.cDate, this.cHour) ) {
  1304. var eHours = this.getEnableHours(this.cDate);
  1305. if( eHours.length ){
  1306. this.cHour = typeOf( eHours[0] ) === "array" ? eHours[0][0] : eHours[0];
  1307. }else{
  1308. this.cHour = 0;
  1309. }
  1310. }
  1311. this.selectedHour = this.cHour;
  1312. return this.cHour;
  1313. },
  1314. calculateCurrentMinute: function(m){
  1315. if( typeOf(m) !== "null" ){
  1316. this.cMinute = m.toInt();
  1317. }else if( typeOf(this.selectedMinute) !== "null" ){
  1318. this.cMinute = this.selectedMinute;
  1319. }else{
  1320. this.cMinute = 0;
  1321. }
  1322. if( !this.isEnableMinute(this.cDate, this.cHour, this.cMinute) ){
  1323. var eMinutes = this.getEnableMinutes(this.cDate, this.cHour);
  1324. if( eMinutes.length ){
  1325. this.cMinute = typeOf( eMinutes[0] ) === "array" ? eMinutes[0][0] : eMinutes[0]
  1326. }else{
  1327. this.cMinute = 0;
  1328. }
  1329. }
  1330. this.selectedMinute = this.cMinute;
  1331. return this.cMinute;
  1332. },
  1333. calculateCurrentSecond: function(s){
  1334. if( typeOf(s) !== "null" ){
  1335. this.cSecond = s.toInt();
  1336. }else if( typeOf(this.selectedSecond) !== "null" ){
  1337. this.cSecond = this.selectedSecond;
  1338. }else{
  1339. this.cSecond = 0;
  1340. }
  1341. if( !this.isEnableSecond(this.cDate, this.cHour, this.cMinute, this.cSecond) ){
  1342. var eSeconds = this.getEnableSeconds(this.cDate, this.cHour, this.cMinute);
  1343. if( eSeconds.length ){
  1344. this.cSecond = typeOf( eSeconds[0] ) === "array" ? eSeconds[0][0] : eSeconds[0]
  1345. }else{
  1346. this.cSecond = 0;
  1347. }
  1348. }
  1349. this.selectedSecond = this.cSecond;
  1350. return this.cSecond;
  1351. },
  1352. createDisabledNodes: function(area, length, type){
  1353. area.getElements(".disable_node").destroy();
  1354. var array;
  1355. switch(type){
  1356. case "h": array = this.getDisabledHours(this.cDate); break;
  1357. case "m": array = this.getDisabledMinutes(this.cDate, this.cHour || 0); break;
  1358. case "s": array = this.getDisabledSeconds(this.cDate, this.cHour || 0, this.cMinute || 0); break;
  1359. }
  1360. if( !array || !array.length )return false;
  1361. area.setStyle("position", "relative");
  1362. if( typeOf(array[0]) === "array" ){
  1363. for( var i=0; i< array.length; i++ ){
  1364. this.createDisabledNode( area, length, array[i] );
  1365. }
  1366. }else{
  1367. this.createDisabledNode( area, length, array );
  1368. }
  1369. },
  1370. createDisabledNode: function(area, length, range){
  1371. var s = area.getSize();
  1372. var width = s.x / length * (range[1] - range[0] + 1);
  1373. var left = s.x / length * range[0];
  1374. new Element("div.disable_node", {
  1375. styles: {
  1376. "position": "absolute",
  1377. "background": "#ccc",
  1378. "width": width+"px",
  1379. "height": s.y,
  1380. "left": left,
  1381. "top": "0px"
  1382. }
  1383. }).inject(area);
  1384. },
  1385. addZero : function( str, length ){
  1386. var zero = "";
  1387. str = str.toString();
  1388. for( var i=0; i<length; i++ ){
  1389. zero = zero + "0";
  1390. }
  1391. var s = zero + str;
  1392. return s.substr(s.length - length, length );
  1393. },
  1394. _selectTime: function(){
  1395. var date = this.currentTextNode.retrieve("date");
  1396. var h = typeOf(this.selectedHour) !== "null" ? this.selectedHour : this.showHNode.get("text");
  1397. var m = typeOf(this.selectedMinute) !== "null" ? this.selectedMinute : this.showMNode.get("text");
  1398. date.setHours(h);
  1399. date.setMinutes(m);
  1400. if( this.options.secondEnable && ( typeOf(this.selectedSecond) !== "null" || this.showSNode) ){
  1401. var s = typeOf(this.selectedSecond) !== "null" ? this.selectedSecond : this.showSNode.get("text");
  1402. date.setSeconds(s);
  1403. }
  1404. if (!this.options.beforeCurrent){
  1405. var now = new Date();
  1406. if (date.getTime()-now.getTime()<0){
  1407. alert( o2.LP.widget.dateGreaterThanCurrentNotice );
  1408. this.node.focus();
  1409. return false;
  1410. }
  1411. }
  1412. var dv = date.format(this.options.format);
  1413. if (this.fireEvent("queryComplate", [dv, date])){
  1414. var t = this.node.get("value");
  1415. this.options.defaultTime = ""+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
  1416. this.node.set("value", dv);
  1417. // this.node.focus();
  1418. this.hide();
  1419. if (t!=dv) this.fireEvent("change", [dv, date, t]);
  1420. this.fireEvent("complate", [dv, date]);
  1421. }
  1422. },
  1423. _selectDate: function(dateStr){
  1424. if( this.options.yearOnly ){
  1425. dateStr = dateStr+"-01-01"
  1426. }else if( this.options.monthOnly ){
  1427. dateStr = dateStr+"-01"
  1428. }
  1429. var date = new Date(dateStr);
  1430. this.options.baseDate = date;
  1431. var dv = date.format(this.options.format);
  1432. if (this.options.isTime){
  1433. this.changeViewToTime(date);
  1434. }else{
  1435. if (!this.options.beforeCurrent){
  1436. var now = new Date();
  1437. date.setHours(23,59,59);
  1438. if (date.getTime()-now.getTime()<0){
  1439. alert( o2.LP.widget.dateGreaterThanCurrentNotice );
  1440. this.node.focus();
  1441. return false;
  1442. }
  1443. }
  1444. if (this.fireEvent("queryComplate", [dv, date])){
  1445. var t = this.node.get("value");
  1446. this.node.set("value", dv);
  1447. this.hide();
  1448. if (t!=dv) this.fireEvent("change", [dv, date, t]);
  1449. this.fireEvent("complate", [dv, date, t]);
  1450. }
  1451. }
  1452. },
  1453. _setDayWeekTitleTh: function(table){
  1454. var dayTable = table || this.contentTable;
  1455. var thead = dayTable.getElement("thead");
  1456. var cells = thead.getElements("th");
  1457. if (this.css.calendarDaysContentTh) cells.setStyles(this.css.calendarDaysContentTh);
  1458. //var days_abbr = Locale.get("Date").days_abbr;
  1459. var days_abbr = o2.LP.widget.days_abbr;
  1460. cells.each(function(item, idx){
  1461. item.set("text", days_abbr[ (idx + this.options.weekBegin) % 7 ]);
  1462. }.bind(this));
  1463. return cells;
  1464. },
  1465. setTitleStyle: function(){
  1466. if( this.options.enableDate ){
  1467. if( this.currentView === "time" ){
  1468. var date = this.currentTextNode.retrieve("date");
  1469. if( this.isEnableDate(date.clone().decrement()) ){
  1470. this.prevNode.setStyles(this.css["notdisable_"+this.options.style]);
  1471. }else{
  1472. this.prevNode.setStyles(this.css["disable_"+this.options.style]);
  1473. }
  1474. if( this.isEnableDate(date.clone().increment()) ){
  1475. this.nextNode.setStyles(this.css["notdisable_"+this.options.style]);
  1476. }else{
  1477. this.nextNode.setStyles(this.css["disable_"+this.options.style]);
  1478. }
  1479. }else{
  1480. this.prevNode.setStyles(this.css["notdisable_"+this.options.style]);
  1481. this.nextNode.setStyles(this.css["notdisable_"+this.options.style]);
  1482. }
  1483. }
  1484. },
  1485. createContainer: function(){
  1486. var div = null;
  1487. var request = new Request.HTML({
  1488. url: this.options.containerPath,
  1489. method: "GET",
  1490. async: false,
  1491. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  1492. div = responseTree[0];
  1493. }
  1494. });
  1495. request.send();
  1496. //this.containerNode = div.getElement(".MWF_calendar_container");
  1497. this.titleNode = div.getElement(".MWF_calendar_title");
  1498. this.prevNode = div.getElement(".MWF_calendar_prev");
  1499. this.currentNode = div.getElement(".MWF_calendar_current");
  1500. this.currentTextNode = div.getElement(".MWF_calendar_currentText");
  1501. this.nextNode = div.getElement(".MWF_calendar_next");
  1502. this.contentNode = div.getElement(".MWF_calendar_content");
  1503. this.contentDateNode = div.getElement(".MWF_calendar_content_date");
  1504. this.contentTimeNode = div.getElement(".MWF_calendar_content_time");
  1505. this.buttonArea = div.getElement(".MWF_calendar_button_area");
  1506. this.bottomNode = div.getElement(".MWF_calendar_bottom");
  1507. div.setStyles(this.css.container);
  1508. this.titleNode.setStyles(this.css.dateTitle);
  1509. this.prevNode.setStyles(this.css.datePrev);
  1510. this.currentNode.setStyles(this.css.dateCurrent);
  1511. debugger;
  1512. this.currentTextNode.setStyles(this.css.dateCurrentText);
  1513. this.nextNode.setStyles(this.css.dateNext);
  1514. this.contentNode.setStyles(this.css.calendarContent);
  1515. if(this.buttonArea)this.buttonArea.setStyles(this.css.buttonArea);
  1516. this.bottomNode.setStyles(this.css.dateBottom);
  1517. return div;
  1518. },
  1519. createContentTable: function(){
  1520. var table = null;
  1521. var request = new Request.HTML({
  1522. url: this.options[this.currentView+"Path"],
  1523. method: "GET",
  1524. async: false,
  1525. onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
  1526. table = responseTree[0];
  1527. }
  1528. });
  1529. request.send();
  1530. var tbody = table.getElement("tbody");
  1531. if (tbody){
  1532. var tds = tbody.getElements("td");
  1533. var calendar = this;
  1534. tds.addEvent("click", function(){
  1535. switch (calendar.currentView) {
  1536. case "day" :
  1537. var d = this.retrieve("dateValue");
  1538. if( calendar.isEnableDate(d) ){
  1539. calendar._selectDate(d, this);
  1540. }
  1541. break;
  1542. case "month" :
  1543. var flag = true;
  1544. if( calendar.options.enableDate ){
  1545. flag = false;
  1546. d = calendar.addZero(this.retrieve("month").toInt() + 1, 2 );
  1547. var y = this.retrieve("year");
  1548. if (calendar.isEnableDate(y+"-"+d+"-01")){
  1549. flag = true;
  1550. }else if(calendar.isEnableDate(y+"-"+d+"-"+new Date(y+"-"+d+"-01").get('lastdayofmonth'))){
  1551. flag = true;
  1552. }
  1553. }
  1554. if( flag ){
  1555. if( calendar.options.monthOnly ){
  1556. var m = calendar.addZero(this.retrieve("month").toInt() + 1, 2 );
  1557. calendar._selectDate(this.retrieve("year")+"-"+ m , this);
  1558. }else{
  1559. calendar.changeViewToDay(this.retrieve("year"), this.retrieve("month"));
  1560. }
  1561. }
  1562. break;
  1563. case "year" :
  1564. d = this.retrieve("year");
  1565. if (calendar.isEnableDate(d+"-01-01") || calendar.isEnableDate(d+"-12-12")){
  1566. if( calendar.options.yearOnly ){
  1567. calendar._selectDate(this.retrieve("year"), this);
  1568. }else{
  1569. calendar.changeViewToMonth(this.retrieve("year"));
  1570. }
  1571. }
  1572. break;
  1573. case "time" :
  1574. //nothing
  1575. break;
  1576. default :
  1577. //nothing;
  1578. }
  1579. });
  1580. switch (this.currentView) {
  1581. case "day" :
  1582. if (!table.display) table.display="";
  1583. if (!table.style.display) table.style.display="";
  1584. table.setStyles(this.css.calendarDaysContent);
  1585. tds.setStyles(this.css.calendarDaysContentTd);
  1586. break;
  1587. case "month" :
  1588. table.setStyles(this.css.calendarMonthsContent);
  1589. tds.setStyles(this.css.calendarMonthsContentTd);
  1590. break;
  1591. case "year" :
  1592. this.yearLength = tds.length;
  1593. table.setStyles(this.css.calendarYearsContent);
  1594. tds.setStyles(this.css.calendarYearsContentTd);
  1595. break;
  1596. case "time" :
  1597. if( this.options.style.indexOf("mobile") > -1 ){
  1598. var nodes = table.getElements(".calendarTimeContent_mobile");
  1599. if (nodes.length) nodes.setStyles(this.css.calendarTimeContent_mobile);
  1600. nodes = table.getElements(".calendarTimeFixWidthNode_mobile");
  1601. if (nodes.length) nodes.setStyles(this.css.calendarTimeFixWidthNode_mobile);
  1602. nodes = table.getElements(".calendarTimeWheels_mobile");
  1603. if (nodes.length) nodes.setStyles(this.css.calendarTimeWheels_mobile);
  1604. nodes = table.getElements(".calendarTimeWheel_mobile");
  1605. if (nodes.length) nodes.setStyles(this.css.calendarTimeWheel_mobile);
  1606. nodes = table.getElements(".calendarTimeSelectContainer_mobile");
  1607. if (nodes.length) nodes.setStyles(this.css.calendarTimeSelectContainer_mobile);
  1608. nodes = table.getElements(".calendarTimeSelectLine_mobile");
  1609. if (nodes.length) nodes.setStyles(this.css.calendarTimeSelectLine_mobile);
  1610. nodes = table.getElements(".calendarTimeShadowMask_mobile");
  1611. if (nodes.length) nodes.setStyles(this.css.calendarTimeShadowMask_mobile);
  1612. var node = table.getElement(".MWF_calendar_action_show");
  1613. if (node){
  1614. node.setStyles(this.css.calendarActionShow);
  1615. //var buttons = node.getElements("button");
  1616. //buttons.setStyles(this.css.calendarActionShowButton);
  1617. }
  1618. }else{
  1619. var nodes = table.getElements(".calendarTimeArea");
  1620. if (nodes.length) nodes.setStyles(this.css.calendarTimeArea);
  1621. nodes = table.getElements(".calendarTimeSlider");
  1622. if (nodes.length) nodes.setStyles(this.css.calendarTimeSlider);
  1623. nodes = table.getElements(".calendarTimeSliderKnob");
  1624. if (nodes.length) nodes.setStyles(this.css.calendarTimeSliderKnob);
  1625. nodes = table.getElements(".calendarTimeShow");
  1626. if (nodes.length) nodes.setStyles(this.css.calendarTimeShow);
  1627. nodes = table.getElements(".calendarTimeShowItem");
  1628. if (nodes.length) nodes.setStyles(this.css.calendarTimeShowItem);
  1629. var node = table.getElement(".MWF_calendar_action_show");
  1630. if (node){
  1631. node.setStyles(this.css.calendarActionShow);
  1632. var buttons = node.getElements("button");
  1633. buttons.setStyles(this.css.calendarActionShowButton);
  1634. }
  1635. }
  1636. break;
  1637. default :
  1638. //nothing;
  1639. }
  1640. tds.addEvent("mouseover", function(){
  1641. var d;
  1642. switch (calendar.currentView) {
  1643. case "day" :
  1644. d = this.retrieve("dateValue");
  1645. if (calendar.isEnableDate(d)) this.setStyle("border", "1px solid #999999");
  1646. break;
  1647. case "month" :
  1648. if( calendar.options.enableDate ){
  1649. d = calendar.addZero(this.retrieve("month").toInt() + 1, 2 );
  1650. var y = this.retrieve("year");
  1651. if (calendar.isEnableDate(y+"-"+d+"-01")){
  1652. this.setStyle("border", "1px solid #999999");
  1653. }else if(calendar.isEnableDate(y+"-"+d+"-"+new Date(y+"-"+d+"-01").get('lastdayofmonth'))){
  1654. this.setStyle("border", "1px solid #999999");
  1655. }
  1656. }else{
  1657. this.setStyle("border", "1px solid #999999");
  1658. }
  1659. break;
  1660. case "year" :
  1661. d = this.retrieve("year");
  1662. if (calendar.isEnableDate(d+"-01-01") || calendar.isEnableDate(d+"-12-12")) this.setStyle("border", "1px solid #999999");
  1663. break;
  1664. default:
  1665. this.setStyle("border", "1px solid #999999");
  1666. break;
  1667. }
  1668. });
  1669. tds.addEvent("mouseout", function(){
  1670. this.setStyle("border", "1px solid #FFF");
  1671. // var d;
  1672. // switch (calendar.currentView) {
  1673. // case "day" :
  1674. // d = this.retrieve("dateValue");
  1675. // if (calendar.isEnableDate(d)) this.setStyle("border", "1px solid #FFF");
  1676. // break;
  1677. // default:
  1678. // this.setStyle("border", "1px solid #FFF");
  1679. // break;
  1680. // }
  1681. });
  1682. }else{
  1683. switch (this.currentView) {
  1684. case "day" :
  1685. table.setStyles(this.css.calendarDaysContent);
  1686. tds.setStyles(this.css.calendarDaysContentTd);
  1687. break;
  1688. case "month" :
  1689. table.setStyles(this.css.calendarMonthsContent);
  1690. tds.setStyles(this.css.calendarMonthsContentTd);
  1691. break;
  1692. case "year" :
  1693. this.yearLength = tds.length;
  1694. table.setStyles(this.css.calendarYearsContent);
  1695. tds.setStyles(this.css.calendarYearsContentTd);
  1696. break;
  1697. case "time" :
  1698. if( this.options.style.indexOf("mobile") > -1 ){
  1699. var nodes = table.getElements(".calendarTimeContent_mobile");
  1700. if (nodes.length) nodes.setStyles(this.css.calendarTimeContent_mobile);
  1701. nodes = table.getElements(".calendarTimeFixWidthNode_mobile");
  1702. if (nodes.length) nodes.setStyles(this.css.calendarTimeFixWidthNode_mobile);
  1703. nodes = table.getElements(".calendarTimeWheels_mobile");
  1704. if (nodes.length) nodes.setStyles(this.css.calendarTimeWheels_mobile);
  1705. nodes = table.getElements(".calendarTimeWheel_mobile");
  1706. if (nodes.length) nodes.setStyles(this.css.calendarTimeWheel_mobile);
  1707. nodes = table.getElements(".calendarTimeSelectContainer_mobile");
  1708. if (nodes.length) nodes.setStyles(this.css.calendarTimeSelectContainer_mobile);
  1709. nodes = table.getElements(".calendarTimeSelectLine_mobile");
  1710. if (nodes.length) nodes.setStyles(this.css.calendarTimeSelectLine_mobile);
  1711. nodes = table.getElements(".calendarTimeShadowMask_mobile");
  1712. if (nodes.length) nodes.setStyles(this.css.calendarTimeShadowMask_mobile);
  1713. var node = table.getElement(".MWF_calendar_action_show");
  1714. if (node){
  1715. node.setStyles(this.css.calendarActionShow);
  1716. //var buttons = node.getElements("button");
  1717. //buttons.setStyles(this.css.calendarActionShowButton);
  1718. }
  1719. }else{
  1720. var nodes = table.getElements(".calendarTimeArea");
  1721. if (nodes.length) nodes.setStyles(this.css.calendarTimeArea);
  1722. nodes = table.getElements(".calendarTimeSlider");
  1723. if (nodes.length) nodes.setStyles(this.css.calendarTimeSlider);
  1724. nodes = table.getElements(".calendarTimeSliderKnob");
  1725. if (nodes.length) nodes.setStyles(this.css.calendarTimeSliderKnob);
  1726. nodes = table.getElements(".calendarTimeShow");
  1727. if (nodes.length) nodes.setStyles(this.css.calendarTimeShow);
  1728. nodes = table.getElements(".calendarTimeShowItem");
  1729. if (nodes.length) nodes.setStyles(this.css.calendarTimeShowItem);
  1730. var node = table.getElement(".MWF_calendar_action_show");
  1731. if (node){
  1732. node.setStyles(this.css.calendarActionShow);
  1733. var buttons = node.getElements("button");
  1734. buttons.setStyles(this.css.calendarActionShowButton);
  1735. }
  1736. }
  1737. break;
  1738. default :
  1739. //nothing;
  1740. }
  1741. }
  1742. return table;
  1743. },
  1744. setRange: function(){
  1745. if( this.options.datetimeRange && this.options.datetimeRange.length ){
  1746. this.setDatetimeRange();
  1747. }else{
  1748. if(this.options.dateRange && this.options.dateRange.length){
  1749. this.setDateRange();
  1750. }
  1751. if(this.options.timeRange && this.options.timeRange.length){
  1752. this.setTimeRange();
  1753. }
  1754. }
  1755. },
  1756. setDatetimeRange: function(){
  1757. var arr = this.options.datetimeRange;
  1758. var _2dArray = typeOf(arr[0]) !== "array" ? [arr] : arr;
  1759. if( !_2dArray[0][0] && !_2dArray[0][1] )return;
  1760. this.datetimeRange = [];
  1761. this.dateRange = [];
  1762. this.dateOnlyRange = [];
  1763. _2dArray.each(function(a){
  1764. var ds, de, ds1, de1, de2;
  1765. if(a[0])ds = typeOf(a[0]) === "date" ? a[0] : Date.parse(a[0]);
  1766. if(a[1])de = typeOf(a[1]) === "date" ? a[1] : Date.parse(a[1]);
  1767. this.datetimeRange.push([ds, de]);
  1768. if(ds){
  1769. ds1 = ds.clone().clearTime();
  1770. }
  1771. if(de){
  1772. de1 = de.clone().set({hr:23, min:59, sec:59});
  1773. de2 = de.clone().clearTime();
  1774. }
  1775. this.dateRange.push([ds1, de1]);
  1776. this.dateOnlyRange.push([ds1, de2]);
  1777. }.bind(this));
  1778. this.options.enableDate = function (date) {
  1779. var d = typeOf(date) === "string" ? Date.parse(date) : date.clone();
  1780. d.clearTime();
  1781. for( var i=0; i<this.dateRange.length; i++ ){
  1782. var ar = this.dateRange[i];
  1783. if( !ar[0] && this.isLessEquals(d, ar[1]) )return true;
  1784. if( !ar[1] && this.isLessEquals(ar[0], d) )return true;
  1785. if( this.isLessEquals(ar[0], d) && this.isLessEquals(d, ar[1]) )return true;
  1786. }
  1787. return false;
  1788. }.bind(this);
  1789. this.options.enableHours = function (date) {
  1790. var d = typeOf(date) === "string" ? Date.parse(date) : date.clone();
  1791. d.clearTime();
  1792. var hours = [];
  1793. for( var i=0; i<this.dateOnlyRange.length; i++ ){
  1794. var ar = this.dateOnlyRange[i];
  1795. var equal1 = this.isEquals(ar[0], d), equal2 = this.isEquals(d, ar[1]);
  1796. if( equal1 || equal2){
  1797. var s = equal1 ? this.datetimeRange[i][0].get("hr") : 0;
  1798. var e = equal2 ? this.datetimeRange[i][1].get("hr") : 23;
  1799. hours.push( [s, e] );
  1800. }
  1801. }
  1802. return hours.length ? o2.Calendar.RangeArrayUtils.union(hours) : [0, 23];
  1803. }.bind(this);
  1804. this.options.enableMinutes = function (date, hour) {
  1805. var d = typeOf(date) === "string" ? Date.parse(date) : date.clone();
  1806. d.clearTime();
  1807. var minutes = [];
  1808. for( var i=0; i<this.dateOnlyRange.length; i++ ){
  1809. var ar = this.dateOnlyRange[i];
  1810. var ardt = this.datetimeRange[i];
  1811. var equal1 = (this.isEquals(ar[0], d) && hour === ardt[0].get("hr"));
  1812. var equal2 = (this.isEquals(d, ar[1]) && hour === ardt[1].get("hr"));
  1813. if( equal1 || equal2 ){
  1814. var s = equal1 ? ardt[0].get("min") : 0;
  1815. var e = equal2 ? ardt[1].get("min") : 59;
  1816. minutes.push( [s, e] );
  1817. }
  1818. }
  1819. return minutes.length ? o2.Calendar.RangeArrayUtils.union(minutes) : [0, 59];
  1820. }.bind(this);
  1821. this.options.enableSeconds = function (date, hour, minute) {
  1822. var d = typeOf(date) === "string" ? Date.parse(date) : date.clone();
  1823. d.clearTime();
  1824. var seconds = [];
  1825. for( var i=0; i<this.dateOnlyRange.length; i++ ){
  1826. var ar = this.dateOnlyRange[i];
  1827. var ardt = this.datetimeRange[i];
  1828. var equal1 = (this.isEquals(ar[0], d) && hour === ardt[0].get("hr") && minute === ardt[0].get("min"));
  1829. var equal2 = (this.isEquals(d, ar[1]) && hour === ardt[1].get("hr") && minute === ardt[1].get("min"));
  1830. if( equal1 || equal2 ){
  1831. var s = equal1 ? ardt[0].get("sec") : 0;
  1832. var e = equal2 ? ardt[1].get("sec") : 59;
  1833. seconds.push( [s, e] );
  1834. }
  1835. }
  1836. return seconds.length ? o2.Calendar.RangeArrayUtils.union(seconds) : [0, 59];
  1837. }.bind(this);
  1838. },
  1839. setDateRange: function(){
  1840. var arr = this.options.dateRange;
  1841. var _2dArray = typeOf(arr[0]) !== "array" ? [arr] : arr;
  1842. if( !_2dArray[0][0] && !_2dArray[0][1] )return;
  1843. this.dateRange = [];
  1844. _2dArray.each(function(a){
  1845. var ds, de, ds1, de1;
  1846. if(a[0])ds = typeOf(a[0]) === "date" ? a[0] : Date.parse(a[0]);
  1847. if(a[1])de = typeOf(a[1]) === "date" ? a[1] : Date.parse(a[1]);
  1848. if(ds)ds1 = ds.clearTime();
  1849. if(de)de1 = de.set({hr:23, min:59, sec:59});
  1850. this.dateRange.push([ds1, de1]);
  1851. }.bind(this));
  1852. this.options.enableDate = function (date) {
  1853. var d = typeOf(date) === "string" ? Date.parse(date) : date.clone();
  1854. d.clearTime();
  1855. for( var i=0; i<this.dateRange.length; i++ ){
  1856. var ar = this.dateRange[i];
  1857. if( !ar[0] && this.isLessEquals(d, ar[1]) )return true;
  1858. if( !ar[1] && this.isLessEquals(ar[0], d) )return true;
  1859. if( this.isLessEquals(ar[0], d) && this.isLessEquals(d , ar[1]) )return true;
  1860. }
  1861. return false;
  1862. }.bind(this);
  1863. },
  1864. setTimeRange: function(){
  1865. var arr = this.options.timeRange;
  1866. var _2dArray = typeOf(arr[0]) !== "array" ? [arr] : arr;
  1867. if( !_2dArray[0][0] && !_2dArray[0][1] )return;
  1868. this.datetimeRange2 = [];
  1869. _2dArray.each(function(a){
  1870. var ds, de, ds1, de1;
  1871. if(a[0])ds = Date.parse("2020-01-01 "+a[0]);
  1872. if(a[1])de = Date.parse("2020-01-01 "+a[1]);
  1873. this.datetimeRange2.push([ds, de]);
  1874. }.bind(this));
  1875. this.options.enableHours = function (date) {
  1876. // var d = typeOf(date) === "string" ? Date.parse(date) : date;
  1877. if(this.hourRange)return this.hourRange;
  1878. var hours = [];
  1879. for( var i=0; i<this.datetimeRange2.length; i++ ){
  1880. var ar = this.datetimeRange2[i];
  1881. var s = ar[0] ? ar[0].get("hr") : 0;
  1882. var e = ar[1] ? ar[1].get("hr") : 23;
  1883. hours.push( [s, e] );
  1884. }
  1885. this.hourRange = o2.Calendar.RangeArrayUtils.union(hours);
  1886. return this.hourRange;
  1887. }.bind(this);
  1888. this.options.enableMinutes = function (date, hour) {
  1889. // var d = typeOf(date) === "string" ? Date.parse(date) : date;
  1890. var minutes = [];
  1891. for( var i=0; i<this.datetimeRange2.length; i++ ){
  1892. var ar = this.datetimeRange2[i];
  1893. var equal1 = (ar[0] && hour === ar[0].get("hr"));
  1894. var equal2 = (ar[1] && hour === ar[1].get("hr"));
  1895. if( equal1 || equal2 ){
  1896. var s = equal1 ? ar[0].get("min") : 0;
  1897. var e = equal2 ? ar[1].get("min") : 59;
  1898. minutes.push( [s, e] );
  1899. }
  1900. }
  1901. return minutes.length ? o2.Calendar.RangeArrayUtils.union(minutes) : [0, 59];
  1902. }.bind(this);
  1903. this.options.enableSeconds = function (date, hour, minute) {
  1904. // var d = typeOf(date) === "string" ? Date.parse(date) : date;
  1905. var seconds = [];
  1906. for( var i=0; i<this.datetimeRange2.length; i++ ){
  1907. var ar = this.datetimeRange2[i];
  1908. var equal1 = (ar[0] && hour === ar[0].get("hr") && minute === ar[0].get("min"));
  1909. var equal2 = (ar[1] && hour === ar[1].get("hr") && minute === ar[1].get("min"));
  1910. if( equal1 || equal2 ){
  1911. var s = equal1 ? ar[0].get("sec") : 0;
  1912. var e = equal2 ? ar[1].get("sec") : 59;
  1913. seconds.push( [s, e] );
  1914. }
  1915. }
  1916. return seconds.length ? o2.Calendar.RangeArrayUtils.union(seconds) : [0, 59];
  1917. }.bind(this);
  1918. },
  1919. isGreatEquals: function( d1, d2 ){
  1920. return (d1 > d2) || ((d1 - d2) === 0);
  1921. },
  1922. isLessEquals: function( d1, d2 ){
  1923. return (d1 < d2) || ((d1 - d2) === 0);
  1924. },
  1925. isEquals: function(d1, d2){
  1926. return (d1 - d2) === 0;
  1927. },
  1928. isEnableDate: function(date){
  1929. var fun = this.options.enableDate;
  1930. if( fun && typeOf(fun) === "function" ){
  1931. var d = typeOf( date ) === "string" ? new Date(date) : date;
  1932. if( fun( d ) === false ){
  1933. return false;
  1934. }
  1935. }
  1936. return true;
  1937. },
  1938. getEnableHours: function(date){
  1939. var fun = this.options.enableHours;
  1940. if( fun && typeOf(fun) === "function" ){
  1941. var d = typeOf( date ) === "string" ? new Date(date) : date;
  1942. return fun( d );
  1943. }
  1944. return [0, 23];
  1945. },
  1946. getDisabledHours: function(date){
  1947. var ar = this.getEnableHours(date);
  1948. if( !ar || !ar.length || (ar[0] === 0 && ar[1] === 23) )return [];
  1949. if( typeOf(ar[0]) !== "array" )ar = [ar];
  1950. return o2.Calendar.RangeArrayUtils.complementary([0, 23], ar, null, 1);
  1951. },
  1952. // getEnableHourStartEnd: function(date){
  1953. // var ar = this.getEnableHours(date);
  1954. // return this._getEnableStartEnd( ar, 0, 23 );
  1955. // },
  1956. isEnableHour: function (thisDate, hour, nearly) {
  1957. var hs = this.getEnableHours( thisDate );
  1958. if( !hs || !hs.length || (hs[0] === 0 && hs[1] === 23) )return true;
  1959. if( typeOf(hs[0]) === "array" ){
  1960. for( var i=0; i< hs.length; i++ ){
  1961. var dhs = hs[i];
  1962. if( dhs[0] <= hour && hour <= dhs[1] )return true;
  1963. }
  1964. }else{
  1965. if( hs[0] <= hour && hour <= hs[1] )return true;
  1966. }
  1967. return nearly ? o2.Calendar.findClosest( hs.flatten(), hour ) : false;
  1968. },
  1969. getEnableMinutes: function(date, h){
  1970. var fun = this.options.enableMinutes;
  1971. if( fun && typeOf(fun) === "function" ){
  1972. var d = typeOf( date ) === "string" ? new Date(date) : date;
  1973. return fun( d, h );
  1974. }
  1975. return [0, 59];
  1976. },
  1977. getDisabledMinutes: function(date, h){
  1978. var ar = this.getEnableMinutes(date, h);
  1979. if( !ar || !ar.length || (ar[0] === 0 && ar[1] === 59))return [];
  1980. if( typeOf(ar[0]) !== "array" )ar = [ar];
  1981. return o2.Calendar.RangeArrayUtils.complementary([0, 59], ar, null, 1);
  1982. },
  1983. isEnableMinute: function (thisDate, hour, minute, nearly) {
  1984. var ms = this.getEnableMinutes( thisDate, hour );
  1985. if( !ms || !ms.length || (ms[0] === 0 && ms[1] === 59))return true;
  1986. if( typeOf(ms[0]) === "array" ){
  1987. for( var i=0; i< ms.length; i++ ){
  1988. var dms = ms[i];
  1989. if( dms[0] <= minute && minute <= dms[1] )return true;
  1990. }
  1991. }else{
  1992. if( ms[0] <= minute && minute <= ms[1] )return true;
  1993. }
  1994. return nearly ? o2.Calendar.findClosest( ms.flatten(), minute ) : false;
  1995. },
  1996. getEnableSeconds: function(date, h, m){
  1997. var fun = this.options.enableSeconds;
  1998. if( fun && typeOf(fun) === "function" ){
  1999. var d = typeOf( date ) === "string" ? new Date(date) : date;
  2000. return fun( d, h, m );
  2001. }
  2002. return [0, 59];
  2003. },
  2004. getDisabledSeconds: function(date, h, m){
  2005. var ar = this.getEnableSeconds(date, h, m);
  2006. if( !ar || !ar.length || (ar[0] === 0 && ar[1] === 59))return [];
  2007. if( typeOf(ar[0]) !== "array" )ar = [ar];
  2008. return o2.Calendar.RangeArrayUtils.complementary([0, 59], ar, null, 1);
  2009. },
  2010. isEnableSecond: function (thisDate, hour, minute, second, nearly) {
  2011. var ss = this.getEnableSeconds( thisDate, hour, minute );
  2012. if( !ss || !ss.length || (ss[0] === 0 && ss[1] === 59))return true;
  2013. if( typeOf(ss[0]) === "array" ){
  2014. for( var i=0; i< ss.length; i++ ){
  2015. var dss = ss[i];
  2016. if( dss[0] <= second && second <= dss[1] )return true;
  2017. }
  2018. }else{
  2019. if( ss[0] <= second && second <= ss[1] )return true;
  2020. }
  2021. return nearly ? o2.Calendar.findClosest( ss.flatten(), second ) : false;
  2022. },
  2023. // _getEnableStartEnd: function( ar, start, end ){
  2024. // if( !ar || !ar.length || (ar[0] === start && ar[1] === 23) )return [start, 23];
  2025. // var s, e;
  2026. // if( typeOf( ar[0] === "array" ) ){
  2027. // ar.each(function(a, i){
  2028. // s = i === 0 ? (a[0] || start ) : Math.min( s , a[0] || start );
  2029. // e = i === 0 ? (a[1] || end ) : Math.max( e, a[1] || end );
  2030. // })
  2031. // }else{
  2032. // s = ar[0] || start;
  2033. // e = ar[1] || end;
  2034. // }
  2035. // return [s, e];
  2036. // }
  2037. });
  2038. o2.Calendar.antiShake = function (fun, wait, bind){
  2039. var time = null;
  2040. return function (args){
  2041. if(time){
  2042. clearTimeout(time);
  2043. }
  2044. time = setTimeout(fun.call(bind, args), wait);
  2045. }
  2046. };
  2047. o2.Calendar.findClosest = function(arr, target) {
  2048. if (!arr.length) {
  2049. return null; // 如果数组为空,返回null
  2050. }
  2051. var closest = arr[0];
  2052. var minDiff = Math.abs(arr[0] - target);
  2053. for (var i = 1; i < arr.length; i++) {
  2054. var currentDiff = Math.abs(arr[i] - target);
  2055. if (currentDiff < minDiff) {
  2056. minDiff = currentDiff;
  2057. closest = arr[i];
  2058. }
  2059. }
  2060. return closest;
  2061. }
  2062. o2.Calendar.MobileSelect = new Class({
  2063. Implements: [Options, Events],
  2064. options: {
  2065. "lineHeight" : 40, //每个item的高度
  2066. "items": [],
  2067. "ratio" : 1, //滑动距离倍率
  2068. "currentItem" : 0
  2069. },
  2070. initialize: function (wheelNode, options) {
  2071. this.setOptions( options );
  2072. this.wheelNode = wheelNode;
  2073. this.sliderNode = wheelNode.getFirst();
  2074. this.options.itemIndex = this.options.items.indexOf(this.options.currentItem);
  2075. if( this.options.itemIndex < 0 )this.options.itemIndex = 0;
  2076. },
  2077. load : function(){
  2078. var _this = this;
  2079. this.curDistance = 0;
  2080. this.sliderNode.style.transform = "translate3d(0px, 80px, 0px)";
  2081. this.wheelNode.addEventListener('touchstart', function (event) {
  2082. _this.touch(event);
  2083. }, false);
  2084. this.wheelNode.addEventListener('touchend', function (event) {
  2085. _this.touch(event);
  2086. }, false);
  2087. this.wheelNode.addEventListener('touchmove', function (event) {
  2088. _this.touch(event);
  2089. }, false);
  2090. this.locatePostion( this.options.itemIndex );
  2091. },
  2092. touch: function (ev) {
  2093. var sliderNode = this.sliderNode;
  2094. ev = ev || window.event;
  2095. switch (ev.type) {
  2096. case "touchstart":
  2097. this.startTime = new Date();
  2098. this.startY = event.touches[0].clientY;
  2099. this.oldMoveY = this.startY;
  2100. break;
  2101. case "touchend":
  2102. this.moveEndY = event.changedTouches[0].clientY;
  2103. this.overTime = new Date();
  2104. var speed = (this.moveEndY - this.startY) / ( this.overTime - this.startTime );
  2105. var ratio = 1;
  2106. if( Math.abs(speed) > 0.7 ){
  2107. ratio = 5;
  2108. }else if( Math.abs(speed) < 0.2 ){
  2109. ratio = 0.7
  2110. }
  2111. this.offsetSum = ( this.moveEndY - this.startY ) * this.options.ratio * ratio;
  2112. this.updateCurDistance();
  2113. this.curDistance = this.fixPosition(this.curDistance);
  2114. this.movePosition( this.curDistance );
  2115. this.oversizeBorder = - ( this.options.items.length - 3) * this.options.lineHeight;
  2116. if (this.curDistance + this.offsetSum > 2 * this.options.lineHeight) {
  2117. this.curDistance = 2 * this.options.lineHeight;
  2118. setTimeout(function () {
  2119. this.movePosition( this.curDistance );
  2120. }.bind(this), 100);
  2121. } else if (this.curDistance + this.offsetSum < this.oversizeBorder) {
  2122. this.curDistance = this.oversizeBorder;
  2123. setTimeout(function () {
  2124. this.movePosition( this.curDistance );
  2125. }.bind(this), 100);
  2126. }
  2127. var idx = this.getCurIndex();
  2128. this.fireEvent( "change", [this.options.items[idx]]);
  2129. break;
  2130. case "touchmove":
  2131. ev.preventDefault();
  2132. this.moveY = event.touches[0].clientY;
  2133. this.overTime = new Date();
  2134. var speed = (this.moveY - this.oldMoveY) / ( this.overTime - this.oldOverTime );
  2135. var ratio = 1;
  2136. if( Math.abs(speed) > 0.7 ){
  2137. ratio = 5;
  2138. }else if( Math.abs(speed) < 0.2 ){
  2139. ratio = 0.7
  2140. }
  2141. this.offset = ( this.moveY - this.oldMoveY ) * this.options.ratio * ratio;
  2142. this.updateCurDistance();
  2143. this.curDistance = this.curDistance + this.offset;
  2144. this.movePosition( this.curDistance );
  2145. this.oldMoveY = this.moveY;
  2146. this.oldOverTime = this.overTime;
  2147. break;
  2148. }
  2149. },
  2150. resetItems: function( items, currentItem ){
  2151. this.options.items = items;
  2152. this.options.currentItem = currentItem;
  2153. this.options.itemIndex = this.options.items.indexOf(this.options.currentItem);
  2154. if( this.options.itemIndex < 0 )this.options.itemIndex = 0;
  2155. this.oversizeBorder = - ( this.options.items.length - 3) * this.options.lineHeight;
  2156. this.locatePostion( this.options.itemIndex );
  2157. },
  2158. calcDistance: function (index) {
  2159. return 2 * this.options.lineHeight - index * this.options.lineHeight;
  2160. },
  2161. setCurDistance: function ( index ) {
  2162. this.curDistance = this.calcDistance( index );
  2163. this.movePosition( this.curDistance );
  2164. },
  2165. fixPosition: function (distance) {
  2166. return -(this.getIndex(distance) - 2) * this.options.lineHeight;
  2167. },
  2168. getCurIndex : function(){
  2169. return this.getIndex( this.curDistance );
  2170. },
  2171. getIndex: function (distance) {
  2172. return Math.round((2 * this.options.lineHeight - distance) / this.options.lineHeight);
  2173. },
  2174. movePosition: function ( distance) {
  2175. this.sliderNode.style.webkitTransform = 'translate3d(0,' + distance + 'px, 0)';
  2176. this.sliderNode.style.transform = 'translate3d(0,' + distance + 'px, 0)';
  2177. },
  2178. locatePostion: function ( index ) {
  2179. this.curDistance = this.calcDistance(index);
  2180. this.movePosition( this.curDistance );
  2181. },
  2182. updateCurDistance: function () {
  2183. this.curDistance = parseInt(this.sliderNode.style.transform.split(',')[1]);
  2184. },
  2185. getDistance: function () {
  2186. return parseInt(this.sliderNode.style.transform.split(',')[1]);
  2187. }
  2188. });
  2189. o2.Calendar.RangeArrayUtils = {
  2190. //补集 range [ start, end ] rangeList [ [start1, end1], [ start2, end2 ] ... ]
  2191. complementary : function( range, rangeList, type, offset ){
  2192. if( !range )return range;
  2193. var r = this.getRangeObject( range );
  2194. if( !rangeList || rangeList.length == 0 )return this.parse( [r] , type);
  2195. var unitedList = this.union( rangeList );
  2196. var newRange = {};
  2197. if( unitedList[0][0] > r.start ){
  2198. newRange.start = r.start;
  2199. }else if( r.end > unitedList[0][1] ){
  2200. newRange.start = unitedList[0][1];
  2201. unitedList.shift();
  2202. }else{
  2203. return [];
  2204. }
  2205. var newList = [];
  2206. while( unitedList.length > 0 ){
  2207. if( unitedList[0][0] >= r.end ){
  2208. newRange.end = r.end;
  2209. newList.push( Object.clone(newRange) );
  2210. return this.parse( newList , type);
  2211. }else if( r.end <= unitedList[0][1] ){
  2212. newRange.end = unitedList[0][0];
  2213. newList.push( Object.clone(newRange) );
  2214. return this.parse( newList, type );
  2215. }else{
  2216. newRange.end = unitedList[0][0];
  2217. newList.push( Object.clone(newRange) );
  2218. newRange.start = unitedList[0][1];
  2219. unitedList.shift();
  2220. }
  2221. }
  2222. newRange.end = r.end;
  2223. newList.push( Object.clone(newRange ));
  2224. var array = this.parse( newList, type );
  2225. if( offset ){
  2226. return array.map(function (a) {
  2227. if( a[0] !== r.start )a[0] = a[0] + offset;
  2228. if( a[1] !== r.end )a[1] = a[1] - offset;
  2229. return a;
  2230. })
  2231. }else{
  2232. return array;
  2233. }
  2234. },
  2235. //取区域并集rangeList [ [start1, end1], [ start2, end2 ] ... ]
  2236. union : function( ranges, type ){
  2237. if( !ranges || ranges.length == 0)return ranges; //this.parse(this.getRangeObject( ranges ) ) ;
  2238. var rangeList = Array.clone( ranges );
  2239. for( var i=0; i<rangeList.length; i++ ){
  2240. rangeList[i] = this.getRangeObject( rangeList[i] );
  2241. }
  2242. rangeList.sort( function( a, b ){
  2243. return a.start - b.start;
  2244. });
  2245. var newRangeList = [];
  2246. var newRange = rangeList.shift();
  2247. while( rangeList.length > 0 ){
  2248. var nextRange = rangeList.shift();
  2249. if( this.isIntersection( newRange, nextRange ) ){
  2250. newRange.end = Math.max( newRange.end, nextRange.end );
  2251. }else{
  2252. newRangeList.push( Object.clone( newRange ) );
  2253. newRange = nextRange;
  2254. }
  2255. }
  2256. if( !nextRange ){
  2257. newRangeList.push( Object.clone( newRange ) );
  2258. }else if( this.isIntersection( newRange, nextRange ) ){
  2259. newRange.end = Math.max( newRange.end, nextRange.end );
  2260. newRangeList.push( Object.clone( newRange ) );
  2261. }else{
  2262. newRangeList.push( Object.clone( nextRange ) );
  2263. }
  2264. return this.parse( newRangeList, type );
  2265. },
  2266. //取区域交集rangeList [ [start1, end1], [ start2, end2 ] ... ],需要测试
  2267. intersection: function( ranges, type ){
  2268. if( !ranges || ranges.length == 0 )return ranges; //this.parse(this.getRangeObject( ranges ) ) ;
  2269. if( ranges.length === 1 )return ranges[1];
  2270. var rangeList = Array.clone( ranges );
  2271. for( var i=0; i<rangeList.length; i++ ){
  2272. rangeList[i] = this.getRangeObject( rangeList[i] );
  2273. }
  2274. rangeList.sort( function( a, b ){
  2275. return a.start - b.start;
  2276. });
  2277. var newRange = rangeList.shift();
  2278. while( rangeList.length > 0 ){
  2279. var nextRange = rangeList.shift();
  2280. if( this.isIntersection( newRange, nextRange ) ){
  2281. newRange.start = nextRange.start;
  2282. newRange.end = Math.min( newRange.end, nextRange.end );
  2283. }else{
  2284. return [];
  2285. }
  2286. }
  2287. if( type && type === "date" ){
  2288. return [ Date.parse(newRange.start), Date.parse(newRange.end) ];
  2289. }else{
  2290. return [newRange.start, newRange.end];
  2291. }
  2292. },
  2293. //区域是否相交
  2294. isIntersection : function( range1, range2 ){
  2295. var r1 = typeOf( range1 ) === "object" ? range1 : this.getRangeObject( range1 );
  2296. var r2 = typeOf( range2 ) === "object" ? range2 : this.getRangeObject( range2 );
  2297. if( r1.start > r2.end )return false;
  2298. if( r2.start > r1.end )return false;
  2299. return true;
  2300. },
  2301. parse: function( objectList, type ){
  2302. var list = [];
  2303. for( var i=0; i<objectList.length; i++ ){
  2304. var range = objectList[i];
  2305. if( type && type == "date" ){
  2306. list.push( [ Date.parse(range.start), Date.parse(range.end) ] );
  2307. }else{
  2308. list.push( [range.start, range.end] );
  2309. }
  2310. }
  2311. return list;
  2312. },
  2313. getRangeObject: function( range ){
  2314. return {
  2315. start : Math.min( range[0], range[1] ),
  2316. end : Math.max( range[0], range[1] )
  2317. }
  2318. }
  2319. };