Calendar.js 9.2 KB

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