Calendar.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. MWF.xApplication.process.Xform.Calendar = MWF.APPCalendar = new Class({
  3. Implements: [Events],
  4. Extends: MWF.APP$Input,
  5. iconStyle: "calendarIcon",
  6. options: {
  7. "moduleEvents": ["queryLoad","postLoad","load","complete", "clear", "change","show","hide"]
  8. },
  9. _loadNode: function(){
  10. if (this.readonly || this.json.isReadonly){
  11. this._loadNodeRead();
  12. }else{
  13. this._loadNodeEdit();
  14. }
  15. },
  16. setDescriptionEvent: function(){
  17. if (this.descriptionNode){
  18. this.descriptionNode.addEvents({
  19. "mousedown": function(){
  20. this.descriptionNode.setStyle("display", "none");
  21. //this.clickSelect();
  22. }.bind(this)
  23. });
  24. }
  25. },
  26. getValue: function(isDate){
  27. var value = this._getBusinessData();
  28. if( value && !isDate)return value;
  29. if (!value) value = this._computeValue();
  30. var d = (!!value) ? Date.parse(value) : "";
  31. if (isDate){
  32. return d || null;
  33. }else{
  34. //if (d) value = Date.parse(value).format(this.json.format);
  35. return (d) ? d.format(this.json.format) : "";
  36. }
  37. },
  38. getValueStr : function(){
  39. var value = this._getBusinessData();
  40. if (!value) value = this._computeValue();
  41. return value;
  42. },
  43. clickSelect: function(){
  44. debugger;
  45. var _self = this;
  46. if (!this.calendar){
  47. MWF.require("MWF.widget.Calendar", function(){
  48. var options = {
  49. "style": layout.mobile ? "xform_mobile" : "xform",
  50. "secondEnable" : this.json.isSelectSecond,
  51. "isTime": (this.json.selectType==="datetime" || this.json.selectType==="time"),
  52. "timeOnly": (this.json.selectType === "time"),
  53. //"target": this.form.node,
  54. "target": this.form.app.content,
  55. "format": this.json.format,
  56. "onComplate": function(formateDate, date){
  57. this.validationMode();
  58. if(this.validation())this._setBusinessData(this.getInputData("change"));
  59. this.fireEvent("complete");
  60. }.bind(this),
  61. "onChange": function(){
  62. this.fireEvent("change");
  63. }.bind(this),
  64. "onClear": function(){
  65. this.validationMode();
  66. if(this.validation())this._setBusinessData(this.getInputData("change"));
  67. this.fireEvent("clear");
  68. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  69. }.bind(this),
  70. "onShow": function(){
  71. if (_self.descriptionNode) _self.descriptionNode.setStyle("display", "none");
  72. if( layout.mobile ){
  73. this.container.position({
  74. relativeTo: $(document.body),
  75. position: 'leftCenter',
  76. edge: 'leftCenter'
  77. //offset : { y : -25 }
  78. });
  79. }else{
  80. var parent = _self.node.getParent();
  81. while( parent ){
  82. var overflow = parent.getStyle("overflow");
  83. var overflowY = parent.getStyle("overflow-y");
  84. if( overflow === "auto" || overflow === "scroll" || overflowY === "auto" || overflowY === "scroll" ){
  85. _self.scrollFun = function( e ){
  86. if (this.container.position ) {
  87. this.container.position({
  88. relativeTo: this.node,
  89. position: 'bottomLeft',
  90. edge: 'upperLeft',
  91. allowNegative : true
  92. });
  93. }
  94. }.bind(this);
  95. _self.scrollParentNode = parent;
  96. parent.addEvent( "scroll", _self.scrollFun );
  97. parent = null;
  98. }else{
  99. parent = parent.getParent();
  100. }
  101. }
  102. }
  103. _self.fireEvent("show");
  104. },
  105. "onHide": function(){
  106. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  107. if( _self.scrollParentNode && _self.scrollFun ){
  108. _self.scrollParentNode.removeEvent("scroll", _self.scrollFun);
  109. }
  110. _self.fireEvent("hide");
  111. }.bind(this)
  112. };
  113. options.baseDate = this.getBaseDate();
  114. this.calendar = new MWF.widget.Calendar(this.node.getFirst(), options);
  115. if( this.form.json && this.form.json.canlendarStyle && typeOf( this.form.json.canlendarStyle.zIndex ) !== "null" && typeOf( this.form.json.canlendarStyle.zIndex ) !== "undefined" ){
  116. this.calendar.container.setStyle("z-index", this.form.json.canlendarStyle.zIndex );
  117. }
  118. this.calendar.show();
  119. }.bind(this));
  120. }else{
  121. var options = {};
  122. options.baseDate = this.getBaseDate();
  123. this.calendar.setOptions(options);
  124. //this.calendar.show();
  125. this.node.getFirst().focus();
  126. }
  127. },
  128. getBaseDate : function(){
  129. var d;
  130. var value = this.getValue(true);
  131. if( value && value.getTime() > 10000 ){
  132. d = value;
  133. }else{
  134. var ud = Date.parse( this.unformatDate( this.getValueStr() ) );
  135. if( ud && ud.getTime() > 10000 ){
  136. d = ud;
  137. }else{
  138. d = new Date();
  139. }
  140. }
  141. return d;
  142. },
  143. unformatDate : function( dateStr ){
  144. var formatStr = this.json.format;
  145. var matchArr = [ "%Y", "%m", "%d", "%H", "%M", "%S", "%z", "%Z" ];
  146. var lengthArr = [ 4, 2, 2, 2, 2, 2, 5, 3];
  147. var indexArr = [ formatStr.indexOf("%Y"), formatStr.indexOf("%m"), formatStr.indexOf("%d"), formatStr.indexOf("%H"), formatStr.indexOf("%M"), formatStr.indexOf("%S"), formatStr.indexOf("%z"), formatStr.indexOf("%Z") ];
  148. var resultArr = [ null, null, null, null, null, null, null, null ];
  149. for( var i=0; i<matchArr.length; i++ ){
  150. if( indexArr[i] === -1 )continue;
  151. var leftLength = 0;
  152. var leftUnitLength = 0;
  153. Array.each( indexArr, function( n, k ){
  154. if( n === -1 )return;
  155. if( indexArr[i] > n ){
  156. leftLength += lengthArr[k];
  157. leftUnitLength += matchArr[k].length;
  158. }
  159. });
  160. resultArr[i] = dateStr.substr( indexArr[i] - leftUnitLength + leftLength, lengthArr[i] );
  161. }
  162. var now = new Date();
  163. for( var i=0; i < resultArr.length; i++ ){
  164. if( !resultArr[i] ){
  165. switch ( matchArr[i] ){
  166. case "%Y":
  167. case "%m":
  168. case "%d":
  169. resultArr[i] = now.format( matchArr[i] );
  170. break;
  171. case "%H":
  172. case "%M":
  173. case "%S":
  174. resultArr[i] = "00";
  175. break;
  176. case "%z":
  177. case "%Z":
  178. default:
  179. break;
  180. }
  181. }
  182. }
  183. return resultArr[0] + "-" + resultArr[1] + "-" + resultArr[2] + " " + resultArr[3]+":"+resultArr[4]+":"+resultArr[5];
  184. }
  185. });