spellcheckapi.js 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 *
 * (c) Copyright Ascensio System Limited 2010-2016
 *
 * This program is freeware. You can redistribute it and/or modify it under the terms of the GNU 
 * General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html). 
 * In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that 
 * Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
 *
 * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
 *
 * You can contact Ascensio System SIA by email at sales@onlyoffice.com
 *
 * The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display 
 * Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
 *
 * Pursuant to Section 7  3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains 
 * relevant author attributions when distributing the software. If the display of the logo in its graphic 
 * form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE" 
 * in every copy of the program you distribute. 
 * Pursuant to Section 7  3(e) we decline to grant you any rights under trademark law for use of our trademarks.
 *
*/
25 26
"use strict";

27 28
(function(global) {
  'use strict';
29

30 31 32 33
  // Класс надстройка, для online и offline работы
  var CSpellCheckApi = function(options) {
    this._SpellCheckApi = new SpellCheckApi();
    this._onlineWork = false;
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    if (options) {
      this.onDisconnect = options.onDisconnect;
      this.onSpellCheck = options.onSpellCheck;
    }
  };

  CSpellCheckApi.prototype.init = function(docid) {
    if (this._SpellCheckApi && this._SpellCheckApi.isRightURL()) {
      var t = this;
      this._SpellCheckApi.onDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
        t.callback_OnDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
      };
      this._SpellCheckApi.onSpellCheck = function(e) {
        t.callback_OnSpellCheck(e);
      };

      this._SpellCheckApi.init(docid);
      this._onlineWork = true;
    }
  };
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
  CSpellCheckApi.prototype.set_url = function(url) {
    if (this._SpellCheckApi) {
      this._SpellCheckApi.set_url(url);
    }
  };

  CSpellCheckApi.prototype.get_state = function() {
    if (this._SpellCheckApi) {
      return this._SpellCheckApi.get_state();
    }

    return 0;
  };

  CSpellCheckApi.prototype.disconnect = function() {
    if (this._SpellCheckApi && this._onlineWork) {
      this._SpellCheckApi.disconnect();
    }
  };

  CSpellCheckApi.prototype.spellCheck = function(spellCheckData) {
    if (this._SpellCheckApi && this._onlineWork) {
      this._SpellCheckApi.spellCheck(spellCheckData);
    }
  };

  CSpellCheckApi.prototype.callback_OnSpellCheck = function(e) {
    if (this.onSpellCheck) {
      return this.onSpellCheck(e);
    }
  };

  /**
   * Event об отсоединении от сервера
   * @param {jQuery} e  event об отсоединении с причиной
   * @param {Bool} isDisconnectAtAll  окончательно ли отсоединяемся(true) или будем пробовать сделать reconnect(false) + сами отключились
   */
  CSpellCheckApi.prototype.callback_OnDisconnect = function(e, isDisconnectAtAll, isCloseCoAuthoring) {
    if (this.onDisconnect) {
      return this.onDisconnect(e, isDisconnectAtAll, isCloseCoAuthoring);
    }
  };

  /** States
   * -1 - reconnect state
   *  0 - not initialized
   *  1 - opened
   *  3 - closed
   */
  var SpellCheckApi = function(options) {
    if (options) {
      this.onDisconnect = options.onDisconnect;
      this.onConnect = options.onConnect;
      this.onSpellCheck = options.onSpellCheck;
    }
    this._state = 0;
    // Мы сами отключились от совместного редактирования
    this.isCloseCoAuthoring = false;

    // Массив данных, который стоит отправить как только подключимся
    this.dataNeedSend = [];

    this._url = "";
  };

  SpellCheckApi.prototype.isRightURL = function() {
    return ("" != this._url);
  };

  SpellCheckApi.prototype.set_url = function(url) {
    this._url = url;
  };

  SpellCheckApi.prototype.get_state = function() {
    return this._state;
  };

  SpellCheckApi.prototype.spellCheck = function(spellCheckData) {
    this._send({"type": "spellCheck", "spellCheckData": spellCheckData});
  };

  SpellCheckApi.prototype.disconnect = function() {
    // Отключаемся сами
    this.isCloseCoAuthoring = true;
    return this.sockjs.close();
  };

  SpellCheckApi.prototype._send = function(data) {
    if (data !== null && typeof data === "object") {
      if (this._state > 0) {
        this.sockjs.send(JSON.stringify(data));
      } else {
        this.dataNeedSend.push(data);
      }
    }
  };

  SpellCheckApi.prototype._sendAfterConnect = function() {
    var data;
    while (this._state > 0 && undefined !== (data = this.dataNeedSend.shift()))
      this._send(data);
  };

  SpellCheckApi.prototype._onSpellCheck = function(data) {
    if (undefined !== data["spellCheckData"] && this.onSpellCheck) {
      this.onSpellCheck(data["spellCheckData"]);
    }
  };
164

165
  var reconnectTimeout, attemptCount = 0;
166

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  function initSocksJs(url, docsCoApi) {
    var sockjs = new (_getSockJs())(url, null, {debug: true});

    sockjs.onopen = function() {
      if (reconnectTimeout) {
        clearTimeout(reconnectTimeout);
        attemptCount = 0;
      }
      docsCoApi._state = 1; // Opened state
      if (docsCoApi.onConnect) {
        docsCoApi.onConnect();
      }

      // Отправляем все данные, которые пришли до соединения с сервером
      docsCoApi._sendAfterConnect();
182 183
    };

184 185 186 187 188 189 190 191 192 193
    sockjs.onmessage = function(e) {
      //TODO: add checks and error handling
      //Get data type
      var dataObject = JSON.parse(e.data);
      var type = dataObject.type;
      switch (type) {
        case 'spellCheck'  :
          docsCoApi._onSpellCheck(dataObject);
          break;
      }
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
    sockjs.onclose = function(evt) {
      docsCoApi._state = -1; // Reconnect state
      var bIsDisconnectAtAll = attemptCount >= 20 || docsCoApi.isCloseCoAuthoring;
      if (bIsDisconnectAtAll) {
        docsCoApi._state = 3;
      } // Closed state
      if (docsCoApi.onDisconnect) {
        docsCoApi.onDisconnect(evt.reason, bIsDisconnectAtAll, docsCoApi.isCloseCoAuthoring);
      }
      if (docsCoApi.isCloseCoAuthoring) {
        return;
      }
      //Try reconect
      if (attemptCount < 20) {
        tryReconnect();
      }
    };

    function tryReconnect() {
      if (reconnectTimeout) {
        clearTimeout(reconnectTimeout);
      }
      attemptCount++;
      reconnectTimeout = setTimeout(function() {
        delete docsCoApi.sockjs;
        docsCoApi.sockjs = initSocksJs(url, docsCoApi);
      }, 500 * attemptCount);
222 223 224

    }

225 226 227 228 229 230 231 232 233 234 235 236 237 238
    return sockjs;
  }

  function _getSockJs() {
    return window['SockJS'] ? window['SockJS'] : require('sockjs');
  }

  SpellCheckApi.prototype.init = function(docid) {
    this._docid = docid;
    //Begin send auth
    this.sockjs_url = this._url + '/doc/' + docid + '/c';
    this.sockjs = initSocksJs(this.sockjs_url, this);
  };
  global["CSpellCheckApi"] = CSpellCheckApi;
239

Oleg.Korshul's avatar
Oleg.Korshul committed
240
})(window);
241