Template.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. MWF.xApplication.CRM = MWF.xApplication.CRM || {};
  2. MWF.xApplication.CRM.Template = MWF.xApplication.CRM.Template || {};
  3. MWF.xDesktop.requireApp("Template", "MPopupForm", null, false);
  4. String.implement({
  5. toDOM: function( container, callback ){
  6. var wrapper = this.test('^<the|^<tf|^<tb|^<colg|^<ca') && ['<table>', '</table>', 1] ||
  7. this.test('^<col') && ['<table><colgroup>', '</colgroup><tbody></tbody></table>',2] ||
  8. this.test('^<tr') && ['<table><tbody>', '</tbody></table>', 2] ||
  9. this.test('^<th|^<td') && ['<table><tbody><tr>', '</tr></tbody></table>', 3] ||
  10. this.test('^<li') && ['<ul>', '</ul>', 1] ||
  11. this.test('^<dt|^<dd') && ['<dl>', '</dl>', 1] ||
  12. this.test('^<le') && ['<fieldset>', '</fieldset>', 1] ||
  13. this.test('^<opt') && ['<select multiple="multiple">', '</select>', 1] ||
  14. ['', '', 0];
  15. if( container ){
  16. var el = new Element('div', {html: wrapper[0] + this + wrapper[1]}).getChildren();
  17. while(wrapper[2]--) el = el[0].getChildren();
  18. el.inject( container )
  19. if( callback )callback( container );
  20. return el;
  21. }else{
  22. var div = new Element('div', {html: wrapper[0] + this + wrapper[1]});
  23. div.setStyle("display","none").inject( $(document.body) );
  24. if( callback )callback( div );
  25. var el = div.getChildren();
  26. while(wrapper[2]--) el = el[0].getChildren();
  27. div.dispose();
  28. return el;
  29. }
  30. }
  31. });
  32. MWF.xApplication.CRM.Template.Select = new Class({
  33. Extends: MWF.widget.Common,
  34. Implements: [Options, Events],
  35. options: {
  36. "style": "default",
  37. "width": "800",
  38. "height": "100%"
  39. },
  40. initialize: function (node ,explorer, actions, options) {
  41. this.setOptions(options);
  42. this.app = explorer.app;
  43. this.explorer = explorer;
  44. this.lp = this.app.lp.template;
  45. this.actions = this.app.restActions;
  46. this.path = "/x_component_CRM/Template/";
  47. this.loadCss();
  48. this.node = $(node);
  49. this.actions = actions;
  50. },
  51. loadCss: function () {
  52. this.cssPath = "/x_component_CRM/$Template/" + this.options.style + "/css.wcss";
  53. this._loadCss();
  54. },
  55. load:function(data,callback){
  56. this._width = this.options.width?this.options.width:230;
  57. this._height = this.options.height?this.options.height:30;
  58. this._available = this.options.available?this.options.available:"yes";
  59. this.createDefault();
  60. this.explorer.allArrowArr.push(this.selectArrowDiv);
  61. //this.setList(data,callback);
  62. if(callback)callback();
  63. },
  64. createDefault:function(){
  65. if(this.node)this.node.empty();
  66. this.selectValueDiv = new Element("div.selectValueDiv",{
  67. "styles":this.css.selectValueDiv,
  68. "id":this.node.get("id")+"Value",
  69. "text":this.lp.defaultSelect
  70. }).inject(this.node);
  71. this.selectArrowDiv = new Element("div.selectArrowDiv",{
  72. "styles":this.css.selectArrowDiv
  73. }).inject(this.node);
  74. this.selectArrowDiv.setStyles({
  75. "width":this._height+"px",
  76. "height":this._height+"px"
  77. });
  78. this.node.setStyles(this.css.selectDiv);
  79. this.node.setStyles({
  80. "width":this._width+"px",
  81. "height":this._height+"px",
  82. "background-color":this._available=="no"?"#eeeeee":""
  83. });
  84. this.node.set("available",this._available);
  85. this.selectValueDiv.setStyles({
  86. "width":(this._width-this._height-10)+"px",
  87. "height":this._height+"px",
  88. "line-height":this._height+"px"
  89. });
  90. },
  91. setAddress:function(data,callback){
  92. data = data || {};
  93. var _self = this;
  94. this.node.removeEvents("click");
  95. this.node.addEvents({
  96. "click":function(e){
  97. if(!data)return false;
  98. if(_self.node.get("available")=="no") return false;
  99. _self.selectArrowDiv.setStyles({
  100. "background":"url(/x_component_CRM/$Template/default/icons/arrow-up.png) no-repeat center"
  101. });
  102. if(_self.explorer.listContentDiv)_self.explorer.listContentDiv.destroy();
  103. if(_self.explorer.listDiv)_self.explorer.listDiv.destroy();
  104. _self.explorer.listContentDiv = new Element("div.listContentDiv",{"styles":_self.css.listContentDiv,"id":"listContentDiv"}).inject(_self.node);
  105. _self.explorer.listContentDiv.setStyles({
  106. "width":_self.node.getSize().x+"px",
  107. "margin-top":(_self.node.getSize().y)+"px",
  108. "z-index":"300"
  109. });
  110. _self.listDiv = new Element("div.listDiv",{"styles":_self.css.listDiv}).inject(_self.explorer.listContentDiv);
  111. _self.app.setScrollBar(_self.listDiv);
  112. data.unshift({
  113. "cityname":_self.lp.defaultSelect
  114. });
  115. data.each(function(d){
  116. var listLi = new Element("li.listLi",{
  117. "styles":_self.css.listLi,
  118. "text": d.cityname
  119. }).inject(_self.listDiv);
  120. listLi.setStyles({
  121. "color":_self.selectValueDiv.get("text")==listLi.get("text")?"#ffffff":"",
  122. "background-color":_self.selectValueDiv.get("text")==listLi.get("text")?"#3d77c1":""
  123. });
  124. listLi.addEvents({
  125. "click":function(ev){
  126. _self.node.set("value",this.get("text"));
  127. _self.explorer.listContentDiv.destroy();
  128. _self.selectArrowDiv.setStyles({"background":"url(/x_component_CRM/$Template/default/icons/arrow.png) no-repeat center"});
  129. if(_self.selectValueDiv.get("text")!=this.get("text")){
  130. _self.selectValueDiv.set({"text":this.get("text")});
  131. if(callback)callback(d);
  132. }
  133. ev.stopPropagation();
  134. },
  135. "mouseover":function(){
  136. if(this.get("text") != _self.selectValueDiv.get("text")){
  137. this.setStyles({
  138. "background-color":"#ccc",
  139. "color":"#ffffff"
  140. });
  141. }
  142. },
  143. "mouseout":function(){
  144. if(this.get("text") != _self.selectValueDiv.get("text")){
  145. this.setStyles({
  146. "background-color":"",
  147. "color":""
  148. });
  149. }
  150. }
  151. });
  152. }.bind(_self));
  153. data.splice(0,1);
  154. e.stopPropagation();
  155. }.bind(this)
  156. })
  157. },
  158. setList:function(data,callback){
  159. data = data || {};
  160. var _self = this;
  161. this.node.removeEvents("click");
  162. this.node.addEvents({
  163. "click":function(e){
  164. if(!data.childNodes)return false;
  165. if(_self.node.get("available")=="no") return false;
  166. _self.selectArrowDiv.setStyles({
  167. "background":"url(/x_component_CRM/$Template/default/icons/arrow-up.png) no-repeat center"
  168. });
  169. if(_self.explorer.listContentDiv)_self.explorer.listContentDiv.destroy();
  170. if(_self.explorer.listDiv)_self.explorer.listDiv.destroy();
  171. _self.explorer.listContentDiv = new Element("div.listContentDiv",{"styles":_self.css.listContentDiv,"id":"listContentDiv"}).inject(_self.node);
  172. _self.explorer.listContentDiv.setStyles({
  173. "width":_self.node.getSize().x+"px",
  174. "margin-top":(_self.node.getSize().y)+"px",
  175. "z-index":"300"
  176. });
  177. _self.listDiv = new Element("div.listDiv",{"styles":_self.css.listDiv}).inject(_self.explorer.listContentDiv);
  178. _self.app.setScrollBar(_self.listDiv);
  179. data.childNodes.unshift({
  180. "configname":_self.lp.defaultSelect
  181. });
  182. data.childNodes.each(function(d){
  183. var listLi = new Element("li.listLi",{
  184. "styles":_self.css.listLi,
  185. "text": d.configname
  186. }).inject(_self.listDiv);
  187. listLi.setStyles({
  188. "color":_self.selectValueDiv.get("text")==listLi.get("text")?"#ffffff":"",
  189. "background-color":_self.selectValueDiv.get("text")==listLi.get("text")?"#3d77c1":""
  190. });
  191. listLi.addEvents({
  192. "click":function(ev){
  193. _self.selectValueDiv.set({"text":this.get("text")});
  194. _self.node.set("value",this.get("text"));
  195. _self.explorer.listContentDiv.destroy();
  196. _self.selectArrowDiv.setStyles({"background":"url(/x_component_CRM/$Template/default/icons/arrow.png) no-repeat center"});
  197. if(callback)callback(d);
  198. ev.stopPropagation();
  199. },
  200. "mouseover":function(){
  201. if(this.get("text") != _self.selectValueDiv.get("text")){
  202. this.setStyles({
  203. "background-color":"#ccc",
  204. "color":"#ffffff"
  205. });
  206. }
  207. },
  208. "mouseout":function(){
  209. if(this.get("text") != _self.selectValueDiv.get("text")){
  210. this.setStyles({
  211. "background-color":"",
  212. "color":""
  213. });
  214. }
  215. }
  216. });
  217. }.bind(_self));
  218. data.childNodes.splice(0,1);
  219. e.stopPropagation();
  220. }.bind(this)
  221. })
  222. }
  223. });
  224. MWF.xApplication.CRM.Template.PopupForm = new Class({
  225. Extends: MWF.widget.Common,
  226. Implements: [Options, Events],
  227. options: {
  228. "style": "default",
  229. "width": 500,
  230. "height": 450,
  231. "top": 0,
  232. "left": 0,
  233. "hasTop": false,
  234. "hasTopIcon" : false,
  235. "hasTopContent" : false,
  236. "hasIcon": true,
  237. "hasScroll" : true,
  238. "hasBottom": true,
  239. "hasMask" : true,
  240. "title": "",
  241. "draggable": false,
  242. "maxAction" : "false",
  243. "closeAction": true,
  244. "relativeToApp" : true,
  245. "sizeRelateTo" : "app" //desktop
  246. },
  247. initialize: function (explorer, data, options, para) {
  248. this.setOptions(options);
  249. this.explorer = explorer;
  250. if( para ){
  251. if( this.options.relativeToApp ){
  252. this.app = para.app || this.explorer.app;
  253. this.container = para.container || this.app.content;
  254. this.lp = para.lp || this.explorer.lp || this.app.lp;
  255. this.css = para.css || this.explorer.css || this.app.css;
  256. this.actions = para.actions || this.explorer.actions || this.app.actions || this.app.restActions;
  257. }else{
  258. this.container = para.container;
  259. this.lp = para.lp || this.explorer.lp;
  260. this.css = para.css || this.explorer.css;
  261. this.actions = para.actions || this.explorer.actions;
  262. }
  263. }else{
  264. if( this.options.relativeToApp ){
  265. this.app = this.explorer.app;
  266. this.container = this.app.content;
  267. this.lp = this.explorer.lp || this.app.lp;
  268. this.css = this.explorer.css || this.app.css;
  269. this.actions = this.explorer.actions || this.app.actions || this.app.restActions;
  270. }else{
  271. this.container = window.document.body;
  272. this.lp = this.explorer.lp;
  273. this.css = this.explorer.css;
  274. this.actions = this.explorer.actions;
  275. }
  276. }
  277. this.data = data || {};
  278. this.cssPath = "/x_component_CRM/$Template/"+this.options.style+"/popup.wcss";
  279. this.load();
  280. },
  281. load: function () {
  282. this._loadCss();
  283. },
  284. _loadCss: function(){
  285. var css = {};
  286. var r = new Request.JSON({
  287. url: this.cssPath,
  288. secure: false,
  289. async: false,
  290. method: "get",
  291. noCache: false,
  292. onSuccess: function(responseJSON, responseText){
  293. css = responseJSON;
  294. MWF.widget.css[key] = responseJSON;
  295. }.bind(this),
  296. onError: function(text, error){
  297. alert(error + text);
  298. }
  299. });
  300. r.send();
  301. var isEmptyObject = true;
  302. for( var key in css ){
  303. if(key)isEmptyObject = false
  304. }
  305. if( !isEmptyObject ){
  306. this.css = Object.merge( css, this.css );
  307. }
  308. },
  309. open: function (e) {
  310. this.fireEvent("queryOpen");
  311. this.isNew = false;
  312. this.isEdited = false;
  313. this._open();
  314. this.fireEvent("postOpen");
  315. },
  316. create: function () {
  317. this.fireEvent("queryCreate");
  318. this.isNew = true;
  319. this._open();
  320. this.fireEvent("postCreate");
  321. },
  322. edit: function () {
  323. this.fireEvent("queryEdit");
  324. this.isEdited = true;
  325. this._open();
  326. this.fireEvent("postEdit");
  327. },
  328. _open: function () {
  329. if( this.options.hasMask ){
  330. this.formMaskNode = new Element("div.formMaskNode", {
  331. "styles": this.css.formMaskNode,
  332. "events": {
  333. "mouseover": function (e) {
  334. e.stopPropagation();
  335. },
  336. "mouseout": function (e) {
  337. e.stopPropagation();
  338. },
  339. "click": function (e) {
  340. e.stopPropagation();
  341. }
  342. }
  343. }).inject( this.container || this.app.content);
  344. }
  345. this.formAreaNode = new Element("div.formAreaNode", {
  346. "styles": this.css.formAreaNode
  347. });
  348. this.createFormNode();
  349. this.formAreaNode.inject(this.formMaskNode || this.container || this.app.content, "after");
  350. this.formAreaNode.fade("in");
  351. this.setFormNodeSize();
  352. this.setFormNodeSizeFun = this.setFormNodeSize.bind(this);
  353. if( this.app )this.app.addEvent("resize", this.setFormNodeSizeFun);
  354. if (this.options.draggable && this.formTopNode) {
  355. var size = (this.container || this.app.content).getSize();
  356. var nodeSize = this.formAreaNode.getSize();
  357. this.formAreaNode.makeDraggable({
  358. "handle": this.formTopNode,
  359. "limit": {
  360. "x": [0, size.x - nodeSize.x],
  361. "y": [0, size.y - nodeSize.y]
  362. }
  363. });
  364. }
  365. },
  366. createFormNode: function () {
  367. var _self = this;
  368. this.formNode = new Element("div.formNode", {
  369. "styles": this.css.formNode
  370. }).inject(this.formAreaNode);
  371. if (this.options.hasTop) {
  372. this.createTopNode();
  373. }
  374. if (this.options.hasIcon) {
  375. this.formIconNode = new Element("div.formIconNode", {
  376. "styles": this.isNew ? this.css.formNewNode : this.css.formIconNode
  377. }).inject(this.formNode);
  378. }
  379. this.createContent();
  380. //formContentNode.set("html", html);
  381. if (this.options.hasBottom) {
  382. this.createBottomNode();
  383. }
  384. this._setCustom();
  385. if( this.options.hasScroll ){
  386. //this.setScrollBar(this.formTableContainer)
  387. MWF.require("MWF.widget.ScrollBar", function () {
  388. new MWF.widget.ScrollBar(this.formTableContainer, {
  389. "indent": false,
  390. "style": "default",
  391. "where": "before",
  392. "distance": 30,
  393. "friction": 4,
  394. "axis": {"x": false, "y": true},
  395. "onScroll": function (y) {
  396. //var scrollSize = _self.viewContainerNode.getScrollSize();
  397. //var clientSize = _self.viewContainerNode.getSize();
  398. //var scrollHeight = scrollSize.y - clientSize.y;
  399. //if (y + 200 > scrollHeight && _self.view && _self.view.loadElementList) {
  400. // if (!_self.view.isItemsLoaded) _self.view.loadElementList();
  401. //}
  402. }
  403. });
  404. }.bind(this));
  405. }
  406. },
  407. _setCustom : function(){
  408. },
  409. createTopNode: function () {
  410. if (!this.formTopNode) {
  411. this.formTopNode = new Element("div.formTopNode", {
  412. "styles": this.css.formTopNode
  413. }).inject(this.formNode);
  414. if(this.options.hasTopIcon){
  415. this.formTopIconNode = new Element("div", {
  416. "styles": this.css.formTopIconNode
  417. }).inject(this.formTopNode)
  418. }
  419. this.formTopTextNode = new Element("div", {
  420. "styles": this.css.formTopTextNode,
  421. "text": this.options.title
  422. }).inject(this.formTopNode);
  423. if (this.options.closeAction) {
  424. this.formTopCloseActionNode = new Element("div", {"styles": this.css.formTopCloseActionNode}).inject(this.formTopNode);
  425. this.formTopCloseActionNode.addEvent("click", function () {
  426. this.close()
  427. }.bind(this))
  428. }
  429. if(this.options.hasTopContent){
  430. this.formTopContentNode = new Element("div.formTopContentNode", {
  431. "styles": this.css.formTopContentNode
  432. }).inject(this.formTopNode);
  433. this._createTopContent();
  434. }
  435. }
  436. },
  437. _createTopContent: function () {
  438. },
  439. createContent: function () {
  440. this.formContentNode = new Element("div.formContentNode", {
  441. "styles": this.css.formContentNode
  442. }).inject(this.formNode);
  443. this.formTableContainer = new Element("div.formTableContainer", {
  444. "styles": this.css.formTableContainer
  445. }).inject(this.formContentNode);
  446. this.formTableArea = new Element("div.formTableArea", {
  447. "styles": this.css.formTableArea,
  448. "text":"loading..."
  449. }).inject(this.formTableContainer);
  450. this._createTableContent();
  451. },
  452. _createTableContent: function () {
  453. },
  454. createBottomNode: function () {
  455. this.formBottomNode = new Element("div.formBottomNode", {
  456. "styles": this.css.formBottomNode
  457. }).inject(this.formNode);
  458. this._createBottomContent()
  459. },
  460. _createBottomContent: function () {
  461. this.cancelActionNode = new Element("div.formCancelActionNode", {
  462. "styles": this.css.formCancelActionNode,
  463. "text": this.lp.actionCancel
  464. }).inject(this.formBottomNode);
  465. this.cancelActionNode.addEvent("click", function (e) {
  466. this.cancel(e);
  467. }.bind(this));
  468. },
  469. cancel: function (e) {
  470. this.fireEvent("queryCancel");
  471. this.close();
  472. this.fireEvent("postCancel");
  473. },
  474. close: function (e) {
  475. this.fireEvent("queryClose");
  476. this._close();
  477. if(this.setFormNodeSizeFun && this.app ){
  478. this.app.removeEvent("resize",this.setFormNodeSizeFun);
  479. }
  480. if( this.formMaskNode )this.formMaskNode.destroy();
  481. this.formAreaNode.destroy();
  482. this.fireEvent("postClose");
  483. delete this;
  484. },
  485. _close: function(){
  486. },
  487. ok: function (e) {
  488. this.fireEvent("queryOk");
  489. var data = this.form.getResult(true, ",", true, false, true);
  490. if (data) {
  491. this._ok(data, function (json) {
  492. if (json.type == "error") {
  493. if( this.app )this.app.notice(json.message, "error");
  494. } else {
  495. if( this.formMaskNode )this.formMaskNode.destroy();
  496. this.formAreaNode.destroy();
  497. if (this.explorer && this.explorer.view)this.explorer.view.reload();
  498. if( this.app )this.app.notice(this.isNew ? this.lp.createSuccess : this.lp.updateSuccess, "success");
  499. this.fireEvent("postOk");
  500. }
  501. }.bind(this))
  502. }
  503. },
  504. _ok: function (data, callback) {
  505. //this.app.restActions.saveDocument( this.data.id, data, function(json){
  506. // if( callback )callback(json);
  507. //}.bind(this), function( errorObj ){
  508. // var error = JSON.parse( errorObj.responseText );
  509. // this.app.notice( error.message, error );
  510. //}.bind(this));
  511. },
  512. setFormNodeSize: function (width, height, top, left) {
  513. if (!width)width = this.options.width ? this.options.width : "50%";
  514. if (!height)height = this.options.height ? this.options.height : "50%";
  515. if (!top) top = this.options.top ? this.options.top : 0;
  516. if (!left) left = this.options.left ? this.options.left : 0;
  517. //var appTitleSize = this.app.window.title.getSize();
  518. var allSize = ( this.container || this.app.content).getSize();
  519. var limitWidth = allSize.x; //window.screen.width
  520. var limitHeight = allSize.y; //window.screen.height
  521. "string" == typeof width && (1 < width.length && "%" == width.substr(width.length - 1, 1)) && (width = parseInt(limitWidth * parseInt(width, 10) / 100, 10));
  522. "string" == typeof height && (1 < height.length && "%" == height.substr(height.length - 1, 1)) && (height = parseInt(limitHeight * parseInt(height, 10) / 100, 10));
  523. 300 > width && (width = 300);
  524. 220 > height && (height = 220);
  525. top = top || parseInt((limitHeight - height) / 2, 10); //+appTitleSize.y);
  526. left = left || parseInt((limitWidth - width) / 2, 10);
  527. this.formAreaNode.setStyles({
  528. "width": "" + width + "px",
  529. "height": "" + height + "px",
  530. "top": "" + top + "px",
  531. "left": "" + left + "px"
  532. });
  533. this.formNode.setStyles({
  534. "width": "" + width + "px",
  535. "height": "" + height + "px"
  536. });
  537. var iconSize = this.formIconNode ? this.formIconNode.getSize() : {x: 0, y: 0};
  538. var topSize = this.formTopNode ? this.formTopNode.getSize() : {x: 0, y: 0};
  539. var bottomSize = this.formBottomNode ? this.formBottomNode.getSize() : {x: 0, y: 0};
  540. var contentHeight = height - iconSize.y - topSize.y - bottomSize.y;
  541. //var formMargin = formHeight -iconSize.y;
  542. this.formContentNode.setStyles({
  543. "height": "" + contentHeight + "px"
  544. });
  545. this.formTableContainer.setStyles({
  546. "height": "" + contentHeight + "px"
  547. });
  548. }
  549. });
  550. MWF.xApplication.CRM.Template.ComplexView = new Class({
  551. Implements: [Options, Events],
  552. options: {
  553. "style": "default",
  554. "templateUrl": "",
  555. "scrollEnable" : false,
  556. "pagingEnable" : false,
  557. "documentKeyWord" : null,
  558. "pagingPar" : {
  559. position : [ "top" , "bottom" ], //分页条,上下
  560. countPerPage : 20,
  561. visiblePages : 10,
  562. currentPage : 1,
  563. currentItem : null,
  564. hasPagingBar : true,
  565. hasTruningBar : true,
  566. hasNextPage : true,
  567. hasPrevPage : false,
  568. hasReturn : true
  569. }
  570. },
  571. initialize: function (container, app, explorer, options, para) {
  572. this.container = container;
  573. this.explorer = explorer;
  574. if( para ){
  575. this.app = app || para.app || this.explorer.app;
  576. this.lp = para.lp || this.explorer.lp || this.app.lp;
  577. this.css = para.css || this.explorer.css || this.app.css;
  578. this.actions = para.actions || this.explorer.actions || this.app.actions || this.app.restActions;
  579. }else{
  580. this.app = app || this.explorer.app;
  581. this.lp = this.explorer.lp || this.app.lp;
  582. this.css = this.explorer.css || this.app.css;
  583. this.actions = this.explorer.actions || this.app.actions || this.app.restActions;
  584. }
  585. if (!options.templateUrl) {
  586. options.templateUrl = this.explorer.path + "listItem.json"
  587. } else if (options.templateUrl.indexOf("/") == -1) {
  588. options.templateUrl = this.explorer.path + options.templateUrl;
  589. }
  590. this.setOptions(options);
  591. },
  592. initData: function () {
  593. this.items = [];
  594. this.documents = {};
  595. this.isItemsLoaded = false;
  596. this.isItemLoadding = false;
  597. this.loadItemQueue = 0;
  598. this.count = 0;
  599. //this.controllers =[];
  600. },
  601. load: function () {
  602. this.initData();
  603. this.ayalyseTemplate();
  604. //this.node = new Element("div.node", {
  605. // "styles": this.css.viewContentListNode
  606. //}).inject(this.node);
  607. //
  608. //if( this.options.scrollEnable ){
  609. // this.setScroll();
  610. //}
  611. //this.getContentTemplateNode();
  612. //this.createViewHeadNode();
  613. //this.initSortData();
  614. //
  615. //this.createViewNode();
  616. //this.createViewHead();
  617. //this.createViewBody();
  618. this.getContentTemplateNode(); //获取template
  619. this.headTableNode = new Element("div.headTableNode", {
  620. "styles": this.css.viewContentListNode
  621. }).inject(this.container);
  622. this.createViewHeadNode();
  623. this.createViewHead();
  624. this.createViewHeadGroup();
  625. this.contentTableNode = new Element("div.contentTableNode", {
  626. "styles": this.css.viewContentListNode
  627. }).inject(this.container);
  628. this.contentTableNode.setStyles({
  629. "overflow-y":"auto",
  630. "height":(this.container.getHeight()-this.headTableNode.getHeight())+"px",
  631. "width":this.headTableNode.getElement("table").getWidth()+"px"
  632. });
  633. //this.app.setScrollBar(this.contentTableNode);
  634. this.createViewNode();
  635. this.createViewBody();
  636. this.createViewContentGroup();
  637. },
  638. reload: function () {
  639. this.clear();
  640. this.node = new Element("div", {
  641. "styles": this.css.viewContentListNode
  642. }).inject(this.container);
  643. this.createViewNode();
  644. this.createViewHead();
  645. this.createViewBody();
  646. },
  647. initSortData: function () {
  648. this.sortField = null;
  649. this.sortType = null;
  650. this.sortFieldDefault = null;
  651. this.sortTypeDefault = null;
  652. },
  653. destroy: function(){
  654. if(this.documentNodeTemplate){
  655. delete this.documentNodeTemplate;
  656. }
  657. if(this.template)delete this.template;
  658. if( this.scrollBar ){
  659. if(this.scrollBar.scrollVAreaNode){
  660. this.scrollBar.scrollVAreaNode.destroy();
  661. }
  662. delete this.scrollBar;
  663. }
  664. if(this.pagingContainerTop ){
  665. if( this.pagingContainerTopCreated ){
  666. this.pagingContainerTop.destroy();
  667. }else{
  668. this.pagingContainerTop.empty();
  669. }
  670. }
  671. if( this.pagingContainerBottom ){
  672. if( this.pagingContainerBottomCreated ){
  673. this.pagingContainerBottom.destroy();
  674. }else{
  675. this.pagingContainerBottom.empty();
  676. }
  677. }
  678. if( this.paging )this.paging.destroy();
  679. this.clear();
  680. delete this;
  681. },
  682. clear: function () {
  683. //if( this.options.pagingEnable ){
  684. // this.documents = null;
  685. // MWF.release(this.items);
  686. // this.items = [];
  687. // this.documents = {};
  688. // this.node.destroy();
  689. // this.container.empty();
  690. // this.node.destroy();
  691. // this.container.empty();
  692. //}else{
  693. // this.documents = null;
  694. // MWF.release(this.items);
  695. // this.items = [];
  696. // this.documents = {};
  697. // this.node.destroy();
  698. // this.container.empty();
  699. // this.isItemsLoaded = false;
  700. // this.isItemLoadding = false;
  701. // this.loadItemQueue = 0;
  702. //}
  703. this.documents = null;
  704. MWF.release(this.items);
  705. this.items = [];
  706. this.documents = {};
  707. this.node.destroy();
  708. this.container.empty();
  709. this.isItemsLoaded = false;
  710. this.isItemLoadding = false;
  711. this.loadItemQueue = 0;
  712. },
  713. clearBody : function(){
  714. this.items.each( function(item,i){
  715. item.destroy();
  716. });
  717. this.documents = null;
  718. MWF.release(this.items);
  719. this.items = [];
  720. this.documents = {};
  721. },
  722. resort: function (el) {
  723. this.sortField = el.retrieve("sortField");
  724. var sortType = el.retrieve("sortType");
  725. if (sortType == "") {
  726. this.sortType = "asc";
  727. } else if (this.sortType == "asc") {
  728. this.sortType = "desc";
  729. } else {
  730. this.sortField = null;
  731. this.sortType = null;
  732. }
  733. this.reload();
  734. },
  735. setScroll: function(){
  736. MWF.require("MWF.widget.ScrollBar", function () {
  737. this.scrollBar = new MWF.widget.ScrollBar(this.container, {
  738. "indent": false,
  739. "style": "xApp_TaskList",
  740. "where": "before",
  741. "distance": 60,
  742. "friction": 4,
  743. "axis": {"x": false, "y": true},
  744. "onScroll": function (y) {
  745. if( !this.options.pagingEnable ){
  746. var scrollSize = this.container.getScrollSize();
  747. var clientSize = this.container.getSize();
  748. var scrollHeight = scrollSize.y - clientSize.y;
  749. if (y + 200 > scrollHeight ) {
  750. if (! this.isItemsLoaded) this.loadElementList();
  751. }
  752. }
  753. }.bind(this)
  754. });
  755. }.bind(this));
  756. },
  757. ayalyseTemplate: function () {
  758. MWF.getJSON(this.options.templateUrl, function (json) {
  759. this.template = json;
  760. }.bind(this), false)
  761. },
  762. formatElement: function (container, setting, clear ) {
  763. //container.appendHTML(setting.html);
  764. var el = setting.html.toDOM( container, function( c , el ){
  765. this.formatStyles(c);
  766. this.formatLable(c);
  767. if(container)this.setEventStyle(c, setting);
  768. }.bind(this) )[0];
  769. if( setting.width ){
  770. el.set("width",setting.width );
  771. }
  772. if( clear && container ){
  773. container.empty();
  774. }
  775. return el;
  776. },
  777. formatStyles: function ( container ) {
  778. container.getElements("[class]").each(function (el) {
  779. var className = el.get("class");
  780. if (className && this.css[className]) {
  781. el.setStyles(this.css[className])
  782. }
  783. }.bind(this))
  784. container.getElements("[styles]").each(function (el) {
  785. var styles = el.get("styles");
  786. if (styles && this.css[styles]) {
  787. el.setStyles(this.css[styles])
  788. }
  789. }.bind(this))
  790. },
  791. formatLable: function (container) {
  792. container.getElements("[lable]").each(function (el) {
  793. var lable = el.get("lable");
  794. if (lable && this.lp[lable]) {
  795. el.set("text", this.lp[lable] + (el.get("colon") ? ":" : "") )
  796. }
  797. }.bind(this))
  798. },
  799. createViewHeadNode: function () {
  800. this.fireEvent("queryCreateViewHeadNode");
  801. this._queryCreateViewHeadNode();
  802. this.viewHeadNode = this.formatElement(this.headTableNode, this.template.viewHeadSetting);
  803. this._postCreateViewHeadNode( this.viewHeadNode );
  804. this.fireEvent("postCreateViewHeadNode");
  805. if (!this.viewHeadNode)return;
  806. },
  807. createViewNode: function () {
  808. this.fireEvent("queryCreateViewNode");
  809. this._queryCreateViewNode();
  810. this.viewNode = this.formatElement(this.contentTableNode, this.template.viewSetting);
  811. this._postCreateViewNode( this.viewNode );
  812. this.fireEvent("postCreateViewNode");
  813. if (!this.viewNode)return;
  814. },
  815. getContentTemplateNode: function(){
  816. this.documentNodeTemplate = this.formatElement(null, this.template.documentSetting);
  817. this.template.items.each(function (item) {
  818. item.nodeTemplate = this.formatElement(null, item.content);
  819. }.bind(this))
  820. },
  821. createViewHead: function () {
  822. this.fireEvent("queryCreateViewHead");
  823. this._queryCreateViewHead( );
  824. if (this.template) {
  825. if (!this.template.headSetting || this.template.headSetting.disable || !this.template.headSetting.html) {
  826. return;
  827. }
  828. }
  829. var _self = this;
  830. var headNode = this.headNode = this.formatElement(this.viewHeadNode, this.template.headSetting);
  831. this.template.items.each(function (item) {
  832. if( !item.head )return;
  833. ////如果设置了权限,那么options里需要有 对应的设置项才会展现
  834. // 比如 item.access == isAdmin 那么 this.options.isAdmin要为true才展现
  835. if (item.access && !this.options[item.access])return;
  836. if (item.head.access && !this.options[item.head.access])return;
  837. var headItemNode = this.formatElement(headNode, item.head);
  838. if (item.name == "$checkbox") {
  839. this.checkboxElement = new Element("input", {
  840. "type": "checkbox"
  841. }).inject(headItemNode);
  842. this.checkboxElement.addEvent("click", function () {
  843. this.selectAllCheckbox()
  844. }.bind(this))
  845. }
  846. if (item.defaultSort && item.defaultSort != "") {
  847. this.sortFieldDefault = item.name;
  848. this.sortTypeDefault = item.defaultSort;
  849. }
  850. if (item.sort && item.sort != "") {
  851. headItemNode.store("sortField", item.name);
  852. if (this.sortField == item.name && this.sortType != "") {
  853. headItemNode.store("sortType", this.sortType);
  854. this.sortIconNode = new Element("div", {
  855. "styles": this.sortType == "asc" ? this.css.sortIconNode_asc : this.css.sortIconNode_desc
  856. }).inject(headItemNode, "top");
  857. } else {
  858. headItemNode.store("sortType", "");
  859. this.sortIconNode = new Element("div", {"styles": this.css.sortIconNode}).inject(headItemNode, "top");
  860. }
  861. headItemNode.setStyle("cursor", "pointer");
  862. headItemNode.addEvent("click", function () {
  863. _self.resort(this);
  864. })
  865. }
  866. }.bind(this));
  867. this.fireEvent("postCreateViewHead");
  868. this._postCreateViewHead( headNode )
  869. },
  870. createViewHeadGroup:function(){
  871. var tab = this.headTableNode.getElement("table");
  872. if(tab){
  873. this.headColGroup = new Element("colgroup.headColGroup").inject(tab);
  874. this.headWidthAll = 0;
  875. var headCol = null;
  876. this.template.items.each(function (item) {
  877. if(item.head && item.head.width){
  878. headCol = new Element("col.headCol",{
  879. "width":item.head.width
  880. }).inject(this.headColGroup);
  881. this.headWidthAll = this.headWidthAll + parseInt(item.head.width.substring(0,item.head.width.length-2));
  882. }
  883. }.bind(this));
  884. //alert(this.headWidthAll);
  885. //alert(this.headWidthAll>this.container.getWidth()?this.headWidthAll:this.container.getWidth())
  886. tab.setStyles({"width":(this.headWidthAll>this.container.getWidth()?this.headWidthAll:this.container.getWidth())+"px"});
  887. }
  888. },
  889. createViewContentGroup:function(){
  890. var tab = this.contentTableNode.getElement("table");
  891. if(tab){
  892. this.contentColGroup = new Element("colgroup.contentColGroup").inject(tab);
  893. this.contentWidthAll = 0;
  894. var contentCol = null;
  895. this.template.items.each(function (item) {
  896. if(item.content && item.content.width){
  897. contentCol = new Element("col.contentCol",{
  898. "width":item.content.width
  899. }).inject(this.contentColGroup);
  900. this.contentWidthAll = this.contentWidthAll + parseInt(item.content.width.substring(0,item.content.width.length-2));
  901. }
  902. }.bind(this));
  903. //alert(this.contentWidthAll>this.container.getWidth()?this.contentWidthAll:this.container.getWidth())
  904. tab.setStyles({"width":(this.contentWidthAll>this.container.getWidth()?this.contentWidthAll:this.container.getWidth())+"px"});
  905. }
  906. },
  907. setEventStyle: function (node, setting, bingObj, data) {
  908. var _self = this;
  909. var styles, overStyles, downStyles;
  910. var styleStr = setting.styles;
  911. if (typeOf(styleStr) == "string"){
  912. if (styleStr && styleStr.substr(0, "function".length) == "function") {
  913. eval("var fun = " + styleStr );
  914. styles = fun.call(bingObj, data);
  915. }else{
  916. styles = this.css[styleStr];
  917. }
  918. }else if (typeOf(styleStr) == "object"){
  919. styles = styleStr;
  920. }else if (typeOf(styleStr) == "function"){
  921. eval("var fun = " + styleStr );
  922. styles = fun.call(bingObj, data);
  923. }
  924. if (!styles) {
  925. var s = node.get("styles");
  926. if (!s)node.get("class");
  927. if (s)styles = this.css[s]
  928. }
  929. if (setting.icon) {
  930. if (!styles)styles = {};
  931. styles["background-image"] = "url(" + this.explorer.path + "/" + this.explorer.options.style + "/icon/" + setting.icon + ")";
  932. }
  933. if (typeOf(setting.mouseoverStyles) == "string")overStyles = this.css[setting.mouseoverStyles];
  934. if (typeOf(setting.mouseoverStyles) == "object") overStyles = setting.mouseoverStyles;
  935. if (setting.mouseoverIcon) {
  936. if (!overStyles)overStyles = {};
  937. overStyles["background-image"] = "url(" + this.explorer.path + "/" + this.explorer.options.style + "/icon/" + setting.mouseoverIcon + ")"
  938. }
  939. if (typeOf(setting.mousedownStyles) == "string")downStyles = this.css[setting.mousedownStyles];
  940. if (typeOf(setting.mousedownStyles) == "object") downStyles = setting.mousedownStyles;
  941. if (setting.mousedownIcon) {
  942. if (!downStyles)downStyles = {};
  943. downStyles["background-image"] = "url(" + this.explorer.path + "/" + this.explorer.options.style + "/icon/" + setting.mousedownIcon + ")"
  944. }
  945. if (styles)node.setStyles(styles);
  946. if (overStyles && styles) {
  947. node.addEvent("mouseover", function (ev) {
  948. if( !_self.lockNodeStyle )this.node.setStyles(this.styles);
  949. }.bind({"styles": overStyles, "node":node }));
  950. node.addEvent("mouseout", function (ev) {
  951. if( !_self.lockNodeStyle )this.node.setStyles(this.styles);
  952. }.bind({"styles": styles, "node":node}));
  953. }
  954. if (downStyles && ( overStyles || styles)) {
  955. node.addEvent("mousedown", function (ev) {
  956. if( !_self.lockNodeStyle )this.node.setStyles(this.styles);
  957. }.bind({"styles": downStyles, "node":node}));
  958. node.addEvent("mouseup", function (ev) {
  959. if( !_self.lockNodeStyle )this.node.setStyles(this.styles);
  960. }.bind({"styles": overStyles || styles, "node":node}))
  961. }
  962. },
  963. selectAllCheckbox: function () {
  964. var flag = this.checkboxElement.get("checked");
  965. this.items.each(function (it) {
  966. if (it.checkboxElement)it.checkboxElement.set("checked", flag)
  967. }.bind(this))
  968. },
  969. getCheckedItems : function(){
  970. var checkedItems = [];
  971. this.items.each(function (it) {
  972. if (it.checkboxElement.get("checked")) {
  973. checkedItems.push( it )
  974. }
  975. }.bind(this));
  976. return checkedItems;
  977. },
  978. createViewBody : function(){
  979. this.loadElementList();
  980. },
  981. loadElementList: function (count) {
  982. if( this.options.pagingEnable ){
  983. var currentItem = this.options.pagingPar.cloaurrentItem;
  984. var countPerPage = this.options.pagingPar.countPerPage;
  985. if( currentItem ){
  986. var pageNum = Math.ceil( currentItem / countPerPage );
  987. var itemNum = currentItem % countPerPage;
  988. this.loadPagingElementList( count , pageNum, currentItem );
  989. }else{
  990. this.loadPagingElementList( count , this.options.pagingPar.currentPage ); //使用分页的方式
  991. }
  992. }else{
  993. this.loadScrollElementList( count ); //滚动条下拉取下一页
  994. }
  995. },
  996. loadScrollElementList : function( count ){
  997. if (!this.isItemsLoaded) {
  998. if (!this.isItemLoadding) {
  999. this.isItemLoadding = true;
  1000. this._getCurrentPageData(function (json) {
  1001. var length = this.dataCount = json.count; //|| json.data.length;
  1002. if (length <= this.items.length) {
  1003. this.isItemsLoaded = true;
  1004. }
  1005. if( json.data && typeOf( json.data )=="array" ){
  1006. json.data.each(function (data ) {
  1007. var key = data[ this.options.documentKeyWord || "id" ];
  1008. if (!this.documents[key]) {
  1009. var item = this._createDocument(data, this.items.length);
  1010. this.items.push(item);
  1011. this.documents[key] = item;
  1012. }
  1013. }.bind(this));
  1014. }
  1015. this.isItemLoadding = false;
  1016. if (this.loadItemQueue > 0) {
  1017. this.loadItemQueue--;
  1018. this.loadElementList();
  1019. }
  1020. }.bind(this), count);
  1021. } else {
  1022. this.loadItemQueue++;
  1023. }
  1024. }
  1025. },
  1026. loadPagingElementList : function( count, pageNum, itemNum ){
  1027. this.currentPage = pageNum || 1;
  1028. this._getCurrentPageData(function (json) {
  1029. this.dataCount = json.count;
  1030. this.createPaging( json.count, pageNum );
  1031. json.data.each(function (data ) {
  1032. var item = this._createDocument(data, this.items.length);
  1033. this.items.push(item);
  1034. var key = data[ this.options.documentKeyWord || "id" ];
  1035. this.documents[key] = item;
  1036. }.bind(this));
  1037. if( itemNum ){
  1038. if( this.options.documentKeyWord ){
  1039. var top = this.documents[ itemNum ].node.getTop();
  1040. }else{
  1041. var top = this.items[itemNum-1].node.getTop();
  1042. }
  1043. this.fireEvent( "gotoItem", top );
  1044. }
  1045. }.bind(this), count, pageNum );
  1046. },
  1047. createPaging : function( itemSize, pageNum ){
  1048. if( !this.options.pagingEnable || this.paging )return;
  1049. if( this.options.pagingPar.position.indexOf("top") > -1 ){
  1050. if( !this.pagingContainerTop ){
  1051. this.pagingContainerTopCreated = true;
  1052. this.pagingContainerTop = new Element("div", {"styles":this.css.pagingContainer}).inject( this.viewNode, "before" );
  1053. }
  1054. }
  1055. if( this.options.pagingPar.position.indexOf("bottom") > -1 ){
  1056. if( !this.pagingContainerBottom ){
  1057. this.pagingContainerBottomCreated = true;
  1058. this.pagingContainerBottom = new Element("div", {"styles":this.css.pagingContainer}).inject( this.viewNode, "after" );
  1059. }
  1060. }
  1061. var par = Object.merge( this.options.pagingPar, {
  1062. itemSize : itemSize,
  1063. onJumpingPage : function( par ){
  1064. this.loadPagingElementList( this.options.pagingPar.countPerPage, par.pageNum, par.itemNum );
  1065. }.bind(this)
  1066. });
  1067. if( pageNum )par.currentPage = pageNum;
  1068. if( this.options.pagingPar.hasPagingBar ){
  1069. this.paging = new MWF.xApplication.Template.Explorer.Paging(this.pagingContainerTop, this.pagingContainerBottom, par, this.css)
  1070. this.paging.load();
  1071. }
  1072. },
  1073. _getCurrentPageData: function (callback, count, page) {
  1074. if( this.options.pagingEnable ){
  1075. this.actions.listDetailFilter(page, count, filter, function (json) {
  1076. if (callback)callback(json);
  1077. }.bind(this))
  1078. }else{
  1079. if (!count)count = 20;
  1080. var id = (this.items.length) ? this.items[this.items.length - 1].data.id : "(0)";
  1081. var filter = this.filterData || {};
  1082. this.actions.listDetailFilterNext(id, count, filter, function (json) {
  1083. if (callback)callback(json);
  1084. }.bind(this))
  1085. }
  1086. },
  1087. getCurrentPageNum: function(){
  1088. return this.paging.options.currentPage;
  1089. },
  1090. getPageSize: function(){
  1091. return this.paging.options.pageSize;
  1092. },
  1093. gotoPage : function( page ){
  1094. this.paging.gotoPage( page );
  1095. },
  1096. _createDocument: function (data, index) {
  1097. return new MWF.xApplication.CRM.Template.ComplexDocument(this.viewNode, data, this.explorer, this, null,index);
  1098. },
  1099. _openDocument: function (documentData) {
  1100. },
  1101. _removeDocument: function (documentData, all) {
  1102. //var id = document.data.id;
  1103. //this.actions.removeDocument(id, function(json){
  1104. // //json.data.each(function(item){
  1105. // this.items.erase(this.documents[id]);
  1106. // this.documents[id].destroy();
  1107. // MWF.release(this.documents[id]);
  1108. // delete this.documents[id];
  1109. // this.app.notice(this.app.lp.deleteDocumentOK, "success");
  1110. // // }.bind(this));
  1111. //}.bind(this));
  1112. },
  1113. _create: function () {
  1114. this.from = new MPopupForm(this.explorer);
  1115. this.from.create();
  1116. },
  1117. _queryCreateViewHeadNode: function(){
  1118. },
  1119. _postCreateViewHeadNode: function( viewNode ){
  1120. },
  1121. _queryCreateViewNode: function(){
  1122. },
  1123. _postCreateViewNode: function( viewNode ){
  1124. },
  1125. _queryCreateViewHead:function(){
  1126. },
  1127. _postCreateViewHead: function( headNode ){
  1128. }
  1129. });
  1130. MWF.xApplication.CRM.Template.ComplexDocument = new Class({
  1131. Implements: [Options, Events],
  1132. initialize: function (container, data, explorer, view, para, index) {
  1133. this.explorer = explorer;
  1134. this.data = data;
  1135. this.container = container;
  1136. this.view = view;
  1137. this.index = index;
  1138. if( para ){
  1139. this.app = para.app || this.view.app || this.explorer.app;
  1140. this.lp = para.lp || this.view.lp || this.explorer.lp || this.app.lp;
  1141. this.css = para.css || this.view.css || this.explorer.css || this.app.css;
  1142. this.actions = para.actions || this.view.actions || this.explorer.actions || this.app.actions || this.app.restActions;
  1143. }else{
  1144. this.app = this.view.app || this.explorer.app;
  1145. this.lp = this.view.lp || this.explorer.lp || this.app.lp;
  1146. this.css = this.view.css || this.explorer.css || this.app.css;
  1147. this.actions = this.view.actions || this.explorer.actions || this.app.actions || this.app.restActions;
  1148. }
  1149. this.load();
  1150. },
  1151. load: function () {
  1152. this.fireEvent("queryCreateDocumentNode");
  1153. this._queryCreateDocumentNode( this.data );
  1154. var _self = this;
  1155. this.node = this.view.documentNodeTemplate.clone().inject(this.container);
  1156. //this.documentAreaNode = new Element("td", {"styles": this.css.documentNode}).inject(this.node);
  1157. this.view.template.items.each(function (item) {
  1158. if( item.access && this._getItemAccess(item) ){
  1159. this.loadItem(item.name, item.content, item.nodeTemplate)
  1160. }else{
  1161. this.loadItem(item.name, item.content, item.nodeTemplate)
  1162. }
  1163. }.bind(this));
  1164. var setting = this.view.template.documentSetting;
  1165. if( setting.styles || setting.mouseoverStyles || setting.mousedownStyles || setting.icon || setting.mouseoverIcon || setting.mousedownIcon ){
  1166. this.view.setEventStyle( this.node, setting, this, this.data );
  1167. }
  1168. var available = this.getConditionResult(setting.condition);
  1169. if( setting.action && this[setting.action] ){
  1170. if ( available ){
  1171. this.node.addEvent("click", function (ev) {
  1172. this.fun.call(_self, this.node, ev);
  1173. ev.stopPropagation();
  1174. }.bind({fun: this[setting.action], node : this.node}))
  1175. }
  1176. }
  1177. if( setting.event && available ){
  1178. this.bindEvent( this.node, setting.event );
  1179. }
  1180. this.fireEvent("postCreateDocumentNode");
  1181. this._postCreateDocumentNode( this.node, this.data )
  1182. },
  1183. loadItem: function (name, item, nodeTemplate ) {
  1184. var itemNode = this[name] = nodeTemplate.clone();
  1185. if( this.format(itemNode, name, item) ){
  1186. itemNode.inject(this.node);
  1187. }
  1188. if (item.items) {
  1189. var elements = itemNode.getElements("[item]");
  1190. if( itemNode.get("item") )elements.push(itemNode);
  1191. elements.each(function (el) {
  1192. var key = el.get("item");
  1193. var sub = item.items[key];
  1194. if( sub ){
  1195. if( !sub.value && sub.value!="" )sub.value = key;
  1196. if( !this.format(el, name, sub) ){
  1197. el.dispose()
  1198. }
  1199. }
  1200. }.bind(this))
  1201. }
  1202. },
  1203. format: function (itemNode, name, item) {
  1204. var _self = this;
  1205. if (item.access) {
  1206. if (!this._getItemAccess(item))return false;
  1207. }
  1208. //if (item.condition) {
  1209. // if (!this.getConditionResult(item.condition))return false;
  1210. //}
  1211. var show = this.getConditionResult( item.show );
  1212. if( !show )itemNode.setStyle("display","none");
  1213. var available = this.getConditionResult(item.condition);
  1214. if (item.text) {
  1215. var text = this.getExecuteResult( item.text );
  1216. //var text = item.text;
  1217. itemNode.set("text", this.view.lp && this.view.lp[text] ? this.view.lp[text] : text);
  1218. }
  1219. if (item.title) {
  1220. var title = this.getExecuteResult( item.title );
  1221. //var title = item.title;
  1222. itemNode.set("title", this.view.lp && this.view.lp[title] ? this.view.lp[title] : title);
  1223. }
  1224. if ( !item.text && item.value && item.value != "") {
  1225. if( item.type == "html" ){
  1226. itemNode.set("html", this.getValue(item.value));
  1227. }else{
  1228. itemNode.set("text", this.getValue(item.value));
  1229. }
  1230. }
  1231. if( item.styles || item.mouseoverStyles || item.mousedownStyles || item.icon || item.mouseoverIcon || item.mousedownIcon ){
  1232. this.view.setEventStyle( itemNode, item, this, this.data );
  1233. }
  1234. if (item.action && this[item.action]) {
  1235. if ( available ){
  1236. itemNode.addEvent("click", function (ev) {
  1237. this.fun.call(_self, this.node, ev);
  1238. ev.stopPropagation();
  1239. }.bind({fun: this[item.action], node : itemNode}))
  1240. }else{
  1241. return false;
  1242. }
  1243. }
  1244. if( item.event && available ){
  1245. this.bindEvent( itemNode, item.event );
  1246. }
  1247. if( item.attr ){
  1248. this.setAttr( itemNode, item.attr );
  1249. }
  1250. if ( name == "$checkbox" ) {
  1251. if ( available ){
  1252. this.checkboxElement = new Element("input", {
  1253. "type": "checkbox"
  1254. }).inject(itemNode);
  1255. this.checkboxElement.addEvent("click", function (ev) {
  1256. ev.stopPropagation();
  1257. }.bind(this));
  1258. itemNode.addEvent("click", function (ev) {
  1259. this.checkboxElement.set("checked", !this.checkboxElement.get("checked"));
  1260. ev.stopPropagation();
  1261. }.bind(this))
  1262. }else{
  1263. //return false;
  1264. }
  1265. }
  1266. return true;
  1267. },
  1268. getExecuteResult : function( str ){
  1269. var result = str;
  1270. if (str && str.substr(0, 8) == "function") { //"function".length
  1271. eval("var fun = " + str);
  1272. result = fun.call(this, this.data);
  1273. }
  1274. return result;
  1275. },
  1276. getValue: function (str) {
  1277. if (str.substr(0, 8 ) == "function") { //"function".length
  1278. eval("var fun = " + str);
  1279. return fun.call(this, this.data);
  1280. } else if (typeOf(this.data[str]) == "number") {
  1281. return this.data[str];
  1282. } else {
  1283. return this.data[str] ? this.data[str] : "";
  1284. }
  1285. },
  1286. getConditionResult: function (str) {
  1287. var flag = true;
  1288. if (str && str.substr(0, 8) == "function") { //"function".length
  1289. eval("var fun = " + str);
  1290. flag = fun.call(this, this.data);
  1291. }
  1292. return flag;
  1293. },
  1294. setAttr: function(item, attr){
  1295. if( !attr || attr == "" || attr == "$none" )return;
  1296. if( typeof attr == "string" ){
  1297. if( attr.indexOf("^^") > -1 ){
  1298. var attrsArr = attr.split("##");
  1299. if( attrsArr[0].split("^^").length != 2 )return;
  1300. attrs = {};
  1301. for(var i=0;i<attrsArr.length;i++){
  1302. var aname = attrsArr[i].split("^^")[0];
  1303. var afunction = attrsArr[i].split("^^")[1];
  1304. if( afunction.substr(0, "function".length) == "function" ){
  1305. eval("var fun = " + afunction );
  1306. attrs[ aname ] = fun.call(this, this.data); //字符串变对象或function,方法1
  1307. }else{
  1308. attrs[ aname ] = afunction;
  1309. }
  1310. }
  1311. }else{
  1312. //字符串变对象或function,方法2
  1313. eval( "var attrs = " + attr );
  1314. }
  1315. }
  1316. if( typeOf(attrs) == "object" ){
  1317. for( var a in attrs ){
  1318. item.set( a, attrs[a] );
  1319. }
  1320. }
  1321. },
  1322. bindEvent: function(item,events){
  1323. if( !events || events == "" || events == "$none" )return;
  1324. if( typeof events == "string" ){
  1325. if( events.indexOf("^^") > -1 ){
  1326. var eventsArr = events.split("##");
  1327. if( eventsArr[0].split("^^").length != 2 )return;
  1328. events = {};
  1329. for(var i=0;i<eventsArr.length;i++){
  1330. var ename = eventsArr[i].split("^^")[0];
  1331. var efunction = eventsArr[i].split("^^")[1];
  1332. events[ ename ] = eval( "(function(){ return "+ efunction +" })()" ); //字符串变对象或function,方法1
  1333. }
  1334. }else{
  1335. //字符串变对象或function,方法2
  1336. eval( "var events = " + events );
  1337. }
  1338. }
  1339. if( typeOf(events) == "object" ){
  1340. for( var e in events ){
  1341. item.addEvent( e, function(ev){
  1342. this.fun.call( this.bingObj, this.target, ev );
  1343. ev.stopPropagation();
  1344. }.bind({bingObj : this, target: item, fun : events[e]}));
  1345. }
  1346. }
  1347. },
  1348. _getItemAccess: function (item) {
  1349. if (item.access && !this.explorer.options[item.access]) {
  1350. return false;
  1351. } else {
  1352. return true;
  1353. }
  1354. },
  1355. _getActionAccess: function (actionData) {
  1356. return true;
  1357. },
  1358. open: function (e) {
  1359. //var options = {"documentId": this.data.id }//this.explorer.app.options.application.allowControl};
  1360. //this.explorer.app.desktop.openApplication(e, "cms.Document", options);
  1361. this.view._openDocument(this.data);
  1362. },
  1363. remove: function (e) {
  1364. var lp = this.app.lp;
  1365. var text = lp.deleteDocument.replace(/{title}/g, this.data.title);
  1366. var _self = this;
  1367. this.node.setStyles(this.css.documentNode_remove);
  1368. this.readyRemove = true;
  1369. this.view.lockNodeStyle = true;
  1370. //this.explorer.app.confirm("warn", e, lp.deleteDocumentTitle, text, 350, 120, function () {
  1371. this.app.confirm("warn", e, lp.deleteDocumentTitle, text, 350, 120, function () {
  1372. //var inputs = this.content.getElements("input");
  1373. //var flag = "";
  1374. //for (var i=0; i<inputs.length; i++){
  1375. // if (inputs[i].checked){
  1376. // flag = inputs[i].get("value");
  1377. // break;
  1378. // }
  1379. //}
  1380. //if (flag){
  1381. //if (flag=="all"){
  1382. //_self.explorer.removeDocument(_self, true);
  1383. //}else{
  1384. _self.view._removeDocument(_self.data, false);
  1385. _self.view.lockNodeStyle = false;
  1386. //}
  1387. this.close();
  1388. //}else{
  1389. // this.content.getElement("#deleteDocument_checkInfor").set("text", lp.deleteAllDocumentCheck).setStyle("color", "red");
  1390. //}
  1391. }, function () {
  1392. _self.node.setStyles(_self.css.documentNode);
  1393. _self.readyRemove = false;
  1394. _self.view.lockNodeStyle = false;
  1395. this.close();
  1396. });
  1397. },
  1398. destroy: function () {
  1399. this.node.destroy();
  1400. delete this;
  1401. },
  1402. _queryCreateDocumentNode:function( itemData ){
  1403. },
  1404. _postCreateDocumentNode: function( itemNode, itemData ){
  1405. }
  1406. });