Calendar.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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"]
  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. getValue: function(isDate){
  27. var value = this._getBusinessData();
  28. if( value && !isDate)return value;
  29. if (!value) value = this._computeValue();
  30. var d = (!!value) ? Date.parse(value) : "";
  31. if (isDate){
  32. return d || null;
  33. }else{
  34. //if (d) value = Date.parse(value).format(this.json.format);
  35. return (d) ? d.format(this.json.format) : "";
  36. }
  37. },
  38. getValueStr : function(){
  39. var value = this._getBusinessData();
  40. if (!value) value = this._computeValue();
  41. return value;
  42. },
  43. clickSelect: function(){
  44. var _self = this;
  45. if (!this.calendar){
  46. MWF.require("MWF.widget.Calendar", function(){
  47. var options = {
  48. "style": layout.mobile ? "xform_mobile" : "xform",
  49. "secondEnable" : this.json.isSelectSecond,
  50. "isTime": (this.json.selectType==="datetime" || this.json.selectType==="time"),
  51. "timeOnly": (this.json.selectType === "time"),
  52. //"target": this.form.node,
  53. "target": this.form.app.content,
  54. "format": this.json.format,
  55. "onComplate": function(formateDate, date){
  56. this.validationMode();
  57. if(this.validation())this._setBusinessData(this.getInputData("change"));
  58. this.fireEvent("complete");
  59. }.bind(this),
  60. "onChange": function(){
  61. this.fireEvent("change");
  62. }.bind(this),
  63. "onClear": function(){
  64. this.validationMode();
  65. if(this.validation())this._setBusinessData(this.getInputData("change"));
  66. this.fireEvent("clear");
  67. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  68. }.bind(this),
  69. "onShow": function(){
  70. if (_self.descriptionNode) _self.descriptionNode.setStyle("display", "none");
  71. if( layout.mobile ){
  72. this.container.position({
  73. relativeTo: $(document.body),
  74. position: 'leftCenter',
  75. edge: 'leftCenter'
  76. //offset : { y : -25 }
  77. });
  78. }
  79. },
  80. "onHide": function(){
  81. if (!this.node.getFirst().get("value")) if (this.descriptionNode) this.descriptionNode.setStyle("display", "block");
  82. }.bind(this)
  83. };
  84. options.baseDate = this.getBaseDate();
  85. this.calendar = new MWF.widget.Calendar(this.node.getFirst(), options);
  86. this.calendar.show();
  87. }.bind(this));
  88. }else{
  89. var options = {};
  90. options.baseDate = this.getBaseDate();
  91. this.calendar.setOptions(options);
  92. //this.calendar.show();
  93. this.node.getFirst().focus();
  94. }
  95. },
  96. getBaseDate : function(){
  97. var d;
  98. var value = this.getValue(true);
  99. if( value && value.getTime() > 10000 ){
  100. d = value;
  101. }else{
  102. var ud = Date.parse( this.unformatDate( this.getValueStr() ) );
  103. if( ud && ud.getTime() > 10000 ){
  104. d = ud;
  105. }else{
  106. d = new Date();
  107. }
  108. }
  109. return d;
  110. },
  111. unformatDate : function( dateStr ){
  112. var formatStr = this.json.format;
  113. var matchArr = [ "%Y", "%m", "%d", "%H", "%M", "%S", "%z", "%Z" ];
  114. var lengthArr = [ 4, 2, 2, 2, 2, 2, 5, 3];
  115. 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") ];
  116. var resultArr = [ null, null, null, null, null, null, null, null ];
  117. for( var i=0; i<matchArr.length; i++ ){
  118. if( indexArr[i] === -1 )continue;
  119. var leftLength = 0;
  120. var leftUnitLength = 0;
  121. Array.each( indexArr, function( n, k ){
  122. if( n === -1 )return;
  123. if( indexArr[i] > n ){
  124. leftLength += lengthArr[k];
  125. leftUnitLength += matchArr[k].length;
  126. }
  127. });
  128. resultArr[i] = dateStr.substr( indexArr[i] - leftUnitLength + leftLength, lengthArr[i] );
  129. }
  130. var now = new Date();
  131. for( var i=0; i < resultArr.length; i++ ){
  132. if( !resultArr[i] ){
  133. switch ( matchArr[i] ){
  134. case "%Y":
  135. case "%m":
  136. case "%d":
  137. resultArr[i] = now.format( matchArr[i] );
  138. break;
  139. case "%H":
  140. case "%M":
  141. case "%S":
  142. resultArr[i] = "00";
  143. break;
  144. case "%z":
  145. case "%Z":
  146. default:
  147. break;
  148. }
  149. }
  150. }
  151. return resultArr[0] + "-" + resultArr[1] + "-" + resultArr[2] + " " + resultArr[3]+":"+resultArr[4]+":"+resultArr[5];
  152. }
  153. });