Calendar.js 8.5 KB

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