HolidayExplorer.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. MWF.xDesktop.requireApp("Attendance", "Explorer", null, false);
  2. MWF.xDesktop.requireApp("Template", "MDomItem", null, false);
  3. MWF.xApplication.Attendance.HolidayExplorer = new Class({
  4. Extends: MWF.xApplication.Attendance.Explorer,
  5. Implements: [Options, Events],
  6. initialize: function(node, app, actions, options){
  7. this.setOptions(options);
  8. this.app = app;
  9. this.path = "/x_component_Attendance/$HolidayExplorer/";
  10. this.cssPath = "/x_component_Attendance/$HolidayExplorer/"+this.options.style+"/css.wcss";
  11. this._loadCss();
  12. this.actions = actions;
  13. this.node = $(node);
  14. this.initData();
  15. if (!this.personActions) this.personActions = new MWF.xAction.org.express.RestActions();
  16. },
  17. loadView : function(){
  18. this.view = new MWF.xApplication.Attendance.HolidayExplorer.View(this.elementContentNode, this.app,this, this.viewData, this.options.searchKey );
  19. this.view.load();
  20. this.setContentSize();
  21. },
  22. createDocument: function(){
  23. if(this.view)this.view._createDocument();
  24. }
  25. });
  26. MWF.xApplication.Attendance.HolidayExplorer.View = new Class({
  27. Extends: MWF.xApplication.Attendance.Explorer.View,
  28. _createItem: function(data){
  29. return new MWF.xApplication.Attendance.HolidayExplorer.Document(this.table, data, this.explorer, this);
  30. },
  31. _getCurrentPageData: function(callback, count){
  32. this.actions.listHolidayAll(function(json){
  33. if (callback) callback(json);
  34. });
  35. },
  36. _removeDocument: function(document, isNotice, callback){
  37. this.actions.listHolidayByYearAndName(document.configYear,document.configName, function( json ){
  38. json.data.each(function(d){
  39. this.actions.deleteHoliday(d.id, function(json){
  40. }.bind(this), null, false);
  41. }.bind(this))
  42. if(!isNotice)this.app.notice(this.app.lp.deleteDocumentOK, "success");
  43. if(callback)callback();
  44. this.explorer.reload();
  45. }.bind(this))
  46. },
  47. _createDocument: function(){
  48. var holiday = new MWF.xApplication.Attendance.HolidayExplorer.Holiday(this.explorer);
  49. holiday.create();
  50. },
  51. _openDocument: function( documentData ){
  52. this.actions.getHoliday( documentData.id, function( json ) {
  53. var data = json.data;
  54. var holidy = {
  55. "configYear" : data.configYear,
  56. "configName" : data.configName,
  57. "makeUpClassDay" : []
  58. }
  59. this.actions.listHolidayByYearAndName(data.configYear,data.configName, function( json ){
  60. var startDate, endDate;
  61. debugger;
  62. json.data.each(function( d ){
  63. if( d.configType == "Workday" ){
  64. holidy.makeUpClassDay.push( d.configDate )
  65. }else{
  66. if( !startDate ){
  67. startDate = d.configDate;
  68. }else if( new Date(startDate) > new Date(d.configDate) ){
  69. startDate = d.configDate;
  70. }
  71. if( !endDate ){
  72. endDate = d.configDate;
  73. }else if( new Date(endDate) < new Date(d.configDate) ){
  74. endDate = d.configDate;
  75. }
  76. }
  77. }.bind(this))
  78. holidy.makeUpClassDay = holidy.makeUpClassDay.join(",")
  79. holidy.startDate = startDate;
  80. holidy.endDate = endDate;
  81. var h = new MWF.xApplication.Attendance.HolidayExplorer.Holiday(this.explorer,holidy);
  82. h.edit();
  83. }.bind(this))
  84. }.bind(this))
  85. }
  86. })
  87. MWF.xApplication.Attendance.HolidayExplorer.Document = new Class({
  88. Extends: MWF.xApplication.Attendance.Explorer.Document
  89. })
  90. MWF.xApplication.Attendance.HolidayExplorer.Holiday = new Class({
  91. Extends: MWF.widget.Common,
  92. options: {
  93. "width": "500",
  94. "height": "400"
  95. },
  96. initialize: function( explorer, data ){
  97. this.explorer = explorer;
  98. this.app = explorer.app;
  99. this.data = data || {};
  100. this.css = this.explorer.css;
  101. this.load();
  102. },
  103. load : function(){
  104. },
  105. open: function(e){
  106. this.isNew = false;
  107. this.isEdited = false;
  108. this._open();
  109. },
  110. create: function(){
  111. this.isNew = true;
  112. this._open();
  113. },
  114. edit: function(){
  115. this.isEdited = true;
  116. this._open();
  117. },
  118. _open : function(){
  119. this.createMarkNode = new Element("div", {
  120. "styles": this.css.createMarkNode,
  121. "events": {
  122. "mouseover": function(e){e.stopPropagation();},
  123. "mouseout": function(e){e.stopPropagation();}
  124. }
  125. }).inject(this.app.content, "after");
  126. this.createAreaNode = new Element("div", {
  127. "styles": this.css.createAreaNode
  128. });
  129. this.createNode();
  130. this.createAreaNode.inject(this.createMarkNode, "after");
  131. this.createAreaNode.fade("in");
  132. this.setCreateNodeSize();
  133. this.setCreateNodeSizeFun = this.setCreateNodeSize.bind(this);
  134. this.addEvent("resize", this.setCreateNodeSizeFun);
  135. },
  136. createNode: function(){
  137. var _self = this;
  138. this.createNode = new Element("div", {
  139. "styles": this.css.createNode
  140. }).inject(this.createAreaNode);
  141. this.createIconNode = new Element("div", {
  142. "styles": this.isNew ? this.css.createNewNode : this.css.createIconNode
  143. }).inject(this.createNode);
  144. this.createFormNode = new Element("div", {
  145. "styles": this.css.createFormNode
  146. }).inject(this.createNode);
  147. var lp = this.app.lp.holiday;
  148. var inputStyle = "width: 99%; border:1px solid #999; background-color:#FFF; border-radius: 3px; box-shadow: 0px 0px 6px #CCC;height: 26px;";
  149. var inputTimeStyle = "width: 99%; border:1px solid #999; background-color:#FFF; border-radius: 3px; box-shadow: 0px 0px 6px #CCC;height: 26px;"+
  150. "background : url(/x_component_Attendance/$HolidayExplorer/default/icon/calendar.png) 98% center no-repeat";
  151. var html = "<table width='100%' height='200' border='0' cellPadding='0' cellSpacing='0'>" +
  152. "<tr>"+
  153. "<td colspan='2' style='height: 50px; line-height: 50px; text-align: center; min-width: 80px; font-size:18px;font-weight: bold;'>" + lp.setHoliday + "</td>" +
  154. "</tr>" +
  155. "<tr>"+
  156. "<td style='height: 30px; line-height: 30px; text-align: left; min-width: 80px; width:25%'>" + lp.year + ":</td>" +
  157. "<td style='; text-align: left;' id='yearArea'>"+
  158. //(!this.isNew && !this.isEdited ? "" :
  159. // ("<input type='text' id='configYear' " + "style='" + inputStyle +"'" + " value='" + ( this.data && this.data.configYear ? this.data.configYear : "") + "'/>")) +
  160. "</td>"+
  161. "</tr>" +
  162. "<tr>"+
  163. "<td style='height: 30px; line-height: 30px; text-align: left'>"+lp.name+":</td>" +
  164. "<td style='; text-align: right;'>"+
  165. (!this.isNew && !this.isEdited ? "" :
  166. ("<input type='text' id='configName' " + "style='" + inputStyle +"'" + " value='" + ( this.data && this.data.configName ? this.data.configName : "") + "'/>")) +
  167. "</td>" +
  168. "</tr>" +
  169. "<tr>" +
  170. "<td style='height: 30px; line-height: 30px; text-align: left'>"+lp.startDate+":</td>" +
  171. "<td style='; text-align: right;'>" +
  172. (!this.isNew && !this.isEdited ? "" :
  173. ("<input type='text' id='startDate' " + "style='" + inputTimeStyle +"'" + " value='" + ( this.data && this.data.startDate ? this.data.startDate : "") + "'/>")) +
  174. "</td>" +
  175. "</tr>" +
  176. "<tr>" +
  177. "<td style='height: 30px; line-height: 30px; text-align: left'>"+lp.endDate+":</td>" +
  178. "<td style='; text-align: right;'>" +
  179. (!this.isNew && !this.isEdited ? "" :
  180. ("<input type='text' id='endDate' " + "style='" + inputTimeStyle +"'" + " value='" + ( this.data && this.data.endDate ? this.data.endDate : "") + "'/>")) +
  181. "</td>" +
  182. "</tr>" +
  183. "<tr>" +
  184. "<td style='height: 30px; line-height: 30px; text-align: left'>"+lp.makeUpClassDay+":</td>" +
  185. "<td style='; text-align: right;'>" +
  186. (!this.isNew && !this.isEdited ? "" :
  187. ("<input type='text' id='makeUpClassDay' " + "style='" + inputTimeStyle +"'" + " value='" + ( this.data && this.data.makeUpClassDay ? this.data.makeUpClassDay : "") + "'/>")) +
  188. "</td>" +
  189. "</tr>" +
  190. "</table>";
  191. this.createFormNode.set("html", html);
  192. //this.configYear = this.createFormNode.getElement("#configYear");
  193. this.yearArea = this.createFormNode.getElement("#yearArea");
  194. this.configYear = new MDomItem( this.yearArea, {
  195. "name" : "configYear",
  196. "type" : "select",
  197. "value" : this.data.configYear || new Date().getFullYear(),
  198. "selectValue" : function(){
  199. var years = [];
  200. var year = new Date().getFullYear()+5;
  201. for(var i=0; i<11; i++ ){
  202. years.push( year-- );
  203. }
  204. return years;
  205. }
  206. }, true, this.app );
  207. this.configYear.load();
  208. this.configName = this.createFormNode.getElement("#configName");
  209. this.startDate = this.createFormNode.getElement("#startDate");
  210. this.endDate = this.createFormNode.getElement("#endDate");
  211. this.makeUpClassDay = this.createFormNode.getElement("#makeUpClassDay");
  212. this.startDate.addEvent("click",function(){
  213. _self.selectCalendar(this);
  214. })
  215. this.endDate.addEvent("click",function(){
  216. _self.selectCalendar(this);
  217. })
  218. this.makeUpClassDay.addEvent("click",function(){
  219. _self.selectCalendar(this);
  220. })
  221. this.cancelActionNode = new Element("div", {
  222. "styles": this.css.createCancelActionNode,
  223. "text": this.app.lp.cancel
  224. }).inject(this.createFormNode);
  225. this.createOkActionNode = new Element("div", {
  226. "styles": this.css.createOkActionNode,
  227. "text": this.app.lp.ok
  228. }).inject(this.createFormNode);
  229. this.cancelActionNode.addEvent("click", function(e){
  230. this.cancelCreate(e);
  231. }.bind(this));
  232. this.createOkActionNode.addEvent("click", function(e){
  233. this.okCreate(e);
  234. }.bind(this));
  235. },
  236. setCreateNodeSize: function (width, height, top, left) {
  237. if (!width)width = this.options && this.options.width ? this.options.width : "50%"
  238. if (!height)height = this.options && this.options.height ? this.options.height : "50%"
  239. if (!top) top = this.options && this.options.top ? this.options.top : 0;
  240. if (!left) left = this.options && this.options.left ? this.options.left : 0;
  241. var allSize = this.app.content.getSize();
  242. var limitWidth = allSize.x; //window.screen.width
  243. var limitHeight = allSize.y; //window.screen.height
  244. "string" == typeof width && (1 < width.length && "%" == width.substr(width.length - 1, 1)) && (width = parseInt(limitWidth * parseInt(width, 10) / 100, 10));
  245. "string" == typeof height && (1 < height.length && "%" == height.substr(height.length - 1, 1)) && (height = parseInt(limitHeight * parseInt(height, 10) / 100, 10));
  246. 300 > width && (width = 300);
  247. 220 > height && (height = 220);
  248. top = top || parseInt((limitHeight - height) / 2, 10);
  249. left = left || parseInt((limitWidth - width) / 2, 10);
  250. this.createAreaNode.setStyles({
  251. "width": "" + width + "px",
  252. "height": "" + height + "px",
  253. "top": "" + top + "px",
  254. "left": "" + left + "px"
  255. });
  256. this.createNode.setStyles({
  257. "width": "" + width + "px",
  258. "height": "" + height + "px"
  259. });
  260. var iconSize = this.createIconNode ? this.createIconNode.getSize() : {x: 0, y: 0};
  261. var topSize = this.formTopNode ? this.formTopNode.getSize() : {x: 0, y: 0};
  262. var bottomSize = this.formBottomNode ? this.formBottomNode.getSize() : {x: 0, y: 0};
  263. var contentHeight = height - iconSize.y - topSize.y - bottomSize.y;
  264. //var formMargin = formHeight -iconSize.y;
  265. this.createFormNode.setStyles({
  266. "height": "" + contentHeight + "px"
  267. });
  268. },
  269. //setCreateNodeSize: function(){
  270. // var size = this.app.node.getSize();
  271. // var allSize = this.app.content.getSize();
  272. //
  273. // this.createAreaNode.setStyles({
  274. // "width": ""+size.x+"px",
  275. // "height": ""+size.y+"px"
  276. // });
  277. // var hY = size.y*0.8;
  278. // var mY = size.y*0.2/2;
  279. // this.createNode.setStyles({
  280. // "height": ""+hY+"px",
  281. // "margin-top": ""+mY+"px"
  282. // });
  283. //
  284. // var iconSize = this.createIconNode.getSize();
  285. // var formHeight = hY*0.7;
  286. // if (formHeight>250) formHeight = 250;
  287. // var formMargin = hY*0.3/2-iconSize.y;
  288. // this.createFormNode.setStyles({
  289. // "height": ""+formHeight+"px",
  290. // "margin-top": ""+formMargin+"px"
  291. // });
  292. //},
  293. cancelCreate: function(e){
  294. var _self = this;
  295. if ( this.isNew && this.configName.get("value") ){
  296. this.app.confirm("warn", e,
  297. this.app.lp.create_cancel_title,
  298. this.app.lp.create_cancel, "320px", "100px",
  299. function(){
  300. _self.createMarkNode.destroy();
  301. _self.createAreaNode.destroy();
  302. this.close();
  303. },function(){
  304. this.close();
  305. }
  306. );
  307. }else{
  308. this.createMarkNode.destroy();
  309. this.createAreaNode.destroy();
  310. delete _self;
  311. }
  312. },
  313. okCreate: function(e){
  314. var data = {
  315. "id" : (this.data && this.data.id) ? this.data.id : this.app.restActions.getUUID(),
  316. "configYear": this.configYear.get("value"),
  317. "configName": this.configName.get("value"),
  318. "startDate": this.startDate.get("value"),
  319. "endDate": this.endDate.get("value"),
  320. "makeUpClassDay": this.makeUpClassDay.get("value")
  321. };
  322. if (data.configYear && data.configName && data.startDate && data.endDate ){
  323. var endDate = new Date( data.endDate )
  324. var startDate = new Date( data.startDate )
  325. if( startDate > endDate ){
  326. this.app.notice("开始日期不能大于结束日期","error");
  327. return;
  328. }
  329. var save = function(){
  330. var error = "";
  331. this.getDateByRange(startDate,endDate).each(function( date ){
  332. this.app.restActions.saveHoliday({
  333. "configName" : data.configName,
  334. "configYear" : data.configYear,
  335. "configDate": date,
  336. "configType": "Holiday"
  337. }, function(json){
  338. if( json.type == "ERROR" ){error=json.message}
  339. }.bind(this),
  340. function(json){
  341. flag = false;
  342. }.bind(this),false);
  343. }.bind(this))
  344. if(data.makeUpClassDay!=""){
  345. data.makeUpClassDay.split(",").each(function( date ){
  346. this.app.restActions.saveHoliday({
  347. "configName" : data.configName,
  348. "configYear" : data.configYear,
  349. "configDate": this.dateFormat( new Date(date),"yyyy-MM-dd"),
  350. "configType": "Workday"
  351. }, function(json){
  352. if( json.type == "ERROR" ){error=json.message}
  353. },function(json){
  354. flag = false;
  355. }.bind(this),false);
  356. }.bind(this))
  357. }
  358. if(error==""){
  359. this.createMarkNode.destroy();
  360. this.createAreaNode.destroy();
  361. if(this.explorer.view)this.explorer.view.reload();
  362. this.app.notice( this.isNew ? this.app.lp.createSuccess : this.app.lp.updateSuccess , "success");
  363. }else{
  364. this.app.notice( error , "error");
  365. }
  366. }.bind(this)
  367. if(!this.isNew){
  368. this.explorer.view._removeDocument(data, false, save )
  369. }else{
  370. save();
  371. }
  372. }else{
  373. this.configName.setStyle("border-color", "red");
  374. this.configName.focus();
  375. this.app.notice( this.app.lp.holiday.inputValid, "error");
  376. }
  377. },
  378. selectCalendar : function( calendarNode ){
  379. MWF.require("MWF.widget.Calendar", function(){
  380. var calendar = new MWF.widget.Calendar( calendarNode, {
  381. "style": "xform",
  382. "isTime": false,
  383. "target": this.app.content
  384. });
  385. calendar.show();
  386. }.bind(this));
  387. },
  388. getDateByRange : function(startDate, endDate){
  389. var days = [];
  390. while (startDate <= endDate) {
  391. days.push( this.dateFormat(startDate,"yyyy-MM-dd") );
  392. startDate.setDate(startDate.getDate() + 1);
  393. }
  394. return days;
  395. },
  396. dateFormat : function(date, fmt){
  397. var o = {
  398. "M+" : date.getMonth()+1, //月份
  399. "d+" : date.getDate(), //日
  400. "h+" : date.getHours(), //小时
  401. "m+" : date.getMinutes(), //分
  402. "s+" : date.getSeconds(), //秒
  403. "q+" : Math.floor((date.getMonth()+3)/3), //季度
  404. "S" : date.getMilliseconds() //毫秒
  405. };
  406. if(/(y+)/.test(fmt))
  407. fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
  408. for(var k in o)
  409. if(new RegExp("("+ k +")").test(fmt))
  410. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  411. return fmt;
  412. }
  413. });