From c564b1b75a9324c1957dcc5bf2e29b026c8c96b7 Mon Sep 17 00:00:00 2001 From: yuri Date: Tue, 9 May 2017 12:57:49 +0300 Subject: [PATCH] stream: store post content on leave --- client/src/app.js | 28 ++++++++++++++++-- client/src/session-storage.js | 50 ++++++++++++++++++++++++++++++++ client/src/storage.js | 13 ++++++--- client/src/view.js | 6 ++++ client/src/views/stream/panel.js | 23 +++++++++++++++ 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 client/src/session-storage.js diff --git a/client/src/app.js b/client/src/app.js index 52edd45bd1..f131579a15 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -29,8 +29,30 @@ Espo.define( 'app', - ['ui', 'utils', 'acl-manager', 'cache', 'storage', 'models/settings', 'language', 'metadata', 'field-manager', 'models/user', 'models/preferences', 'model-factory' ,'collection-factory', 'pre-loader', 'view-helper', 'controllers/base', 'router', 'date-time', 'layout-manager', 'theme-manager'], - function (Ui, Utils, AclManager, Cache, Storage, Settings, Language, Metadata, FieldManager, User, Preferences, ModelFactory, CollectionFactory, PreLoader, ViewHelper, BaseController, Router, DateTime, LayoutManager, ThemeManager) { + [ + 'ui', + 'utils', + 'acl-manager', + 'cache', + 'storage', + 'models/settings', + 'language', + 'metadata', + 'field-manager', + 'models/user', + 'models/preferences', + 'model-factory', + 'collection-factory', + 'pre-loader', + 'view-helper', + 'controllers/base', + 'router', + 'date-time', + 'layout-manager', + 'theme-manager', + 'session-storage' + ], + function (Ui, Utils, AclManager, Cache, Storage, Settings, Language, Metadata, FieldManager, User, Preferences, ModelFactory, CollectionFactory, PreLoader, ViewHelper, BaseController, Router, DateTime, LayoutManager, ThemeManager, SessionStorage) { var App = function (options, callback) { var options = options || {}; @@ -56,6 +78,7 @@ Espo.define( } this.storage = new Storage(); + this.sessionStorage = new SessionStorage(); this.loader.cache = this.cache; @@ -323,6 +346,7 @@ Espo.define( helper.cache = this.cache; helper.storage = this.storage; helper.themeManager = this.themeManager; + helper.sessionStorage = this.sessionStorage; helper.basePath = this.basePath; this.viewLoader = function (viewName, callback) { diff --git a/client/src/session-storage.js b/client/src/session-storage.js new file mode 100644 index 0000000000..c84a850d7a --- /dev/null +++ b/client/src/session-storage.js @@ -0,0 +1,50 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014-2017 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + * + * The interactive user interfaces in modified source and object code versions + * of this program must display Appropriate Legal Notices, as required under + * Section 5 of the GNU General Public License version 3. + * + * In accordance with Section 7(b) of the GNU General Public License version 3, + * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. + ************************************************************************/ + +Espo.define('session-storage', 'storage', function (Dep) { + + return Dep.extend({ + + _prefix: 's', + + storageObject: sessionStorage, + + get: function (name) { + return Dep.prototype.get.call(this, 'session', name); + }, + + set: function (name, value) { + Dep.prototype.set.call(this, 'session', name, value); + }, + + clear: function (type, name) { + Dep.prototype.clear.call(this, 'session', name); + } + + }); +}); diff --git a/client/src/storage.js b/client/src/storage.js index 03b99fdf66..fd0269383c 100644 --- a/client/src/storage.js +++ b/client/src/storage.js @@ -35,6 +35,8 @@ Espo.define('storage', [], function () { _prefix: 'espo', + storageObject: localStorage, + _composeFullPrefix: function (type) { return this._prefix + '-' + type; }, @@ -53,7 +55,8 @@ Espo.define('storage', [], function () { this._checkType(type); var key = this._composeKey(type, name); - var stored = localStorage.getItem(key); + + var stored = this.storageObject.getItem(key); if (stored) { var str = stored; if (stored[0] == "{" || stored[0] == "[") { @@ -76,7 +79,7 @@ Espo.define('storage', [], function () { if (value instanceof Object) { value = JSON.stringify(value); } - localStorage.setItem(key, value); + this.storageObject.setItem(key, value); }, clear: function (type, name) { @@ -91,13 +94,15 @@ Espo.define('storage', [], function () { reText = '^' + this._prefix + '-'; } var re = new RegExp(reText); - for (var i in localStorage) { + for (var i in this.storageObject) { if (re.test(i)) { - delete localStorage[i]; + delete this.storageObject[i]; } } } }); + Storage.extend = Backbone.Router.extend; + return Storage; }); diff --git a/client/src/view.js b/client/src/view.js index f6e3291414..c335417d7c 100644 --- a/client/src/view.js +++ b/client/src/view.js @@ -112,6 +112,12 @@ Espo.define('view', [], function () { } }, + getSessionStorage: function () { + if (this._helper) { + return this._helper.sessionStorage; + } + }, + getLanguage: function () { if (this._helper) { return this._helper.language; diff --git a/client/src/views/stream/panel.js b/client/src/views/stream/panel.js index ec8a53911b..1afb8daa85 100644 --- a/client/src/views/stream/panel.js +++ b/client/src/views/stream/panel.js @@ -97,6 +97,9 @@ Espo.define('views/stream/panel', ['views/record/panels/relationship', 'lib!Text this.$el.find('.buttons-panel').removeClass('hide'); if (!this.postingMode) { + if (this.$textarea.val() && this.$textarea.val().length) { + this.controlTextareaHeight(); + } $('body').on('click.stream-panel', function (e) { var $target = $(e.target); if ($target.parent().hasClass('remove-attachment')) return; @@ -143,6 +146,19 @@ Espo.define('views/stream/panel', ['views/record/panels/relationship', 'lib!Text this.isInternalNoteMode = false; + this.storageTextKey = 'stream-post-' + this.model.name + '-' + this.model.id; + + this.on('remove', function () { + if (this.$textarea && this.$textarea.size()) { + var text = this.$textarea.val(); + if (text.length) { + this.getSessionStorage().set(this.storageTextKey, text); + } else { + this.getSessionStorage().clear(this.storageTextKey); + } + } + }, this); + this.wait(true); this.getModelFactory().create('Note', function (model) { this.seed = model; @@ -170,6 +186,11 @@ Espo.define('views/stream/panel', ['views/record/panels/relationship', 'lib!Text var $textarea = this.$textarea; + var storedText = this.getSessionStorage().get(this.storageTextKey); + if (storedText && storedText.length) { + this.$textarea.val(storedText); + } + $textarea.off('drop'); $textarea.off('dragover'); $textarea.off('dragleave'); @@ -319,6 +340,8 @@ Espo.define('views/stream/panel', ['views/record/panels/relationship', 'lib!Text if (this.getPreferences().get('followEntityOnStreamPost')) { this.model.set('isFollowed', true); } + + this.getSessionStorage().clear(this.storageTextKey); }, this); model.set('post', message);