From 5773c4ead5b141e2e34c92cc39dc676540e616fd Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Fri, 6 Oct 2017 15:16:07 +0200 Subject: [PATCH] fix: quiqqer/package-bricks#42 --- bin/BrickEdit.js | 66 +++++------- bin/Bricks.js | 9 ++ bin/Manager.js | 226 ++++++++++-------------------------------- bin/Site/Area.js | 86 +++++++++++----- bin/Site/BrickEdit.js | 9 -- bin/Site/Category.js | 13 +-- bin/classes/Areas.js | 7 +- bin/classes/Bricks.js | 192 +++++++++++++++++++++++++++++++++++ 8 files changed, 348 insertions(+), 260 deletions(-) create mode 100644 bin/Bricks.js create mode 100644 bin/classes/Bricks.js diff --git a/bin/BrickEdit.js b/bin/BrickEdit.js index 6047e60..aea46ba 100644 --- a/bin/BrickEdit.js +++ b/bin/BrickEdit.js @@ -21,10 +21,12 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ 'qui/utils/Form', 'utils/Controls', 'utils/Template', + 'package/quiqqer/bricks/bin/Bricks', 'css!package/quiqqer/bricks/bin/BrickEdit.css' -], function (QUI, QUIPanel, QUIConfirm, BrickAreas, QUIAjax, QUILocale, Projects, QUIFormUtils, ControlUtils, Template) { +], function (QUI, QUIPanel, QUIConfirm, BrickAreas, QUIAjax, QUILocale, + Projects, QUIFormUtils, ControlUtils, Template, Bricks) { "use strict"; var lg = 'quiqqer/bricks'; @@ -201,57 +203,46 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ * * @return Promise */ - save: function (callback) { - var Active = this.$Active; + save: function () { + var self = this, + Active = this.$Active; this.Loader.show(); this.$unload(); return this.$load(Active).then(function () { + var data = self.getAttribute('data'); + data.customfields = self.$customfields; - return new Promise(function (resolve, reject) { - var data = this.getAttribute('data'); - - data.customfields = this.$customfields; - - QUIAjax.post('package_quiqqer_bricks_ajax_brick_save', function () { - if (typeof callback === 'function') { - callback(); - } - - resolve(); - - QUI.getMessageHandler().then(function (MH) { - MH.addSuccess( - QUILocale.get(lg, 'message.brick.save.success') - ); - }); - - this.fireEvent('save', [this]); - this.Loader.hide(); - - }.bind(this), { - 'package': 'quiqqer/brick', - brickId : this.getAttribute('id'), - data : JSON.encode(data), - onError : reject + return Bricks.saveBrick(self.getAttribute('id'), data).then(function () { + QUI.getMessageHandler().then(function (MH) { + MH.addSuccess( + QUILocale.get(lg, 'message.brick.save.success') + ); }); - }.bind(this)); - - }.bind(this)); + self.fireEvent('save', [self]); + self.Loader.hide(); + }); + }); }, /** * Delete the brick */ del: function () { - var self = this; + var self = this, + data = this.getAttribute('data'); new QUIConfirm({ title : QUILocale.get(lg, 'window.brick.delete.title'), - text : QUILocale.get(lg, 'window.brick.delete.text'), + text : QUILocale.get(lg, 'window.brick.delete.text', { + brickId : self.getAttribute('id'), + brickTitle: data.attributes.title + }), information: QUILocale.get(lg, 'window.brick.delete.information'), + icon : 'fa fa-trash', + texticon : 'fa fa-trash', maxHeight : 300, maxWidth : 600, autoclose : false, @@ -259,14 +250,9 @@ define('package/quiqqer/bricks/bin/BrickEdit', [ onSubmit: function (Win) { Win.Loader.show(); - QUIAjax.post('package_quiqqer_bricks_ajax_brick_delete', function () { + Bricks.deleteBricks([self.getAttribute('id')]).then(function () { Win.close(); - self.fireEvent('delete'); - self.destroy(); - }, { - 'package': 'quiqqer/bricks', - brickIds : JSON.encode([self.getAttribute('id')]) }); } } diff --git a/bin/Bricks.js b/bin/Bricks.js new file mode 100644 index 0000000..206e413 --- /dev/null +++ b/bin/Bricks.js @@ -0,0 +1,9 @@ +/** + * Main bricks handler + */ +define('package/quiqqer/bricks/bin/Bricks', [ + 'package/quiqqer/bricks/bin/classes/Bricks' +], function (Bricks) { + "use strict"; + return new Bricks(); +}); \ No newline at end of file diff --git a/bin/Manager.js b/bin/Manager.js index 7979104..45a51b6 100644 --- a/bin/Manager.js +++ b/bin/Manager.js @@ -16,6 +16,7 @@ define('package/quiqqer/bricks/bin/Manager', [ 'Locale', 'Projects', 'Ajax', + 'package/quiqqer/bricks/bin/Bricks', 'Mustache', 'text!package/quiqqer/bricks/bin/Manager.Copy.html', @@ -23,7 +24,7 @@ define('package/quiqqer/bricks/bin/Manager', [ 'css!package/quiqqer/bricks/bin/Manager.css' ], function (QUI, QUIPanel, QUISelect, QUIButton, QUISeparator, QUIConfirm, Grid, QUILocale, Projects, Ajax, - Mustache, templateCopy) { + Bricks, Mustache, templateCopy) { "use strict"; var lg = 'quiqqer/bricks'; @@ -43,7 +44,9 @@ define('package/quiqqer/bricks/bin/Manager', [ '$openCopyDialog', '$openDeleteDialog', '$onDblClick', - '$onClick' + '$onClick', + '$onInject', + '$onDestroy' ], options: { @@ -58,8 +61,10 @@ define('package/quiqqer/bricks/bin/Manager', [ this.$ProjectLangs = false; this.addEvents({ - onCreate: this.$onCreate, - onResize: this.$onResize + onCreate : this.$onCreate, + onResize : this.$onResize, + onInject : this.$onInject, + onDestroy: this.$onDestroy }); }, @@ -79,7 +84,7 @@ define('package/quiqqer/bricks/bin/Manager', [ this.Loader.show(); - this.getBricksFromProject(project, lang, function (result) { + Bricks.getBricksFromProject(project, lang).then(function (result) { if (typeof callback === 'function') { callback(); } @@ -282,6 +287,30 @@ define('package/quiqqer/bricks/bin/Manager', [ }); }, + /** + * event: on inject + */ + $onInject: function () { + Bricks.addEvents({ + onBrickDelete: this.refresh, + onBrickSave : this.refresh, + onBrickCopy : this.refresh, + onBrickCreate: this.refresh + }); + }, + + /** + * event: on destroy + */ + $onDestroy: function () { + Bricks.removeEvents({ + onBrickDelete: this.refresh, + onBrickSave : this.refresh, + onBrickCopy : this.refresh, + onBrickCreate: this.refresh + }); + }, + /** * event : on resize */ @@ -390,7 +419,7 @@ define('package/quiqqer/bricks/bin/Manager', [ '</label>' ); - self.getAvailableBricks(function (bricklist) { + Bricks.getAvailableBricks().then(function (bricklist) { if (!Body) { return; } @@ -444,17 +473,15 @@ define('package/quiqqer/bricks/bin/Manager', [ } var project = self.$ProjectSelect.getValue(), - lang = self.$ProjectLangs.getValue(); + lang = self.$ProjectLangs.getValue(), + data = { + title: Title.value, + type : Type.value + }; - self.createBrick(project, lang, { - title: Title.value, - type : Type.value - }, function (brickId) { + Bricks.createBrick(project, lang, data).then(function (brickId) { Win.close(); - - self.refresh(function () { - self.editBrick(brickId); - }); + self.editBrick(brickId); }); } } @@ -497,9 +524,8 @@ define('package/quiqqer/bricks/bin/Manager', [ onSubmit: function (Win) { Win.Loader.show(); - self.deleteBricks(brickIds, function () { + Bricks.deleteBricks(brickIds).then(function () { Win.close(); - self.refresh(); }); } } @@ -555,16 +581,13 @@ define('package/quiqqer/bricks/bin/Manager', [ Select.setValue(lang); - Ajax.get('package_quiqqer_bricks_ajax_getBrick', function (data) { + Bricks.getBrick(brickId).then(function (data) { var Form = Content.getElement('form'); Form.elements.title.value = data.attributes.title; Form.elements.description.value = data.attributes.description; Win.Loader.hide(); - }, { - 'package': 'quiqqer/bricks', - brickId : brickId }); }); }, @@ -578,19 +601,13 @@ define('package/quiqqer/bricks/bin/Manager', [ var Select = Content.getElement('.dialog-bricks-copy-languages [data-quiid]'); var Language = QUI.Controls.getById(Select.get('data-quiid')); - Ajax.post('package_quiqqer_bricks_ajax_brick_copy', function (data) { - self.refresh(); + Bricks.copyBrick(brickId, { + 'lang' : Language.getValue(), + 'title' : Form.elements.title.value, + 'description': Form.elements.description.value + }).then(function (data) { Win.close(); - - self.editBrick(data['id']); - }, { - 'package': 'quiqqer/bricks', - brickId : brickId, - params : JSON.encode({ - 'lang' : Language.getValue(), - 'title' : Form.elements.title.value, - 'description': Form.elements.description.value - }) + self.editBrick(data.id); }); } } @@ -612,151 +629,10 @@ define('package/quiqqer/bricks/bin/Manager', [ '#id' : 'brick-edit-' + brickId, id : brickId, projectName: this.$ProjectSelect.getValue(), - projectLang: this.$ProjectLangs.getValue(), - events : { - onDelete: this.refresh - } + projectLang: this.$ProjectLangs.getValue() }) ); }.bind(this)); - }, - - /** - * Methods / Model - */ - - /** - * Return the available bricks - * - * @param {Function} [callback] - callback function - * - * @return Promise - */ - getAvailableBricks: function (callback) { - return new Promise(function (resolve, reject) { - - Ajax.get('package_quiqqer_bricks_ajax_getAvailableBricks', function (result) { - - if (typeof callback === 'function') { - callback(result); - } - - resolve(result); - }, { - 'package': 'quiqqer/bricks', - onError : reject - }); - - }); - }, - - /** - * Return the bricksf from a project - * - * @param {String} project - name of the project - * @param {String} lang - Language of the project - * @param {Function} [callback] - callback function - * - * @return Promise - */ - getBricksFromProject: function (project, lang, callback) { - return new Promise(function (resolve, reject) { - - Ajax.get('package_quiqqer_bricks_ajax_project_getBricks', function (result) { - - if (typeof callback === 'function') { - callback(result); - } - - resolve(result); - }, { - 'package': 'quiqqer/bricks', - project : JSON.encode({ - name: project, - lang: lang - }), - onError : reject - }); - - }); - }, - - /** - * Create a new brick - * - * @param {String} project - name of the project - * @param {String} lang - Language of the project - * @param {Object} data - Data of the brick - * @param {Function} [callback] - callback function - * - * @return Promise - */ - createBrick: function (project, lang, data, callback) { - return new Promise(function (resolve, reject) { - - Ajax.post('package_quiqqer_bricks_ajax_project_createBrick', function (brickId) { - - if (typeof callback === 'function') { - callback(brickId); - } - - resolve(brickId); - }, { - 'package': 'quiqqer/bricks', - project : JSON.encode({ - name: project, - lang: lang - }), - data : JSON.encode(data), - onError : reject - }); - - }); - }, - - /** - * Delete the Brick-Ids - * - * @param {Array} brickIds - Brick IDs which should be deleted - * @param {Function} [callback] - callback function - * - * @return Promise - */ - deleteBricks: function (brickIds, callback) { - var panels = QUI.Controls.getByType( - 'package/quiqqer/bricks/bin/BrickEdit' - ); - - return new Promise(function (resolve, reject) { - - Ajax.post('package_quiqqer_bricks_ajax_brick_delete', function () { - - if (typeof callback === 'function') { - callback(); - } - - resolve(); - - // exist brick panels? - var c, i, len, clen, brickId; - - for (i = 0, len = brickIds.length; i < len; i++) { - brickId = brickIds[i]; - - for (c = 0, clen = panels.length; c < clen; c++) { - if (panels[c].getAttribute('id') === brickId) { - panels[c].destroy(); - } - } - } - - }, { - 'package': 'quiqqer/bricks', - brickIds : JSON.encode(brickIds), - onError : reject - }); - - }); } }); }); diff --git a/bin/Site/Area.js b/bin/Site/Area.js index 91d41a6..f5e6b0f 100644 --- a/bin/Site/Area.js +++ b/bin/Site/Area.js @@ -3,17 +3,6 @@ * * @module package/quiqqer/bricks/bin/Site/Area * @author www.pcsg.de (Henning Leutz) - * - * @require qui/QUI - * @require qui/controls/Control - * @require qui/controls/buttons/Button - * @require qui/controls/windows/Popup - * @require qui/controls/windows/Confirm - * @require qui/controls/elements/List - * @require Locale - * @require Ajax - * @require package/quiqqer/bricks/bin/Sortables - * @require css!package/quiqqer/bricks/bin/Site/Area.css */ define('package/quiqqer/bricks/bin/Site/Area', [ @@ -24,18 +13,18 @@ define('package/quiqqer/bricks/bin/Site/Area', [ 'qui/controls/windows/Alert', 'qui/controls/windows/Confirm', 'qui/controls/elements/List', + 'package/quiqqer/bricks/bin/Bricks', 'Locale', 'Ajax', 'package/quiqqer/bricks/bin/Sortables', 'css!package/quiqqer/bricks/bin/Site/Area.css' -], function (QUI, QUIControl, QUIButton, QUIPopup, QUIAlert, QUIConfirm, QUIList, QUILocale, QUIAjax, Sortables) { +], function (QUI, QUIControl, QUIButton, QUIPopup, QUIAlert, QUIConfirm, QUIList, Bricks, QUILocale, QUIAjax, Sortables) { "use strict"; var lg = 'quiqqer/bricks'; - return new Class({ Extends: QUIControl, @@ -46,7 +35,9 @@ define('package/quiqqer/bricks/bin/Site/Area', [ 'openBrickSettingDialog', 'openSettingsDialog', 'createNewBrick', - '$onInject' + '$onInject', + '$onDestroy', + '$onBrickRefresh' ], options: { @@ -78,12 +69,33 @@ define('package/quiqqer/bricks/bin/Site/Area', [ this.$ExtraBtns = false; this.addEvents({ - onInject: this.$onInject + onInject : this.$onInject, + onDestroy: this.$onDestroy + }); + + Bricks.addEvents({ + onBrickDelete: this.$onBrickRefresh, + onBrickSave : this.$onBrickRefresh, + onBrickCopy : this.$onBrickRefresh, + onBrickCreate: this.$onBrickRefresh }); }, /** - * Return the domnode element + * event: on destroy + */ + $onDestroy: function () { + Bricks.removeEvents({ + onBrickDelete: this.$onBrickRefresh, + onBrickSave : this.$onBrickRefresh, + onBrickCopy : this.$onBrickRefresh, + onBrickCreate: this.$onBrickRefresh + }); + }, + + /** + * Return the DOMNode element + * * @return {HTMLElement} */ create: function () { @@ -214,9 +226,7 @@ define('package/quiqqer/bricks/bin/Site/Area', [ * event : on inject */ $onInject: function () { - var self = this, - Site = this.getAttribute('Site'), - Project = Site.getProject(); + var self = this; var Loader = new Element('div', { 'class': 'quiqqer-bricks-site-category-area-loader', @@ -231,11 +241,9 @@ define('package/quiqqer/bricks/bin/Site/Area', [ opacity : 0 }); - QUIAjax.get('package_quiqqer_bricks_ajax_project_getBricks', function (bricks) { + this.$refreshAvailableBricks().then(function () { self.$AddButton.enable(); - - self.$availableBricks = bricks; - self.$loaded = true; + self.$loaded = true; self.$brickIds.each(function (brickId) { self.addBrickById(brickId); @@ -257,11 +265,35 @@ define('package/quiqqer/bricks/bin/Site/Area', [ } Loader.destroy(); + }); + }, - }, { - 'package': 'quiqqer/bricks', - project : Project.encode(), - area : this.getAttribute('name') + /** + * event : on brick changes + */ + $onBrickRefresh: function () { + this.$refreshAvailableBricks(); + }, + + /** + * Refresh the available bricks + * + * @return {*|Promise} + */ + $refreshAvailableBricks: function () { + var self = this, + Site = this.getAttribute('Site'), + Project = Site.getProject(); + console.warn('refresh'); + return new Promise(function (resolve) { + QUIAjax.get('package_quiqqer_bricks_ajax_project_getBricks', function (bricks) { + self.$availableBricks = bricks; + resolve(bricks); + }, { + 'package': 'quiqqer/bricks', + project : Project.encode(), + area : self.getAttribute('name') + }); }); }, diff --git a/bin/Site/BrickEdit.js b/bin/Site/BrickEdit.js index 1395ff6..c3b9b8f 100644 --- a/bin/Site/BrickEdit.js +++ b/bin/Site/BrickEdit.js @@ -3,15 +3,6 @@ * * @module package/quiqqer/bricks/bin/Site/Area * @author www.pcsg.de (Henning Leutz) - * - * @require qui/QUI - * @require qui/controls/Control - * @require qui/controls/loader/Loader - * @require qui/utils/Form - * @require utils/Template - * @require utils/Controls - * @require Ajax - * @require css!package/quiqqer/bricks/bin/Site/BrickEdit.css */ define('package/quiqqer/bricks/bin/Site/BrickEdit', [ diff --git a/bin/Site/Category.js b/bin/Site/Category.js index 4f2d089..6645767 100644 --- a/bin/Site/Category.js +++ b/bin/Site/Category.js @@ -4,14 +4,6 @@ * @module package/quiqqer/bricks/bin/Site/Category * @author www.pcsg.de (Henning Leutz) * - * @require qui/QUI - * @require qui/controls/Control - * @require qui/controls/loader/Loader - * @require Ajax - * @require Locale - * @require package/quiqqer/bricks/bin/Site/Area - * @require css!package/quiqqer/bricks/bin/Site/Category.css - * * @event onLoaded */ define('package/quiqqer/bricks/bin/Site/Category', [ @@ -141,6 +133,11 @@ define('package/quiqqer/bricks/bin/Site/Category', [ */ $onDestroy: function () { this.updateSite(); + + // destroy areas + this.areas.each(function (Area) { + Area.destroy(); + }); }, /** diff --git a/bin/classes/Areas.js b/bin/classes/Areas.js index 280cfb3..32af44a 100644 --- a/bin/classes/Areas.js +++ b/bin/classes/Areas.js @@ -1,3 +1,8 @@ +/** + * Main area handler + * + * @author www.pcsg.de (Henning Leutz) + */ define('package/quiqqer/bricks/bin/classes/Areas', [ 'qui/QUI', @@ -10,7 +15,7 @@ define('package/quiqqer/bricks/bin/classes/Areas', [ return new Class({ Extends: QDOM, - Type : 'classes/editor/Manager', + Type : 'package/quiqqer/bricks/bin/classes/Areas', initialize: function (options) { this.parent(options); diff --git a/bin/classes/Bricks.js b/bin/classes/Bricks.js new file mode 100644 index 0000000..609c61f --- /dev/null +++ b/bin/classes/Bricks.js @@ -0,0 +1,192 @@ +/** + * Main Brick Handler + * + * @author www.pcsg.de (Henning Leutz) + * + * @event onBrickSave [brickId, data, result] + * @event onBrickCopy [brickId, params, result] + * @event onBrickCreate [brick, project, lang, data] + * @event onBrickDelete [brickIds] + */ +define('package/quiqqer/bricks/bin/classes/Bricks', [ + + 'qui/QUI', + 'qui/classes/DOM', + 'Ajax' + +], function (QUI, QDOM, QUIAjax) { + "use strict"; + + return new Class({ + Extends: QDOM, + Type : 'package/quiqqer/bricks/bin/classes/Bricks', + + initialize: function (options) { + this.parent(options); + }, + + /** + * Return the data of a brick + * + * @param {Number|String} brickId + * @return Promise + */ + getBrick: function (brickId) { + return new Promise(function (resolve, reject) { + QUIAjax.get('package_quiqqer_bricks_ajax_getBrick', resolve, { + 'package': 'quiqqer/bricks', + brickId : brickId, + onError : reject + }); + }); + }, + + /** + * Return the data of a brick + * + * @param {Number|String} brickId + * @param {Object} data - Brick data + * @return Promise + */ + saveBrick: function (brickId, data) { + var self = this; + return new Promise(function (resolve, reject) { + QUIAjax.get('package_quiqqer_bricks_ajax_brick_save', function (result) { + self.fireEvent('brickSave', [brickId, data, result]); + resolve(result); + }, { + 'package': 'quiqqer/bricks', + brickId : brickId, + data : JSON.encode(data), + onError : reject + }); + }); + }, + + /** + * Return the data of a brick + * + * @param {Number|String} brickId + * @param {Object} params + * @return Promise + */ + copyBrick: function (brickId, params) { + var self = this; + + params = params || {}; + + return new Promise(function (resolve, reject) { + QUIAjax.get('package_quiqqer_bricks_ajax_brick_copy', function (result) { + self.fireEvent('brickCopy', [brickId, params, result]); + resolve(result); + }, { + 'package': 'quiqqer/bricks', + brickId : brickId, + params : JSON.encode(params), + onError : reject + }); + }); + }, + + /** + * Return the bricks from a project + * + * @param {String} project - name of the project + * @param {String} lang - Language of the project + * @return Promise + */ + getBricksFromProject: function (project, lang) { + return new Promise(function (resolve, reject) { + QUIAjax.get('package_quiqqer_bricks_ajax_project_getBricks', resolve, { + 'package': 'quiqqer/bricks', + project : JSON.encode({ + name: project, + lang: lang + }), + onError : reject + }); + }); + }, + + /** + * Return all available bricks + * + * @return Promise + */ + getAvailableBricks: function () { + return new Promise(function (resolve, reject) { + QUIAjax.get('package_quiqqer_bricks_ajax_getAvailableBricks', resolve, { + 'package': 'quiqqer/bricks', + onError : reject + }); + }); + }, + + + /** + * Create a new brick + * + * @param {String} project - name of the project + * @param {String} lang - Language of the project + * @param {Object} data - Data of the brick + * + * @return Promise + */ + createBrick: function (project, lang, data) { + var self = this; + + return new Promise(function (resolve, reject) { + QUIAjax.post('package_quiqqer_bricks_ajax_project_createBrick', function (brick) { + self.fireEvent('brickCreate', [brick, project, lang, data]); + resolve(brick); + }, { + 'package': 'quiqqer/bricks', + project : JSON.encode({ + name: project, + lang: lang + }), + data : JSON.encode(data), + onError : reject + }); + }); + }, + + /** + * Delete the Brick-Ids + * + * @param {Array} brickIds - Brick IDs which should be deleted + * @return Promise + */ + deleteBricks: function (brickIds) { + var self = this; + + var panels = QUI.Controls.getByType( + 'package/quiqqer/bricks/bin/BrickEdit' + ); + + return new Promise(function (resolve, reject) { + QUIAjax.post('package_quiqqer_bricks_ajax_brick_delete', function () { + // exist brick panels? + var c, i, len, clen, brickId; + + for (i = 0, len = brickIds.length; i < len; i++) { + brickId = brickIds[i]; + + for (c = 0, clen = panels.length; c < clen; c++) { + if (panels[c].getAttribute('id') === brickId) { + panels[c].destroy(); + } + } + } + + self.fireEvent('brickDelete', [brickIds]); + resolve(); + }, { + 'package': 'quiqqer/bricks', + brickIds : JSON.encode(brickIds), + onError : reject + }); + }); + } + }); +}); -- GitLab