Calendar.js 11 KB

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