OnlyOffice在线文档

jscript.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**
  2. *
  3. * (c) Copyright Ascensio System SIA 2023
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var directUrl;
  19. if (typeof jQuery !== "undefined") {
  20. jq = jQuery.noConflict();
  21. directUrl = getUrlVars()["directUrl"] == "true";
  22. mustReload = false;
  23. if (directUrl)
  24. jq("#directUrl").prop("checked", directUrl);
  25. else
  26. directUrl = jq("#directUrl").prop("checked");
  27. jq("#directUrl").change(function() {
  28. window.location = "?directUrl=" + jq(this).prop("checked");
  29. });
  30. jq(function () {
  31. jq("#fileupload").fileupload({
  32. dataType: "json",
  33. add: function (e, data) {
  34. jq(".error").removeClass("error");
  35. jq(".done").removeClass("done");
  36. jq(".current").removeClass("current");
  37. jq("#step1").addClass("current");
  38. jq("#mainProgress .error-message").hide().find("span").text("");
  39. jq("#blockPassword").hide();
  40. jq("#mainProgress").removeClass("embedded");
  41. jq("#uploadFileName").text("");
  42. jq.blockUI({
  43. theme: true,
  44. title: "File upload" + "<div class=\"dialog-close\"></div>",
  45. message: jq("#mainProgress"),
  46. overlayCSS: { "background-color": "#aaa" },
  47. themedCSS: { width: "539px", top: "20%", left: "50%", marginLeft: "-269px" }
  48. });
  49. jq("#beginEdit, #beginView, #beginEmbedded").addClass("disable");
  50. data.submit();
  51. },
  52. always: function (e, data) {
  53. if (!jq("#mainProgress").is(":visible")) {
  54. return;
  55. }
  56. var response = data.result;
  57. if (response.error) {
  58. jq(".current").removeClass("current");
  59. jq(".step:not(.done)").addClass("error");
  60. jq("#mainProgress .error-message").show().find("span").text(response.error);
  61. jq('#hiddenFileName').val("");
  62. return;
  63. }
  64. jq("#hiddenFileName").val(response.filename);
  65. jq("#uploadFileName").text(response.filename);
  66. jq("#uploadFileName").addClass(response.documentType);
  67. mustReload = true;
  68. jq("#step1").addClass("done").removeClass("current");
  69. checkConvert();
  70. }
  71. });
  72. initSelectors();
  73. });
  74. var timer = null;
  75. var checkConvert = function (filePass) {
  76. filePass = filePass ? filePass : null;
  77. if (timer !== null) {
  78. clearTimeout(timer);
  79. }
  80. if (!jq("#mainProgress").is(":visible")) {
  81. return;
  82. }
  83. jq("#step2").addClass("current");
  84. jq("#filePass").val("");
  85. var fileName = jq("#hiddenFileName").val();
  86. var posExt = fileName.lastIndexOf(".");
  87. posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : "";
  88. if (ConverExtList.indexOf(posExt) === -1) {
  89. jq("#step2").addClass("done").removeClass("current");
  90. loadScripts();
  91. return;
  92. }
  93. timer = setTimeout(function () {
  94. jq.ajax({
  95. async: true,
  96. contentType: "application/json",
  97. type: "post",
  98. dataType: "json",
  99. data: JSON.stringify({filename: fileName, filePass: filePass}),
  100. url: UrlConverter,
  101. complete: function (data) {
  102. var responseText = data.responseText;
  103. var response = jq.parseJSON(responseText);
  104. if (response.error) {
  105. if (response.error.includes("Incorrect password")) {
  106. jq(".current").removeClass("current");
  107. jq("#step2").addClass("error");
  108. jq("#blockPassword").show();
  109. if (filePass) {
  110. jq("#filePass").addClass("errorInput");
  111. jq(".errorPass").text("The password is incorrect, please try again.");
  112. }
  113. return;
  114. } else {
  115. jq(".current").removeClass("current");
  116. jq(".step:not(.done)").addClass("error");
  117. jq("#mainProgress .error-message").show().find("span").text(response.error);
  118. jq('#hiddenFileName').val("");
  119. return;
  120. }
  121. }
  122. jq("#hiddenFileName").val(response.filename);
  123. if (response.step && response.step < 100) {
  124. checkConvert(filePass);
  125. } else {
  126. jq("#step2").addClass("done").removeClass("current");
  127. loadScripts();
  128. }
  129. }
  130. });
  131. }, 1000);
  132. };
  133. var loadScripts = function () {
  134. if (!jq("#mainProgress").is(":visible")) {
  135. return;
  136. }
  137. jq("#step3").addClass("current");
  138. if (jq("#loadScripts").is(":empty")) {
  139. var urlScripts = jq("#loadScripts").attr("data-docs");
  140. var frame = "<iframe id=\"iframeScripts\" width=1 height=1 style=\"position: absolute; visibility: hidden;\" ></iframe>";
  141. jq("#loadScripts").html(frame);
  142. document.getElementById("iframeScripts").onload = onloadScripts;
  143. jq("#loadScripts iframe").attr("src", urlScripts);
  144. } else {
  145. onloadScripts();
  146. }
  147. };
  148. var onloadScripts = function () {
  149. if (!jq("#mainProgress").is(":visible")) {
  150. return;
  151. }
  152. jq("#step3").addClass("done").removeClass("current");
  153. jq("#beginView, #beginEmbedded").removeClass("disable");
  154. var fileName = jq("#hiddenFileName").val();
  155. var posExt = fileName.lastIndexOf(".");
  156. posExt = 0 <= posExt ? fileName.substring(posExt).trim().toLowerCase() : "";
  157. if (EditedExtList.indexOf(posExt) !== -1 || FillExtList.indexOf(posExt) !== -1) {
  158. jq("#beginEdit").removeClass("disable");
  159. }
  160. };
  161. var initSelectors = function () {
  162. var userSel = jq("#user");
  163. var langSel = jq("#language");
  164. function getCookie(name) {
  165. let matches = document.cookie.match(new RegExp(
  166. "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
  167. ));
  168. return matches ? decodeURIComponent(matches[1]) : null;
  169. }
  170. function setCookie(name, value) {
  171. document.cookie = name + "=" + value + "; expires=" + new Date(Date.now() + 1000 * 60 * 60 * 24 * 7).toUTCString(); //week
  172. }
  173. var userId = getCookie("uid");
  174. if (userId) userSel.val(userId);
  175. var langId = getCookie("ulang");
  176. if (langId) langSel.val(langId);
  177. userSel.on("change", function () {
  178. setCookie("uid", userSel.val());
  179. });
  180. langSel.on("change", function () {
  181. setCookie("ulang", langSel.val());
  182. });
  183. if(!userId){
  184. setCookie("uid",1);
  185. }
  186. if(!langId){
  187. setCookie("ulang","en");
  188. }
  189. };
  190. jq(document).on("click", "#enterPass", function () {
  191. var pass = jq("#filePass").val();
  192. if (pass) {
  193. jq("#step2").removeClass("error");
  194. jq("#blockPassword").hide();
  195. checkConvert(pass);
  196. } else {
  197. jq("#filePass").addClass("errorInput");
  198. jq(".errorPass").text("Password can't be blank.");
  199. }
  200. });
  201. jq(document).on("click", "#skipPass", function () {
  202. jq("#blockPassword").hide();
  203. loadScripts();
  204. });
  205. jq(document).on("click", "#beginEdit:not(.disable)", function () {
  206. var fileId = encodeURIComponent(jq("#hiddenFileName").val());
  207. var url = UrlEditor + "?action=edit&fileName=" + fileId + "&directUrl=" + directUrl;
  208. window.open(url, "_blank");
  209. jq("#hiddenFileName").val("");
  210. jq.unblockUI();
  211. });
  212. jq(document).on("click", "#beginView:not(.disable)", function () {
  213. var fileId = encodeURIComponent(jq("#hiddenFileName").val());
  214. var url = UrlEditor + "?action=view&fileName=" + fileId + "&directUrl=" + directUrl;
  215. window.open(url, "_blank");
  216. jq("#hiddenFileName").val("");
  217. jq.unblockUI();
  218. });
  219. jq(document).on("click", "#beginEmbedded:not(.disable)", function () {
  220. var fileId = encodeURIComponent(jq("#hiddenFileName").val());
  221. var url = UrlEditor + "?type=embedded&action=embedded&fileName=" + fileId + "&directUrl=" + directUrl;
  222. jq("#mainProgress").addClass("embedded");
  223. jq("#beginEmbedded").addClass("disable");
  224. jq("#embeddedView").attr("src", url);
  225. });
  226. jq(document).on("click", "#cancelEdit, .dialog-close", function () {
  227. jq('#hiddenFileName').val("");
  228. jq("#embeddedView").attr("src", "");
  229. jq.unblockUI();
  230. if (mustReload) {
  231. document.location.reload();
  232. }
  233. });
  234. jq(document).on("click", ".try-editor", function (e) {
  235. var url = "/create?fileExt=" + e.target.attributes["data-type"].value;
  236. if (jq("#createSample").is(":checked")) {
  237. url += "&sample=true";
  238. }
  239. var w = window.open(url, "_blank");
  240. w.onload = function () {
  241. window.location.reload();
  242. }
  243. });
  244. jq(document).on("click", ".delete-file", function () {
  245. var fileName = encodeURIComponent(jq(this).attr("data-filename"));
  246. var requestAddress = "/delete";
  247. jq.ajax({
  248. async: true,
  249. contentType: "application/json",
  250. type: "post",
  251. dataType: "json",
  252. data: JSON.stringify({filename: fileName, filePass: null}),
  253. url: requestAddress,
  254. complete: function (data) {
  255. document.location.reload();
  256. }
  257. });
  258. });
  259. function showUserTooltip (isMobile) {
  260. if ( jq("div#portal-info").is(":hidden") ) {
  261. jq("div#portal-info").show();
  262. jq("div.stored-list").hide();
  263. } else if (isMobile && jq("div#portal-info").is(":visible")) {
  264. jq("div#portal-info").hide();
  265. jq("div.stored-list").show();
  266. }
  267. };
  268. function getUrlVars() {
  269. var vars = [], hash;
  270. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  271. for (var i = 0; i < hashes.length; i++) {
  272. hash = hashes[i].split('=');
  273. vars.push(hash[0]);
  274. vars[hash[0]] = hash[1];
  275. }
  276. return vars;
  277. };
  278. jq("#portal-info")[0].innerHTML += jq("#portal-info")[0].attributes.tooltip.value;
  279. var fileList = jq("tr.tableRow");
  280. var mouseIsOverTooltip = false;
  281. var hideTooltipTimeout = null;
  282. if (/android|avantgo|playbook|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\\|plucker|pocket|psp|symbian|treo|up\\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i
  283. .test(navigator.userAgent)) {
  284. if (fileList.length > 0) {
  285. if (hideTooltipTimeout != null) {
  286. clearTimeout(hideTooltipTimeout);
  287. }
  288. jq("#info").on("touchend", function () {
  289. showUserTooltip(true);
  290. });
  291. }
  292. } else {
  293. jq("#info").mouseover(function (event) {
  294. if (fileList.length > 0) {
  295. if (hideTooltipTimeout != null) {
  296. clearTimeout(hideTooltipTimeout);
  297. }
  298. showUserTooltip(false);
  299. jq("div#portal-info").mouseenter(function () {
  300. mouseIsOverTooltip = true;
  301. }).mouseleave(function () {
  302. mouseIsOverTooltip = false;
  303. jq("div.stored-list").show();
  304. jq("div#portal-info").hide();
  305. })
  306. }
  307. }).mouseleave(function () {
  308. hideTooltipTimeout = setTimeout(function () {
  309. if (mouseIsOverTooltip == false && fileList.length > 0) {
  310. jq("div.stored-list").show();
  311. jq("div#portal-info").hide();
  312. }
  313. }, 500);
  314. });
  315. }
  316. jq(".info-tooltip").mouseover(function (event) {
  317. var target = event.target;
  318. var id = target.dataset.id ? target.dataset.id : target.id;
  319. var tooltip = target.dataset.tooltip;
  320. jq("<div class='tooltip'>" + tooltip + "<div class='arrow'></div></div>").appendTo("body");
  321. var top = jq("#" + id).offset().top + jq("#" + id).outerHeight() / 2 - jq("div.tooltip").outerHeight() / 2;
  322. var left = jq("#" + id).offset().left + jq("#" + id).outerWidth() + 20;
  323. jq("div.tooltip").css({"top": top, "left": left});
  324. }).mouseout(function () {
  325. jq("div.tooltip").remove();
  326. });
  327. }