Calendar.js 10 KB

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