api.js 20.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
!(function (e, t, o, n) {
  function i(e, o) {
    e && e.postMessage && t.JSON && (o.data?.event && (o = JSON.stringify(o)), e.postMessage(o, '*'));
  }
  function s(e, t) {
    for (var o in t)
      t.hasOwnProperty(o) &&
        (void 0 === e[o] ? (e[o] = t[o]) : 'object' == typeof e[o] && 'object' == typeof t[o] && s(e[o], t[o]));
    return e;
  }
  ((e.DocEditor = function (n, d) {
    var r = this,
      a = d || {};
    (s(a, e.DocEditor.defaultConfig),
      (a.editorConfig.canUseHistory = a.events && !!a.events.onRequestHistory),
      (a.editorConfig.canHistoryClose = a.events && !!a.events.onRequestHistoryClose),
      (a.editorConfig.canHistoryRestore = a.events && !!a.events.onRequestRestore),
      (a.editorConfig.canSendEmailAddresses = a.events && !!a.events.onRequestEmailAddresses),
      (a.editorConfig.canRequestEditRights = a.events && !!a.events.onRequestEditRights),
      (a.editorConfig.canRequestClose = a.events && !!a.events.onRequestClose),
      (a.editorConfig.canRename = a.events && !!a.events.onRequestRename),
      (a.editorConfig.canMakeActionLink = a.events && !!a.events.onMakeActionLink),
      (a.editorConfig.canRequestUsers = a.events && !!a.events.onRequestUsers),
      (a.editorConfig.canRequestSendNotify = a.events && !!a.events.onRequestSendNotify),
      (a.editorConfig.mergeFolderUrl = a.editorConfig.mergeFolderUrl || a.editorConfig.saveAsUrl),
      (a.editorConfig.canRequestSaveAs = a.events && !!a.events.onRequestSaveAs),
      (a.editorConfig.canRequestInsertImage = a.events && !!a.events.onRequestInsertImage),
      (a.editorConfig.canRequestMailMergeRecipients = a.events && !!a.events.onRequestMailMergeRecipients),
      (a.editorConfig.canRequestCompareFile = a.events && !!a.events.onRequestCompareFile),
      (a.editorConfig.canRequestSharingSettings = a.events && !!a.events.onRequestSharingSettings),
      (a.editorConfig.canRequestCreateNew = !1),
      (a.editorConfig.canRequestReferenceData = a.events && !!a.events.onRequestReferenceData),
      (a.editorConfig.canRequestOpen = a.events && !!a.events.onRequestOpen),
      (a.editorConfig.canRequestSelectDocument = a.events && !!a.events.onRequestSelectDocument),
      (a.editorConfig.canRequestSelectSpreadsheet = a.events && !!a.events.onRequestSelectSpreadsheet),
      (a.editorConfig.canRequestReferenceSource = a.events && !!a.events.onRequestReferenceSource),
      (a.frameEditorId = n),
      (a.parentOrigin = t.location.origin));
    var c,
      m = function (e) {
        R(e);
      },
      u = function () {
        t.addEventListener ? t.addEventListener('mouseup', m, !1) : t.attachEvent && t.attachEvent('onmouseup', m);
      },
      f = function () {
        t.removeEventListener
          ? t.removeEventListener('mouseup', m, !1)
          : t.detachEvent && t.detachEvent('onmouseup', m);
      };
    (c = /[\?\&]placement=(\w+)&?/.exec(t.location.search)) &&
      c.length &&
      'desktop' == c[1] &&
      ((a.editorConfig.targetApp = c[1]),
      a.editorConfig.customization || (a.editorConfig.customization = {}),
      (a.editorConfig.customization.about = !1),
      (a.editorConfig.customization.compactHeader = !1));
    var p,
      g = o.getElementById(n);
    if (
      g &&
      (function () {
        if (a.document) {
          if (
            !a.document.url ||
            (('string' != typeof a.document.fileType || '' == a.document.fileType) &&
              ('string' != typeof a.documentType || '' == a.documentType))
          )
            return !1;
          var e;
          if (
            (('text' != a.documentType && 'spreadsheet' != a.documentType && 'presentation' != a.documentType) ||
              console.warn(
                'The "documentType" parameter for the config object must take one of the values word/cell/slide.',
              ),
            'string' == typeof a.documentType && '' != a.documentType)
          ) {
            if (
              !(e = {
                text: 'docx',
                'text-pdf': 'pdf',
                spreadsheet: 'xlsx',
                presentation: 'pptx',
                word: 'docx',
                cell: 'xlsx',
                slide: 'pptx',
              }[a.documentType.toLowerCase()])
            )
              return (t.alert('The "documentType" parameter for the config object is invalid. Please correct it.'), !1);
            ('string' == typeof a.document.fileType && '' != a.document.fileType) || (a.document.fileType = e);
          }
          if ('string' == typeof a.document.fileType && '' != a.document.fileType) {
            var o;
            if (
              ((a.document.fileType = a.document.fileType.toLowerCase()),
              !(o =
                /^(?:(xls|xlsx|ods|csv|gsheet|xlsm|xlt|xltm|xltx|fods|ots|xlsb|sxc|et|ett)|(pps|ppsx|ppt|pptx|odp|gslides|pot|potm|potx|ppsm|pptm|fodp|otp|sxi|dps|dpt)|(doc|docx|odt|gdoc|txt|rtf|mht|htm|html|mhtml|epub|docm|dot|dotm|dotx|fodt|ott|fb2|xml|oform|docxf|sxw|stw|wps|wpt|pdf|djvu|xps|oxps))$/.exec(
                  a.document.fileType,
                )))
            )
              return (
                t.alert('The "document.fileType" parameter for the config object is invalid. Please correct it.'),
                !1
              );
            ('string' == typeof a.documentType && '' != a.documentType) ||
              ('string' == typeof o[1]
                ? (a.documentType = 'cell')
                : 'string' == typeof o[2]
                  ? (a.documentType = 'slide')
                  : 'string' == typeof o[3] && (a.documentType = 'word'));
          }
          if (
            ((o = /^(?:(pdf|djvu|xps|oxps))$/.exec(a.document.fileType)) &&
              'string' == typeof o[1] &&
              (a.editorConfig.canUseHistory = !1),
            (a.document.title && '' != a.document.title) || (a.document.title = 'Unnamed.' + a.document.fileType),
            a.document.key)
          ) {
            if ('string' != typeof a.document.key)
              return (
                t.alert('The "document.key" parameter for the config object must be string. Please correct it.'),
                !1
              );
          } else
            a.document.key = 'xxxxxxxxxxxxxxxxxxxx'.replace(/[x]/g, function (e) {
              return ((16 * Math.random()) | 0).toString(16);
            });
          (a.editorConfig.user &&
            a.editorConfig.user.id &&
            'number' == typeof a.editorConfig.user.id &&
            ((a.editorConfig.user.id = a.editorConfig.user.id.toString()),
            console.warn('The "id" parameter for the editorConfig.user object must be a string.')),
            (a.document.token = a.token));
        }
        return !0;
      })()
    ) {
      if (
        ((p = (function (e) {
          var t = o.createElement('iframe');
          ((t.src =
            (function (e) {
              var t =
                  'undefined' == typeof extensionParams || null == extensionParams.url
                    ? null
                    : extensionParams.url + 'apps/',
                n =
                  t ||
                  ('test' == e.type
                    ? (function () {
                        for (var e, t = o.getElementsByTagName('script'), n = t.length - 1; n >= 0; n--)
                          if ((e = t[n].src.match(/(.*)apps\/api\/documents\/api.js/i))) return e[1] + 'test/';
                        return '';
                      })()
                    : (function () {
                        for (var e, t = o.getElementsByTagName('script'), n = t.length - 1; n >= 0; n--)
                          if ((e = t[n].src.match(/(.*)api\/documents\/api.js/i))) return e[1];
                        return '';
                      })()),
                i = {
                  text: 'documenteditor',
                  'text-pdf': 'documenteditor',
                  spreadsheet: 'spreadsheeteditor',
                  presentation: 'presentationeditor',
                  word: 'documenteditor',
                  cell: 'spreadsheeteditor',
                  slide: 'presentationeditor',
                  pdf: 'pdfeditor',
                },
                s = i.word;
              if ('string' == typeof e.documentType) {
                if (
                  ((s = i[e.documentType.toLowerCase()]),
                  'desktop' == e.type && e.document && 'string' == typeof e.document.fileType)
                )
                  (d = /^(?:(pdf|djvu|xps|oxps))$/.exec(e.document.fileType)) && 'string' == typeof d[1] && (s = i.pdf);
              } else if (e.document && 'string' == typeof e.document.fileType) {
                var d;
                ((d =
                  /^(?:(xls|xlsx|ods|csv|xlst|xlsy|gsheet|xlsm|xlt|xltm|xltx|fods|ots|xlsb)|(pps|ppsx|ppt|pptx|odp|pptt|ppty|gslides|pot|potm|potx|ppsm|pptm|fodp|otp))$/.exec(
                    e.document.fileType,
                  )) && ('string' == typeof d[1] ? (s = i.cell) : 'string' == typeof d[2] && (s = i.slide)),
                  'desktop' == e.type &&
                    (d = /^(?:(pdf|djvu|xps|oxps))$/.exec(e.document.fileType)) &&
                    'string' == typeof d[1] &&
                    (s = i.pdf));
              }
              n += s + '/';
              const r =
                'mobile' === e.type
                  ? 'mobile'
                  : 'embedded' === e.type
                    ? 'embed'
                    : e.document &&
                        'string' == typeof e.document.fileType &&
                        'oform' === e.document.fileType.toLowerCase()
                      ? 'forms'
                      : 'main';
              n += r;
              var a = '/index.html';
              if (e.editorConfig && 'forms' !== r) {
                var c = e.editorConfig.customization;
                'object' == typeof c &&
                (c.toolbarNoTabs || ('desktop' !== e.editorConfig.targetApp && (c.loaderName || c.loaderLogo)))
                  ? (a = '/index_loader.html')
                  : ('editdiagram' !== e.editorConfig.mode &&
                      'editmerge' !== e.editorConfig.mode &&
                      'editole' !== e.editorConfig.mode) ||
                    (a = '/index_internal.html');
              }
              return (n += a);
            })(e) +
            (function (e) {
              var t = '?_dc=0';
              e.editorConfig && e.editorConfig.lang && (t += '&lang=' + e.editorConfig.lang);
              e.editorConfig &&
                'desktop' !== e.editorConfig.targetApp &&
                ('object' == typeof e.editorConfig.customization && e.editorConfig.customization.loaderName
                  ? 'none' !== e.editorConfig.customization.loaderName &&
                    (t += '&customer=' + encodeURIComponent(e.editorConfig.customization.loaderName))
                  : (t += '&customer=ONLYOFFICE'),
                'object' == typeof e.editorConfig.customization &&
                  (e.editorConfig.customization.loaderLogo &&
                    '' !== e.editorConfig.customization.loaderLogo &&
                    (t += '&logo=' + encodeURIComponent(e.editorConfig.customization.loaderLogo)),
                  e.editorConfig.customization.logo &&
                    ('embedded' == e.type &&
                    (e.editorConfig.customization.logo.image || e.editorConfig.customization.logo.imageEmbedded)
                      ? (t +=
                          '&headerlogo=' +
                          encodeURIComponent(
                            e.editorConfig.customization.logo.image || e.editorConfig.customization.logo.imageEmbedded,
                          ))
                      : 'embedded' != e.type &&
                        (e.editorConfig.customization.logo.image || e.editorConfig.customization.logo.imageDark) &&
                        (e.editorConfig.customization.logo.image &&
                          (t += '&headerlogo=' + encodeURIComponent(e.editorConfig.customization.logo.image)),
                        e.editorConfig.customization.logo.imageDark &&
                          (t +=
                            '&headerlogodark=' + encodeURIComponent(e.editorConfig.customization.logo.imageDark))))));
              !e.editorConfig ||
                ('editdiagram' != e.editorConfig.mode &&
                  'editmerge' != e.editorConfig.mode &&
                  'editole' != e.editorConfig.mode) ||
                (t += '&internal=true');
              e.frameEditorId && (t += '&frameEditorId=' + e.frameEditorId);
              var o = /^(?:(pdf))$/.exec(e.document.fileType);
              (o && 'string' == typeof o[1]) ||
                !(
                  (e.editorConfig && 'view' == e.editorConfig.mode) ||
                  (e.document &&
                    e.document.permissions &&
                    !1 === e.document.permissions.edit &&
                    !e.document.permissions.review)
                ) ||
                (t += '&mode=view');
              e.editorConfig &&
                e.editorConfig.customization &&
                e.editorConfig.customization.compactHeader &&
                (t += '&compact=true');
              e.editorConfig &&
                e.editorConfig.customization &&
                !1 === e.editorConfig.customization.toolbar &&
                (t += '&toolbar=false');
              e.parentOrigin && (t += '&parentOrigin=' + e.parentOrigin);
              e.editorConfig &&
                e.editorConfig.customization &&
                e.editorConfig.customization.uiTheme &&
                (t += '&uitheme=' + e.editorConfig.customization.uiTheme);
              return (
                (t += '&_document=' + encodeURIComponent(JSON.stringify(e.document))),
                (t += '&_config=' + encodeURIComponent(JSON.stringify(e.editorConfig))),
                t
              );
            })(e)),
            (t.width = e.width),
            (t.height = e.height),
            (t.align = 'top'),
            (t.frameBorder = 0),
            (t.name = 'frameEditor'),
            e.title && 'string' == typeof e.title && (t.title = e.title),
            (t.allowFullscreen = !0),
            t.setAttribute('allowfullscreen', ''),
            t.setAttribute('onmousewheel', ''),
            t.setAttribute('allow', 'autoplay; camera; microphone; display-capture; clipboard-write;'),
            'mobile' == e.type &&
              ((t.style.position = 'fixed'),
              (t.style.overflow = 'hidden'),
              (o.body.style.overscrollBehaviorY = 'contain')));
          return t;
        })(a)),
        a.editorConfig.customization &&
          'embed' === a.editorConfig.customization.integrationMode &&
          t.AscEmbed &&
          t.AscEmbed.initWorker(p),
        p.src)
      ) {
        var l = p.src.split('/');
        this.frameOrigin = l[0] + '//' + l[2];
      }
      g.parentNode && g.parentNode.replaceChild(p, g);
      var C = new MessageDispatcher(function (e) {
        if (e)
          if ('onExternalPluginMessage' === e.type) v(e);
          else if (t.parent !== t && 'onExternalPluginMessageCallback' === e.type) i(t.parent, e);
          else if (e.frameEditorId == n) {
            var s = (a.events || {})[e.event];
            'onRequestEditRights' !== e.event || s
              ? ('onAppReady' === e.event &&
                  ('mobile' === a.type &&
                    (o.body.onfocus = function (e) {
                      setTimeout(function () {
                        (p.contentWindow.focus(), v({ command: 'resetFocus', data: {} }));
                      }, 10);
                    }),
                  u(),
                  a.editorConfig && y(a.editorConfig),
                  a.document && h(a.document)),
                s && 'function' == typeof s && s.call(r, { target: r, data: e.data }))
              : x(!1, "handler isn't defined");
          }
      }, this);
    }
    var v = function (e) {
        p && p.contentWindow && i(p.contentWindow, e);
      },
      y = function (e) {
        v({ command: 'init', data: { config: e } });
      },
      h = function (e) {
        v({ command: 'openDocument', data: { doc: e } });
      },
      x = function (e, t) {
        v({ command: 'applyEditRights', data: { allowed: e, message: t } });
      },
      R = function (e) {
        var t = p.getBoundingClientRect(),
          o = { type: e.type, x: e.x - t.left, y: e.y - t.top, event: e };
        v({ command: 'processMouse', data: o });
      };
    return {
      showMessage: function (e, t) {
        v({ command: 'showMessage', data: { msg: (t = t || e) } });
      },
      processSaveResult: function (e, t) {
        v({ command: 'processSaveResult', data: { result: e, message: t } });
      },
      processRightsChange: function (e, t) {
        v({ command: 'processRightsChange', data: { enabled: e, message: t } });
      },
      denyEditingRights: function (e) {
        v({ command: 'processRightsChange', data: { enabled: !1, message: e } });
      },
      refreshHistory: function (e, t) {
        v({ command: 'refreshHistory', data: { data: e, message: t } });
      },
      setHistoryData: function (e, t) {
        v({ command: 'setHistoryData', data: { data: e, message: t } });
      },
      setEmailAddresses: function (e) {
        v({ command: 'setEmailAddresses', data: { data: e } });
      },
      setActionLink: function (e) {
        v({ command: 'setActionLink', data: { url: e } });
      },
      processMailMerge: function (e, t) {
        v({ command: 'processMailMerge', data: { enabled: e, message: t } });
      },
      downloadAs: function (e) {
        v({ command: 'downloadAs', data: e });
      },
      serviceCommand: function (e, t) {
        v({ command: 'internalCommand', data: { command: e, data: t } });
      },
      attachMouseEvents: u,
      detachMouseEvents: f,
      destroyEditor: function (e) {
        var t = o.createElement('div');
        (t.setAttribute('id', n), p && (C && C.unbindEvents(), f(), p.parentNode && p.parentNode.replaceChild(t, p)));
      },
      setUsers: function (e) {
        v({ command: 'setUsers', data: e });
      },
      showSharingSettings: function (e) {
        v({ command: 'showSharingSettings', data: e });
      },
      setSharingSettings: function (e) {
        v({ command: 'setSharingSettings', data: e });
      },
      insertImage: function (e) {
        v({ command: 'insertImage', data: e });
      },
      setMailMergeRecipients: function (e) {
        v({ command: 'setMailMergeRecipients', data: e });
      },
      setRevisedFile: function (e) {
        v({ command: 'setRevisedFile', data: e });
      },
      setFavorite: function (e) {
        v({ command: 'setFavorite', data: e });
      },
      requestClose: function (e) {
        v({ command: 'requestClose', data: e });
      },
      grabFocus: function (e) {
        setTimeout(function () {
          v({ command: 'grabFocus', data: e });
        }, 10);
      },
      blurFocus: function (e) {
        v({ command: 'blurFocus', data: e });
      },
      setReferenceData: function (e) {
        v({ command: 'setReferenceData', data: e });
      },
      setRequestedDocument: function (e) {
        v({ command: 'setRequestedDocument', data: e });
      },
      setRequestedSpreadsheet: function (e) {
        v({ command: 'setRequestedSpreadsheet', data: e });
      },
      setReferenceSource: function (e) {
        v({ command: 'setReferenceSource', data: e });
      },
      sendCommand: v,
    };
  }),
    (e.DocEditor.defaultConfig = {
      type: 'desktop',
      width: '100%',
      height: '100%',
      editorConfig: { lang: 'en', canCoAuthoring: !0, customization: { about: !0, feedback: !1 } },
    }),
    (e.DocEditor.version = function () {
      return '7.4.1';
    }),
    (MessageDispatcher = function (e, o) {
      var n = e,
        i = o || t,
        s = function (e) {
          d(e);
        },
        d = function (e) {
          if (e && t.JSON && i.frameOrigin == e.origin)
            try {
              e = e.data;
              n && n.call(i, e);
            } catch (e) {
              console.error(e);
            }
        };
      return (
        function () {
          t.addEventListener ? t.addEventListener('message', s, !1) : t.attachEvent && t.attachEvent('onmessage', s);
        }.call(this),
        {
          unbindEvents: function () {
            t.removeEventListener
              ? t.removeEventListener('message', s, !1)
              : t.detachEvent && t.detachEvent('onmessage', s);
          },
        }
      );
    }));
})((window.DocsAPI = window.DocsAPI || {}), window, document);