Office.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. MWF.xDesktop.requireApp("process.Xform", "Office", null, false);
  2. MWF.xApplication.cms.Xform.Office = MWF.CMSOffice = new Class({
  3. Extends: MWF.APPOffice,
  4. getFormId: function(){
  5. var id = this.form.businessData.document.id;
  6. return "form"+this.json.id+id;
  7. },
  8. getFileName: function(){
  9. var ename = "docx";
  10. switch (this.json.officeType){
  11. case "word":
  12. ename = "docx";
  13. break;
  14. case "excel":
  15. ename = "xlsx";
  16. break;
  17. case "ppt":
  18. ename = "pptx";
  19. }
  20. var id = this.form.businessData.document.id;
  21. return "file"+this.json.id+id+"."+ename;
  22. },
  23. getOfficeObjectId: function(){
  24. var id = this.form.businessData.document.id;
  25. return "NTKOOCX"+this.json.id+id;
  26. },
  27. getFileInputName: function(){
  28. var id = this.form.businessData.document.id;
  29. return "fileInput"+this.json.id+id;
  30. },
  31. getFile: function(site){
  32. var file = null;
  33. atts = this.form.businessData.attachmentList || [];
  34. for (var i=0; i<atts.length; i++){
  35. //if ((atts[i].name===fileName) || (atts[i].site===this.json.id)){
  36. //if (atts[i].site===this.json.id){
  37. if (atts[i].site===site){
  38. file = atts[i];
  39. break;
  40. }
  41. }
  42. return file
  43. },
  44. getTempleteUrl: function(){
  45. //return "../x_desktop/temp/杭州城管委文件.doc";
  46. if (this.json.template){
  47. var root = "";
  48. var flag = this.json.template.substr(0,1);
  49. if (flag==="/"){
  50. root = this.json.template.substr(1, this.json.template.indexOf("/", 1)-1);
  51. }else{
  52. root = this.json.template.substr(0, this.json.template.indexOf("/"));
  53. }
  54. if (["x_cms_assemble_control"].indexOf(root.toLowerCase())!==-1){
  55. var host = MWF.Actions.getHost(root);
  56. return (flag==="/") ? host+this.json.template : host+"/"+this.json.template
  57. }
  58. }
  59. return this.json.template;
  60. },
  61. getOfficeFileUrl: function(){
  62. var fileName = this.getFileName();
  63. this.readSite = this.json.id;
  64. if (this.json.fileSite && this.json.fileSite.code){
  65. this.readSite = this.form.Macro.exec(this.json.fileSite.code, this);
  66. }
  67. var file = this.getFile(this.readSite);
  68. if (!file) if (this.readSite !== this.json.id) file = this.getFile(this.json.id);
  69. if (file){
  70. this.file = file;
  71. var url = "";
  72. url = this.form.documentAction.action.actions.getAttachmentData.uri;
  73. url = url.replace("{id}", encodeURIComponent(file.id));
  74. return this.form.documentAction.action.address+url.replace("{documentid}", encodeURIComponent(this.form.businessData.document.id));
  75. }else{
  76. return this.getTempleteUrl();
  77. }
  78. },
  79. createMenuAction: function(id, title, img){
  80. var title = title || MWF.xApplication.process.Xform.LP[id];
  81. return new Element("div", {
  82. "MWFnodeid": id,
  83. "MWFnodetype": "MWFToolBarButton",
  84. "MWFButtonImage": this.form.path+""+this.form.options.style+"/actionbar/"+img,
  85. "title": title,
  86. "MWFButtonAction": "menuAction",
  87. "MWFButtonText": title
  88. }).inject(this.menuNode);
  89. },
  90. createMenuActionMenu: function(id, title, img){
  91. var title = title || MWF.xApplication.process.Xform.LP[id];
  92. return new Element("div", {
  93. "MWFnodeid": id,
  94. "MWFnodetype": "MWFToolBarMenu",
  95. "MWFButtonImage": this.form.path+""+this.form.options.style+"/actionbar/"+img,
  96. "title": title,
  97. "MWFButtonAction": "menuAction",
  98. "MWFButtonText": title
  99. }).inject(this.menuNode);
  100. },
  101. createMenuActionMenuItem: function(id, title, img, action){
  102. return new Element("div", {
  103. "MWFnodeid": id,
  104. "MWFnodetype": "MWFToolBarMenuItem",
  105. "MWFButtonImage": this.form.path+""+this.form.options.style+"/actionbar/"+img,
  106. "title": title,
  107. "MWFButtonAction": action,
  108. "MWFButtonText": title
  109. }).inject(this.menuNode);
  110. },
  111. openAttachment: function(id, site, name){
  112. if (!this.openedAttachment || this.openedAttachment.id!==id){
  113. this.save();
  114. MWF.Actions.get("x_cms_assemble_control").getAttachmentUrl(id, this.form.businessData.document.id, function(url){
  115. this.openedAttachment = {"id": id, "site": site, "name": name};
  116. this.officeOCX.BeginOpenFromURL(url, true, this.readonly);
  117. }.bind(this));
  118. }
  119. },
  120. openOfficeHistory: function(e, dlg){
  121. var fileName = e.target.getParent().get("value");
  122. url = this.form.documentAction.action.actions.getAttachmentData.uri;
  123. url = url.replace("{id}", encodeURIComponent(fileName));
  124. url = this.form.documentAction.action.address+url.replace("{documentid}", encodeURIComponent(this.form.businessData.document.id));
  125. dlg.close();
  126. this.save();
  127. this.officeOCX.BeginOpenFromURL(url, true, true);
  128. this.historyMode = true;
  129. if (button){
  130. button.setText(MWF.xApplication.process.Xform.LP.menu_hideHistory)
  131. }
  132. },
  133. save: function(history){
  134. //if (!this.uploadFileAreaNode) this.createUploadFileNode();
  135. if (!this.readonly){
  136. if (this.historyMode) return true;
  137. if (!this.officeForm) return true;
  138. this.fireEvent("beforeSave");
  139. try{
  140. if (this.openedAttachment){
  141. this.officeForm.getElement("input").set("value", this.openedAttachment.site);
  142. url = this.form.documentAction.action.actions.replaceAttachment.uri;
  143. url = url.replace("{id}", this.openedAttachment.id);
  144. url = this.form.documentAction.action.address+url.replace("{documentid}", this.form.businessData.document.id);
  145. this.officeOCX.SaveToURL(url, "file", "", this.openedAttachment.name, this.getFormId());
  146. }else{
  147. if (history){
  148. if (this.json.isHistory) this.saveHistory();
  149. }
  150. //this.saveHTML();
  151. this.officeForm.getElement("input").set("value", this.json.id);
  152. var url = "";
  153. if (this.file){
  154. url = this.form.documentAction.action.actions.replaceAttachment.uri;
  155. url = url.replace("{id}", this.file.id);
  156. url = this.form.documentAction.action.address+url.replace("{documentid}", this.form.businessData.document.id);
  157. this.officeOCX.SaveToURL(url, "file", "", this.getFileName(), this.getFormId());
  158. }else{
  159. url = this.form.documentAction.action.actions.uploadAttachment.uri;
  160. url = this.form.documentAction.action.address+url.replace("{id}", this.form.businessData.document.id);
  161. this.officeOCX.SaveToURL(url, "file", "", this.getFileName(), this.getFormId());
  162. this.form.documentAction.getDocument(this.form.businessData.document.id, function(json){
  163. this.form.businessData.attachmentList = json.data.attachmentList;
  164. this.getOfficeFileUrl();
  165. }.bind(this));
  166. }
  167. }
  168. }catch (e){}
  169. this.fireEvent("afterSave");
  170. }
  171. },
  172. getHistoryFileName: function(){
  173. var ename = "docx";
  174. switch (this.json.officeType){
  175. case "word":
  176. ename = "docx";
  177. break;
  178. case "excel":
  179. ename = "xlsx";
  180. break;
  181. case "ppt":
  182. ename = "pptx";
  183. }
  184. //var id = (this.form.businessData.work) ? this.form.businessData.work.id : this.form.businessData.workCompleted.id;
  185. var activity = "";
  186. var name = MWF.name.cn(layout.session.user.name);
  187. var d = Date.parse(new Date());
  188. var dText = d.format("%Y-%m-%d %H:%M");
  189. return activity+"("+name+")-"+dText+"."+ename;
  190. },
  191. saveHistory: function(){
  192. var fileName = this.getHistoryFileName();
  193. this.officeForm.getElement("input").set("value", this.json.id+"history");
  194. url = this.form.documentAction.action.actions.uploadAttachment.uri;
  195. url = this.form.documentAction.action.address+url.replace("{documentid}", this.form.businessData.document.id);
  196. this.officeOCX.SaveToURL(url, "file", "", fileName, this.getFormId());
  197. },
  198. getHTMLFileName: function(){
  199. //var id = (this.form.businessData.work) ? this.form.businessData.work.id : this.form.businessData.workCompleted.id;
  200. var id = this.form.businessData.document.id;
  201. return id+this.json.id+".mht";
  202. },
  203. saveHTML: function(){
  204. this.officeForm.getElement("input").set("value", this.json.id+"$view");
  205. var file = null;
  206. for (var i=0; i<this.form.businessData.attachmentList.length; i++){
  207. var att = this.form.businessData.attachmentList[i];
  208. if (att.site==this.json.id+"$view"){
  209. file = att;
  210. }
  211. }
  212. var fileName = (file) ? file.name : this.getHTMLFileName();
  213. this.officeForm.getElement("input").getNext().set("value", fileName);
  214. if (file){
  215. url = this.form.documentAction.action.actions.replaceAttachment.uri;
  216. url = url.replace("{id}", file.id);
  217. url = this.form.documentAction.action.address+url.replace("{documentid}", this.form.businessData.document.id);
  218. }else{
  219. url = this.form.documentAction.action.actions.uploadAttachment.uri;
  220. url = this.form.documentAction.action.address+url.replace("{id}", this.form.businessData.document.id);
  221. }
  222. //this.officeOCX.PublishAsHTMLToURL(url, "file", "", fileName, this.getFormId());
  223. this.officeOCX.SaveAsOtherFormatToURL(1, url, "file", "", fileName, this.getFormId());
  224. //this.officeOCX.PublishAsPDFToURL(url, "file", "", fileName, this.getFormId());
  225. },
  226. getHTMLFileUrl: function(name){
  227. var fileName = name || this.getHTMLFileName();
  228. var file = null;
  229. atts = this.form.businessData.attachmentList;
  230. for (var i=0; i<atts.length; i++){
  231. if ((atts[i].name===fileName) || (atts[i].site===this.json.id+"$view")){
  232. file = atts[i];
  233. break;
  234. }
  235. }
  236. if (file){
  237. //this.file = file;
  238. var url = "";
  239. url = this.form.documentAction.action.actions.getAttachmentData.uri;
  240. url = url.replace("{id}", encodeURIComponent(file.id));
  241. return this.form.documentAction.action.address+url.replace("{documentid}", encodeURIComponent(this.form.businessData.document.id));
  242. }else{
  243. return this.getTempleteUrl();
  244. }
  245. },
  246. validationMode: function(){},
  247. validation: function(){return true},
  248. loadOfficeNotActive: function(){
  249. var fileName = this.getFileName();
  250. var htmlName = "";
  251. var isHtml = false;
  252. for (var i=0; i<this.form.businessData.attachmentList.length; i++){
  253. var att = this.form.businessData.attachmentList[i];
  254. if (att.site==this.json.id+"$view"){
  255. htmlName = att.name;
  256. }
  257. }
  258. if (false){
  259. this.node.setStyles({
  260. "min-height": "600px",
  261. "padding": "0px",
  262. "border": "0px solid #999999",
  263. "background-color": "#e6e6e6",
  264. "overflow": "hidden"
  265. });
  266. if (this.node.getSize().y<800) this.node.setStyle("height", "800px");
  267. //this.node.setStyles(this.json.styles);
  268. var wordNode = new Element("div", {
  269. "styles": {
  270. "padding": "40px",
  271. "border": "1px solid #999999",
  272. "background-color": "#e6e6e6",
  273. "overflow": "auto"
  274. }
  275. }).inject(this.node);
  276. var size = this.node.getSize();
  277. var y = (size.y-80-80);
  278. wordNode.setStyle("height", ""+y+"px");
  279. var node = new Element("div", {
  280. "styles": {
  281. "width": "90%",
  282. "height": "1900px",
  283. "margin": "auto",
  284. "background-color": "#ffffff"
  285. }
  286. }).inject(wordNode);
  287. var iframe = new Element("iframe", {
  288. "styles": {
  289. "width": "100%",
  290. "height": "100%",
  291. "min-height": "600px",
  292. "overflow": "auto",
  293. "border": "1px solid #cccccc"
  294. }
  295. //"src": this.getHTMLFileUrl(htmlName)
  296. }).inject(node);
  297. //alert(iframe.contentWindow.document.body.firstChild);
  298. iframe.contentWindow.document.addEventListener("readystatechange", function(){
  299. this.body.style.padding = "20px 40px";
  300. });
  301. // iframe.contentWindow.document.onreadystatechange = function(){
  302. // alert("onreadystatechange"+ this.readyState );
  303. // alert(this.body.firstChild);
  304. // this.body.style.padding = "20px 40px";
  305. // };
  306. iframe.set("src", this.getHTMLFileUrl(htmlName));
  307. // iframe.contentWindow.document.body.firstChild.style.paddingTop = "20px";
  308. // iframe.contentWindow.document.body.firstChild.style.paddingBottom = "20px";
  309. // iframe.contentWindow.document.body.firstChild.style.paddingLeft = "40px";
  310. // iframe.contentWindow.document.body.firstChild.style.paddingRight = "40px";
  311. }else{
  312. this.node.setStyles({
  313. "overflow": "hidden",
  314. "background-color": "#f3f3f3",
  315. "min-height": "24px",
  316. "padding": "18px"
  317. });
  318. var str = this.getData();
  319. if (layout.mobile || COMMON.Browser.Platform.isMobile){
  320. if (str.length>300) str = str.substr(0,300)+"……";
  321. }
  322. var text = new Element("div", {
  323. "text": str
  324. }).inject(this.node);
  325. }
  326. var text = MWF.xApplication.process.Xform.LP.openOfficeInfor;
  327. text = text.replace("{type}", this.json.officeType);
  328. var icon = new Element("div", {
  329. "styles": {
  330. "width": "200px",
  331. "height": "24px",
  332. "margin": "auto",
  333. "margin-top": "18px",
  334. "padding-left": "30px",
  335. "font-size": "16px",
  336. "font-weight": "bold",
  337. "color": "#2b5797",
  338. "font-family": "Gadugi",
  339. "cursor": "pointer",
  340. "background": "url("+this.form.path+""+this.form.options.style+"/icon/"+this.json.officeType+".png"+") no-repeat left center"
  341. },
  342. "text": text
  343. }).inject(this.node);
  344. var url = this.getOfficeFileUrl();
  345. if (!url){
  346. this.node.setStyle("display", "none");
  347. }
  348. icon.addEvent("click", function(){
  349. var url = this.getOfficeFileUrl();
  350. if (url){
  351. if (window.o2android){
  352. window.o2android.openDocument(url);
  353. }else if(window.webkit){
  354. window.webkit.messageHandlers.openDocument.postMessage(url);
  355. }else{
  356. window.open(o2.filterUrl(url));
  357. }
  358. }
  359. }.bind(this));
  360. }
  361. });