Calendar.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. _getValueAg: function(value,isDate){
  27. if (value && value.isAG){
  28. return value.then(function(v){
  29. this._getValueAg(v, isDate);
  30. }.bind(this));
  31. }else{
  32. var d = (!!value) ? Date.parse(value) : "";
  33. if (isDate){
  34. return d || null;
  35. }else{
  36. return (d) ? d.format(this.json.format) : "";
  37. }
  38. }
  39. },
  40. getValue: function(isDate){
  41. if (this.moduleValueAG) return this.moduleValueAG;
  42. var value = this._getBusinessData();
  43. if( value && !isDate)return value;
  44. if (!value) value = this._computeValue();
  45. if (value.isAG) return value;
  46. var d = (!!value) ? Date.parse(value) : "";
  47. if (isDate){
  48. return d || null;
  49. }else{
  50. //if (d) value = Date.parse(value).format(this.json.format);
  51. return (d) ? d.format(this.json.format) : "";
  52. }
  53. return value || "";
  54. },
  55. getValueStr : function(){
  56. var value = this._getBusinessData();
  57. if (!value) value = this._computeValue();
  58. return value;
  59. },
  60. __setValue: function(value){
  61. var d = Date.parse(value);
  62. var v = d.format(this.json.format);
  63. this._setBusinessData(value);
  64. if (this.node.getFirst()) this.node.getFirst().set("value", v || "");
  65. if (this.readonly || this.json.isReadonly) this.node.set("text", v);
  66. this.moduleValueAG = null;
  67. return value;
  68. },
  69. clickSelect: function(){
  70. debugger;
  71. var _self = this;
  72. if (!this.calendar){
  73. MWF.require("MWF.widget.Calendar", function(){
  74. var options = {
  75. "style": layout.mobile ? "xform_mobile" : "xform",
  76. "secondEnable" : this.json.isSelectSecond,
  77. "isTime": (this.json.selectType==="datetime" || this.json.selectType==="time"),
  78. "timeOnly": (this.json.selectType === "time"),
  79. //"target": this.form.node,
  80. "target": this.form.app.content,
  81. "format": this.json.format,
  82. "onComplate": function(formateDate, date){
  83. this.validationMode();
  84. if(this.validation())this._setBusinessData(this.getInputData("change"));
  85. this.fireEvent("complete");
  86. }.bind(this),
  87. "onChange": function(){
  88. this.fireEvent("change");
  89. }.bind(this),
  90. "onClear": function(){
  91. this.validationMode();
  92. if(this.validation())this._setBusinessData(this.getInputData("change"));
  93. this.fireEvent("clear");
  94. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  95. }.bind(this),
  96. "onShow": function(){
  97. if (_self.descriptionNode) _self.descriptionNode.setStyle("display", "none");
  98. if( layout.mobile ){
  99. this.container.position({
  100. relativeTo: $(document.body),
  101. position: 'leftCenter',
  102. edge: 'leftCenter'
  103. //offset : { y : -25 }
  104. });
  105. }else{
  106. var parent = _self.node.getParent();
  107. while( parent ){
  108. var overflow = parent.getStyle("overflow");
  109. var overflowY = parent.getStyle("overflow-y");
  110. if( overflow === "auto" || overflow === "scroll" || overflowY === "auto" || overflowY === "scroll" ){
  111. _self.scrollFun = function( e ){
  112. if (this.container.position ) {
  113. this.container.position({
  114. relativeTo: this.node,
  115. position: 'bottomLeft',
  116. edge: 'upperLeft',
  117. allowNegative : true
  118. });
  119. }
  120. }.bind(this);
  121. _self.scrollParentNode = parent;
  122. parent.addEvent( "scroll", _self.scrollFun );
  123. parent = null;
  124. }else{
  125. parent = parent.getParent();
  126. }
  127. }
  128. }
  129. _self.fireEvent("show");
  130. },
  131. "onHide": function(){
  132. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  133. if( _self.scrollParentNode && _self.scrollFun ){
  134. _self.scrollParentNode.removeEvent("scroll", _self.scrollFun);
  135. }
  136. _self.fireEvent("hide");
  137. }.bind(this)
  138. };
  139. options.baseDate = this.getBaseDate();
  140. this.calendar = new MWF.widget.Calendar(this.node.getFirst(), options);
  141. if( this.form.json && this.form.json.canlendarStyle && typeOf( this.form.json.canlendarStyle.zIndex ) !== "null" && typeOf( this.form.json.canlendarStyle.zIndex ) !== "undefined" ){
  142. this.calendar.container.setStyle("z-index", this.form.json.canlendarStyle.zIndex );
  143. }
  144. this.calendar.show();
  145. }.bind(this));
  146. }else{
  147. var options = {};
  148. options.baseDate = this.getBaseDate();
  149. this.calendar.setOptions(options);
  150. //this.calendar.show();
  151. this.node.getFirst().focus();
  152. }
  153. },
  154. getBaseDate : function(){
  155. var d;
  156. var value = this.getValue(true);
  157. if( value && value.getTime() > 10000 ){
  158. d = value;
  159. }else{
  160. var ud = Date.parse( this.unformatDate( this.getValueStr() ) );
  161. if( ud && ud.getTime() > 10000 ){
  162. d = ud;
  163. }else{
  164. d = new Date();
  165. }
  166. }
  167. return d;
  168. },
  169. unformatDate : function( dateStr ){
  170. var formatStr = this.json.format;
  171. var matchArr = [ "%Y", "%m", "%d", "%H", "%M", "%S", "%z", "%Z" ];
  172. var lengthArr = [ 4, 2, 2, 2, 2, 2, 5, 3];
  173. 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") ];
  174. var resultArr = [ null, null, null, null, null, null, null, null ];
  175. for( var i=0; i<matchArr.length; i++ ){
  176. if( indexArr[i] === -1 )continue;
  177. var leftLength = 0;
  178. var leftUnitLength = 0;
  179. Array.each( indexArr, function( n, k ){
  180. if( n === -1 )return;
  181. if( indexArr[i] > n ){
  182. leftLength += lengthArr[k];
  183. leftUnitLength += matchArr[k].length;
  184. }
  185. });
  186. resultArr[i] = dateStr.substr( indexArr[i] - leftUnitLength + leftLength, lengthArr[i] );
  187. }
  188. var now = new Date();
  189. for( var i=0; i < resultArr.length; i++ ){
  190. if( !resultArr[i] ){
  191. switch ( matchArr[i] ){
  192. case "%Y":
  193. case "%m":
  194. case "%d":
  195. resultArr[i] = now.format( matchArr[i] );
  196. break;
  197. case "%H":
  198. case "%M":
  199. case "%S":
  200. resultArr[i] = "00";
  201. break;
  202. case "%z":
  203. case "%Z":
  204. default:
  205. break;
  206. }
  207. }
  208. }
  209. return resultArr[0] + "-" + resultArr[1] + "-" + resultArr[2] + " " + resultArr[3]+":"+resultArr[4]+":"+resultArr[5];
  210. }
  211. });