OnlyOffice.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. MWF.xAction.RestActions.Action["x_onlyofficefile_assemble_control"] = new Class({
  2. Extends: MWF.xAction.RestActions.Action
  3. });
  4. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  5. MWF.xApplication.process.Xform.OnlyOffice = MWF.APPOnlyOffice = new Class({
  6. Extends: MWF.APP$Module,
  7. isActive: false,
  8. options:{
  9. "moduleEvents": ["queryLoad","beforeOpen",
  10. "afterOpen",
  11. "afterSave"
  12. ]
  13. },
  14. initialize: function(node, json, form, options){
  15. this.node = $(node);
  16. this.node.store("module", this);
  17. this.json = json;
  18. this.form = form;
  19. this.documentId = "";
  20. this.mode = "edit";
  21. },
  22. _loadUserInterface: function(){
  23. this.node.empty();
  24. this.node.setStyles({
  25. "min-height": "100px"
  26. });
  27. },
  28. _afterLoaded: function(){
  29. this.fireEvent("queryLoad");
  30. if(!layout.serviceAddressList["x_onlyofficefile_assemble_control"]){
  31. this.node.set("html","<h3><font color=red>"+MWF.xApplication.process.Xform.LP.onlyoffice.noInstall+"</font></h3>");
  32. return false;
  33. }
  34. if(this.mode !== "read" && this.json.allowUpload){
  35. this.createUpload();
  36. }
  37. this.action = o2.Actions.load("x_onlyofficefile_assemble_control");
  38. if (!this.json.isNotLoadNow){
  39. this.data = this.getData();
  40. if(this.data.documentId === ""){
  41. if (this.json.officeType === "other" && this.json.templateType === "script"){
  42. this.json.template = this.form.Macro.exec(this.json.templeteScript.code, this);
  43. }
  44. this[this.json.officeType === "other"&&this.json.template !== ""? "createDocumentByTemplate":"createDocument"](function (){
  45. this.loadDocument();
  46. }.bind(this));
  47. }else {
  48. this.documentId = this.data.documentId;
  49. this.loadDocument();
  50. }
  51. }
  52. },
  53. getFileName : function (){
  54. if (this.json.fileNameType === "value"){
  55. return this.json.fileName;
  56. }else if(this.json.fileNameType === "script"){
  57. return this.form.Macro.exec(this.json.fileNameScript.code, this);
  58. }else{
  59. return MWF.xApplication.process.Xform.LP.onlyoffice.filetext;
  60. }
  61. },
  62. createDocument : function (callback){
  63. var data = {
  64. "fileName" : this.getFileName() + "." + this.json.officeType,
  65. "fileType" : this.json.officeType,
  66. "appToken" : "x_processplatform_assemble_surface",
  67. "workId" : this.form.businessData.work.id,
  68. "site" : this.json.fileSite?this.json.fileSite:"filetext"
  69. };
  70. this.action.OnlyofficeAction.createForO2(data,
  71. function( json ){
  72. this.documentId = json.data.fileId;
  73. this.setData();
  74. if (callback) callback();
  75. }.bind(this),null, false
  76. );
  77. },
  78. createDocumentByTemplate : function (callback){
  79. this.action.OnlyofficeAction.get(this.json.template).then(function(json) {
  80. var data = {
  81. "fileName": this.getFileName() + "." + json.data.fileModel.document.fileType,
  82. "fileType": json.data.fileModel.document.fileType,
  83. "appToken" : "x_processplatform_assemble_surface",
  84. "workId" : this.form.businessData.work.id,
  85. "site" : this.json.fileSite?this.json.fileSite:"filetext",
  86. "tempId": this.json.template
  87. };
  88. this.action.OnlyofficeAction.createForO2(data,
  89. function( json ){
  90. this.documentId = json.data.fileId;
  91. this.setData();
  92. if (callback) callback();
  93. }.bind(this),null, false
  94. );
  95. }.bind(this))
  96. },
  97. createUpload : function (){
  98. this.uploadNode = new Element("div",{"style":"margin:10px;"}).inject(this.node);
  99. var uploadBtn = new Element("button",{"text":MWF.xApplication.process.Xform.LP.ofdview.upload,"style":"margin-left: 15px; color: rgb(255, 255, 255); cursor: pointer; height: 26px; line-height: 26px; padding: 0px 10px; min-width: 40px; background-color: rgb(74, 144, 226); border: 1px solid rgb(82, 139, 204); border-radius: 15px;"}).inject(this.uploadNode);
  100. uploadBtn.addEvent("click",function (){
  101. o2.require("o2.widget.Upload", null, false);
  102. var upload = new o2.widget.Upload(this.content, {
  103. "action": o2.Actions.get("x_processplatform_assemble_surface").action,
  104. "method": "replaceAttachment",
  105. "accept" : ".docx,.xlsx,.pptx",
  106. "parameter": {
  107. "id" : this.documentId,
  108. "workid" : this.form.businessData.work.id,
  109. },
  110. "data":{
  111. },
  112. "onCompleted": function(data){
  113. this.documentId = data.id;
  114. this.reload();
  115. }.bind(this)
  116. });
  117. upload.load();
  118. }.bind(this));
  119. },
  120. reload : function (){
  121. this.setData();
  122. this.node.empty();
  123. if(this.mode !== "read" && this.json.allowUpload){
  124. this.createUpload();
  125. }
  126. this.loadDocument();
  127. },
  128. loadDocument: function () {
  129. this.getEditor(function () {
  130. this.loadApi(function (){
  131. this.loadEditor();
  132. }.bind(this));
  133. }.bind(this));
  134. },
  135. loadApi : function (callback){
  136. this.action.OnlyofficeConfigAction.getConfig(function( json ){
  137. var data = json.data;
  138. var docserviceApi = data.docserviceApi;
  139. o2.load(docserviceApi, function () {
  140. if (callback) callback();
  141. }.bind(this));
  142. }.bind(this),null, false);
  143. },
  144. getEditor: function (callback) {
  145. if (this.isReadonly()){
  146. this.mode = "view";
  147. }else{
  148. if (this.json.readScript && this.json.readScript.code){
  149. var flag = this.form.Macro.exec(this.json.readScript.code, this);
  150. if (flag){
  151. this.mode = "view";
  152. }
  153. }
  154. }
  155. if(this.data.appToken){
  156. this.action.OnlyofficeAction.appFileEdit({
  157. "appToken" : this.data.appToken,
  158. "mode" : this.mode,
  159. "fileId" : this.documentId
  160. }, function( json ){
  161. this.document = json.data;
  162. this.document.editor = this.document.fileModel;
  163. if (callback) callback();
  164. }.bind(this),null,false);
  165. }else{
  166. this.action.OnlyofficeAction.get(this.documentId, function( json ){
  167. this.document = json.data;
  168. this.document.editor = this.document.fileModel;
  169. if (callback) callback();
  170. }.bind(this),null,false);
  171. }
  172. },
  173. loadEditor: function () {
  174. this.fireEvent("beforeOpen");
  175. var docEditor;
  176. var _self = this;
  177. var innerAlert = function (message) {
  178. if (console && console.log)
  179. console.log(message);
  180. };
  181. var onAppReady = function () {
  182. innerAlert("Document editor ready");
  183. };
  184. var onDocumentStateChange = function (event) {
  185. var title = document.title.replace(/\*$/g, "");
  186. document.title = title + (event.data ? "*" : "");
  187. if(event.data){
  188. _self.fireEvent("afterSave");
  189. }
  190. };
  191. var onRequestEditRights = function () {
  192. location.href = location.href.replace(RegExp("mode=view\&?", "i"), "");
  193. };
  194. var onRequestHistory = function (event) {
  195. if (this.document.FileHistory[0] === "") {
  196. docEditor.refreshHistory({
  197. currentVersion: null,
  198. history: null
  199. });
  200. } else {
  201. var historyArr = JSON.parse(this.document.FileHistory[0]).history;
  202. var newHistoryArr = [];
  203. for (var i = 0; i < historyArr.length; i++) {
  204. if (historyArr[i].version > 0) {
  205. newHistoryArr.push(historyArr[i]);
  206. }
  207. }
  208. newHistoryArr.sort(function (a, b) {
  209. return a.version - b.version;
  210. });
  211. var historyObj = newHistoryArr || null;
  212. docEditor.refreshHistory({
  213. currentVersion: JSON.parse(this.document.FileHistory[0]).currentVersion,
  214. history: historyObj
  215. });
  216. }
  217. }.bind(this);
  218. var onRequestHistoryData = function (data) {
  219. var historyArr = [];
  220. var history = JSON.parse(this.document.FileHistory[1]);
  221. for (var key in history) {
  222. if (key !== "0") {
  223. historyArr.push(history[key]);
  224. }
  225. }
  226. var version = data.data;
  227. var historyData = historyArr || null;
  228. docEditor.setHistoryData(historyData[version - 1]);
  229. }.bind(this);
  230. var onRequestHistoryClose = function (event) {
  231. //document.location.reload();
  232. _self.reload();
  233. };
  234. var onError = function (event) {
  235. if (event) innerAlert(event.data);
  236. };
  237. var onOutdatedVersion = function (event) {
  238. location.reload(true);
  239. };
  240. var onDocumentReady= function() {
  241. console.log("Document is loaded");
  242. }.bind(this);
  243. var onPluginsReady= function() {
  244. console.log("Plugins is loaded");
  245. this.fireEvent("afterOpen");
  246. }.bind(this);
  247. this.document.editor.events = {
  248. "onAppReady": onAppReady,
  249. "onDocumentReady":onDocumentReady,
  250. "onPluginsReady":onPluginsReady,
  251. "onDocumentStateChange": onDocumentStateChange,
  252. 'onRequestEditRights': onRequestEditRights,
  253. "onError": onError,
  254. "onOutdatedVersion": onOutdatedVersion,
  255. }
  256. if (this.document.FileHistory[0] !== "") {
  257. this.document.editor.events.onRequestHistory = onRequestHistory;
  258. this.document.editor.events.onRequestHistoryData = onRequestHistoryData;
  259. this.document.editor.events.onRequestHistoryClose = onRequestHistoryClose;
  260. }
  261. if(layout.mobile){
  262. this.document.editor.type = "mobile";
  263. }
  264. this.officeNode = new Element("div#_" + this.documentId, {
  265. "styles": this.form.css.officeAreaNode
  266. }).inject(this.node);
  267. if (this.node.getSize().y<800) this.node.setStyle("height", "800px");
  268. this.document.editor.editorConfig.mode = this.mode;
  269. var lang = layout.session.user.language;
  270. if(lang != "en"){
  271. lang = "zh";
  272. }
  273. this.document.editor.editorConfig.lang = "zh";
  274. this.document.editor.editorConfig.location = "zh-CN";
  275. this.document.editor.editorConfig.region = "zh-CN";
  276. this.document.editor.editorConfig.customization = {
  277. "chat": this.json.chat,
  278. "commentAuthorOnly": false,
  279. "comments": this.json.comments,
  280. "autosave" : this.json.autosave,
  281. "compactHeader": this.json.compactHeader,
  282. "compactToolbar": this.json.compactToolbar,
  283. "compatibleFeatures": this.json.compatibleFeatures,
  284. "customer": {
  285. "address": this.json.address,
  286. "info": this.json.info,
  287. "logo": this.json.logo,
  288. "mail": this.json.mail,
  289. "name": this.json.companyName,
  290. "www": this.json.www
  291. },
  292. "feedback": {
  293. "url": this.json.feedback,
  294. "visible": this.json.feedbackUrl
  295. },
  296. "forcesave": this.json.forcesave,
  297. "help": this.json.help,
  298. "hideRightMenu": this.json.hideRightMenu,
  299. "logo": {
  300. "image": this.json.logoImg,
  301. "url": this.json.logoUrl
  302. },
  303. "macros": true,
  304. "macrosMode": "warn",
  305. "mentionShare": true,
  306. "plugins": this.json.plugins,
  307. "reviewDisplay": this.json.reviewDisplay,
  308. "showReviewChanges": this.json.showReviewChanges,
  309. "spellcheck": this.json.spellcheck,
  310. "toolbarHideFileName": this.json.toolbarHideFileName,
  311. "toolbarNoTabs": this.json.toolbarNoTabs,
  312. "trackChanges": this.json.trackChanges,
  313. "unit": this.json.unit,
  314. "zoom": this.json.zoom,
  315. "review" : {
  316. "trackChanges" : this.json.trackChanges,
  317. "reviewDisplay" : this.json.reviewDisplay,
  318. "showReviewChanges" : this.json.showReviewChanges
  319. }
  320. }
  321. docEditor = new DocsAPI.DocEditor("_" + this.documentId, this.document.editor);
  322. this.onlyOffice = docEditor;
  323. },
  324. hide: function(){
  325. this.node.hide();
  326. },
  327. show: function(){
  328. this.node.show();
  329. },
  330. isEmpty : function(){
  331. var data = this.getData();
  332. if(data.documentId === ""){
  333. return true;
  334. }else {
  335. return false;
  336. }
  337. },
  338. getData: function(){
  339. var data = {
  340. "documentId" : ""
  341. };
  342. var site = this.json.fileSite?this.json.fileSite:"filetext";
  343. if(this.form.businessData.data[this.json.id] && this.form.businessData.data[this.json.id].documentId){
  344. data = this.form.businessData.data[this.json.id];
  345. }else {
  346. //判断对应的site里有没有值
  347. var attachmentList = this.form.businessData.attachmentList;
  348. attachmentList = attachmentList.filter(function(att) {
  349. return att.site === site;
  350. });
  351. if(attachmentList.length>0){
  352. data = {
  353. "documentId": attachmentList[0].id,
  354. "appToken": "x_processplatform_assemble_surface"
  355. };
  356. }
  357. }
  358. return data;
  359. },
  360. setData: function() {
  361. var data = {
  362. "documentId": this.documentId,
  363. "appToken": "x_processplatform_assemble_surface"
  364. }
  365. this.data = data;
  366. this._setBusinessData(data);
  367. var jsonData = {}
  368. jsonData[this.json.id] = data;
  369. o2.Actions.load("x_processplatform_assemble_surface").DataAction.updateWithJob(this.form.businessData.work.job, jsonData, function (json) {
  370. data = json.data;
  371. })
  372. },
  373. setBookmark : function (name , value){
  374. var connector = this.onlyOffice.createConnector();
  375. Asc.scope.name = name;
  376. Asc.scope.text = value;
  377. connector.callCommand(function() {
  378. var oDocument = Api.GetDocument();
  379. var oRange = oDocument.GetBookmarkRange(Asc.scope.name);
  380. var oRangeParagraph = oRange.GetParagraph(0);
  381. var aSearch = oRangeParagraph.Search(oRange.GetText());
  382. try {
  383. oRange.AddText(Asc.scope.text, 'after');
  384. aSearch[0].Delete();
  385. } catch (err) {
  386. oRange.AddText(Asc.scope.text, 'before');
  387. aSearch = oRangeParagraph.Search(oRange.GetText());
  388. aSearch[0].AddBookmark(Asc.scope.name);
  389. }
  390. }, function() { console.log("callback command"); });
  391. connector.disconnect();
  392. },
  393. save : function (){
  394. var connector = this.onlyOffice.createConnector();
  395. connector.callCommand(function() {
  396. Api.Save();
  397. }, function() { console.log("callback command"); });
  398. connector.disconnect();
  399. },
  400. startRevisions : function (){
  401. //开启修订
  402. var connector = this.onlyOffice.createConnector();
  403. connector.callCommand(function() {
  404. var oDocument = Api.GetDocument();
  405. oDocument.SetTrackRevisions(true);
  406. }, function() { console.log("callback command"); });
  407. connector.disconnect();
  408. },
  409. stopRevisions : function (){
  410. //关闭修订
  411. var connector = this.onlyOffice.createConnector();
  412. connector.callCommand(function() {
  413. var oDocument = Api.GetDocument();
  414. oDocument.SetTrackRevisions(false);
  415. }, function() { console.log("callback command"); });
  416. connector.disconnect();
  417. },
  418. acceptAllRevisions : function (){
  419. var connector = this.onlyOffice.createConnector();
  420. connector.callCommand(function() {
  421. var oDocument = Api.GetDocument();
  422. oDocument.AcceptAllRevisionChanges();
  423. }, function() { console.log("callback command"); });
  424. connector.disconnect();
  425. },
  426. rejectAllRevisions : function (){
  427. var connector = this.onlyOffice.createConnector();
  428. connector.callCommand(function() {
  429. var oDocument = Api.GetDocument();
  430. oDocument.RejectAllRevisionChanges();
  431. }, function() { console.log("callback command"); });
  432. connector.disconnect();
  433. },
  434. });