Calendar.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. MWF.xDesktop.requireApp("process.Xform", "$Input", null, false);
  2. /** @class process.Calendar 日期组件。
  3. * @example
  4. * //可以在脚本中获取该组件
  5. * //方法1:
  6. * var field = this.form.get("fieldName"); //获取组件对象
  7. * //方法2
  8. * var field = this.target; //在组件本身的脚本中获取,比如事件脚本、默认值脚本、校验脚本等等
  9. * @extends MWF.xApplication.process.Xform.$Input
  10. * @hideconstructor
  11. */
  12. MWF.xApplication.process.Xform.Calendar = MWF.APPCalendar = new Class(
  13. /** @lends MWF.xApplication.process.Xform.Calendar# */
  14. {
  15. Implements: [Events],
  16. Extends: MWF.APP$Input,
  17. iconStyle: "calendarIcon",
  18. options: {
  19. "moduleEvents": ["queryLoad","postLoad","load","complete", "clear", "change","show","hide"]
  20. },
  21. _loadNode: function(){
  22. if (this.readonly || this.json.isReadonly){
  23. this._loadNodeRead();
  24. }else{
  25. this._loadNodeEdit();
  26. }
  27. },
  28. setDescriptionEvent: function(){
  29. if (this.descriptionNode){
  30. this.descriptionNode.addEvents({
  31. "mousedown": function(){
  32. this.descriptionNode.setStyle("display", "none");
  33. //this.clickSelect();
  34. }.bind(this)
  35. });
  36. }
  37. },
  38. _getValueAg: function(value,isDate){
  39. if (value && value.isAG){
  40. return value.then(function(v){
  41. this._getValueAg(v, isDate);
  42. }.bind(this), function(){});
  43. }else{
  44. var d = (!!value) ? Date.parse(value) : "";
  45. if (isDate){
  46. return d || null;
  47. }else{
  48. return (d) ? d.format(this.json.format) : "";
  49. }
  50. }
  51. },
  52. getValue: function(isDate){
  53. if (this.moduleValueAG) return this.moduleValueAG;
  54. var value = this._getBusinessData();
  55. if( value && !isDate)return value;
  56. if (!value) value = this._computeValue();
  57. if (value.then) return value;
  58. var d = (!!value) ? Date.parse(value) : "";
  59. if (isDate){
  60. return d || null;
  61. }else{
  62. //if (d) value = Date.parse(value).format(this.json.format);
  63. return (d) ? d.format(this.json.format) : "";
  64. }
  65. return value || "";
  66. },
  67. getValueStr : function(){
  68. var value = this._getBusinessData();
  69. if (!value) value = this._computeValue();
  70. return value;
  71. },
  72. __setValue: function(value){
  73. var v = (value) ? ( Date.parse(value)).format(this.json.format) : "";
  74. this._setBusinessData(value);
  75. if (this.node.getFirst()) this.node.getFirst().set("value", v || "");
  76. if (this.readonly || this.json.isReadonly) this.node.set("text", v);
  77. this.moduleValueAG = null;
  78. return value;
  79. },
  80. clickSelect: function(){
  81. var _self = this;
  82. if (!this.calendar){
  83. MWF.require("MWF.widget.Calendar", function(){
  84. var options = {
  85. "style": layout.mobile ? "xform_mobile" : "xform",
  86. "secondEnable" : this.json.isSelectSecond,
  87. "isTime": (this.json.selectType==="datetime" || this.json.selectType==="time"),
  88. "timeOnly": (this.json.selectType === "time"),
  89. //"target": this.form.node,
  90. "target": layout.mobile ? $(document.body) : this.form.app.content,
  91. "format": this.json.format,
  92. "onComplate": function(formateDate, date){
  93. this.validationMode();
  94. if(this.validation())this._setBusinessData(this.getInputData("change"));
  95. this.fireEvent("complete");
  96. }.bind(this),
  97. "onChange": function(){
  98. this.fireEvent("change");
  99. }.bind(this),
  100. "onClear": function(){
  101. this.validationMode();
  102. if(this.validation())this._setBusinessData(this.getInputData("change"));
  103. this.fireEvent("clear");
  104. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  105. }.bind(this),
  106. "onShow": function(){
  107. if (_self.descriptionNode) _self.descriptionNode.setStyle("display", "none");
  108. if( layout.mobile ){
  109. this.container.position({
  110. relativeTo: $(document.body),
  111. position: 'leftCenter',
  112. edge: 'leftCenter'
  113. //offset : { y : -25 }
  114. });
  115. }else{
  116. var parent = _self.node.getParent();
  117. while( parent ){
  118. var overflow = parent.getStyle("overflow");
  119. var overflowY = parent.getStyle("overflow-y");
  120. if( overflow === "auto" || overflow === "scroll" || overflowY === "auto" || overflowY === "scroll" ){
  121. _self.scrollFun = function( e ){
  122. if (this.container.position ) {
  123. this.container.position({
  124. relativeTo: this.node,
  125. position: 'bottomLeft',
  126. edge: 'upperLeft',
  127. allowNegative : true
  128. });
  129. }
  130. }.bind(this);
  131. _self.scrollParentNode = parent;
  132. parent.addEvent( "scroll", _self.scrollFun );
  133. parent = null;
  134. }else{
  135. parent = parent.getParent();
  136. }
  137. }
  138. }
  139. _self.fireEvent("show");
  140. },
  141. "onHide": function(){
  142. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  143. if( _self.scrollParentNode && _self.scrollFun ){
  144. _self.scrollParentNode.removeEvent("scroll", _self.scrollFun);
  145. }
  146. _self.fireEvent("hide");
  147. }.bind(this)
  148. };
  149. options.baseDate = this.getBaseDate();
  150. /**
  151. * 日期弹出选择组件,只读情况下无此节点.
  152. * @member {MWF.widget.Calendar}
  153. * @example
  154. * var calendar = this.form.get("fieldName").calendar; //获取组件
  155. * if(calendar)calendar.show(); //弹出选择组件
  156. */
  157. this.calendar = new MWF.widget.Calendar(this.node.getFirst(), options);
  158. if( this.form.json && this.form.json.canlendarStyle && typeOf( this.form.json.canlendarStyle.zIndex ) !== "null" && typeOf( this.form.json.canlendarStyle.zIndex ) !== "undefined" ){
  159. this.calendar.container.setStyle("z-index", this.form.json.canlendarStyle.zIndex );
  160. }
  161. this.calendar.show();
  162. }.bind(this));
  163. }else{
  164. var options = {};
  165. options.baseDate = this.getBaseDate();
  166. this.calendar.setOptions(options);
  167. //this.calendar.show();
  168. this.node.getFirst().focus();
  169. }
  170. },
  171. getBaseDate : function(){
  172. var d;
  173. var value = this.getValue(true);
  174. if( value && value.getTime() > 10000 ){
  175. d = value;
  176. }else{
  177. var ud = Date.parse( this.unformatDate( this.getValueStr() ) );
  178. if( ud && ud.getTime() > 10000 ){
  179. d = ud;
  180. }else{
  181. d = new Date();
  182. }
  183. }
  184. return d;
  185. },
  186. unformatDate : function( dateStr ){
  187. var formatStr = this.json.format;
  188. var matchArr = [ "%Y", "%m", "%d", "%H", "%M", "%S", "%z", "%Z" ];
  189. var lengthArr = [ 4, 2, 2, 2, 2, 2, 5, 3];
  190. 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") ];
  191. var resultArr = [ null, null, null, null, null, null, null, null ];
  192. for( var i=0; i<matchArr.length; i++ ){
  193. if( indexArr[i] === -1 )continue;
  194. var leftLength = 0;
  195. var leftUnitLength = 0;
  196. Array.each( indexArr, function( n, k ){
  197. if( n === -1 )return;
  198. if( indexArr[i] > n ){
  199. leftLength += lengthArr[k];
  200. leftUnitLength += matchArr[k].length;
  201. }
  202. });
  203. resultArr[i] = dateStr.substr( indexArr[i] - leftUnitLength + leftLength, lengthArr[i] );
  204. }
  205. var now = new Date();
  206. for( var i=0; i < resultArr.length; i++ ){
  207. if( !resultArr[i] ){
  208. switch ( matchArr[i] ){
  209. case "%Y":
  210. case "%m":
  211. case "%d":
  212. resultArr[i] = now.format( matchArr[i] );
  213. break;
  214. case "%H":
  215. case "%M":
  216. case "%S":
  217. resultArr[i] = "00";
  218. break;
  219. case "%z":
  220. case "%Z":
  221. default:
  222. break;
  223. }
  224. }
  225. }
  226. return resultArr[0] + "-" + resultArr[1] + "-" + resultArr[2] + " " + resultArr[3]+":"+resultArr[4]+":"+resultArr[5];
  227. }
  228. });