Calendar.js 8.7 KB

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