Calendar.js 10 KB

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