Skip to content

Commit

Permalink
Update Asset Manager modal and do not rerender it
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Aug 31, 2017
1 parent 8cc5e97 commit c7a2c85
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 35 deletions.
4 changes: 2 additions & 2 deletions dist/grapes.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "grapesjs",
"description": "Free and Open Source Web Builder Framework",
"version": "0.9.37",
"version": "0.9.38",
"author": "Artur Arseniev",
"license": "BSD-3-Clause",
"homepage": "http://grapesjs.com",
Expand Down
11 changes: 10 additions & 1 deletion src/asset_manager/config/config.js
@@ -1,10 +1,16 @@
module.exports = {
// Default assets
// eg. [
// 'https://...image1.png',
// 'https://...image2.png',
// {type: 'image', src: 'https://...image3.png', someOtherCustomProp: 1},
// ..
// ]
assets: [],

// Content to add where there is no assets to show
// eg. 'No <b>assets</b> here, drag to upload'
noAssets: 'No assets',
noAssets: '',

// Style prefix
stylePrefix: 'am-',
Expand Down Expand Up @@ -67,4 +73,7 @@ module.exports = {

// Any dropzone content to append inside dropzone element
dropzoneContent: '',

// Default title for the asset manager modal
modalTitle: 'Select Image',
};
5 changes: 5 additions & 0 deletions src/asset_manager/index.js
Expand Up @@ -255,6 +255,11 @@ module.exports = () => {
*/
render(assets) {
const toRender = assets || this.getAll().models;

if (!am.rendered) {
am.render();
}

am.collection.reset(toRender);
return this.getContainer();
},
Expand Down
10 changes: 5 additions & 5 deletions src/asset_manager/model/AssetImage.js
Expand Up @@ -2,11 +2,11 @@ const Asset = require('./Asset');

module.exports = Asset.extend({

defaults: _.extend({}, Asset.prototype.defaults, {
type: 'image',
unitDim: 'px',
height: 0,
width: 0,
defaults: Object.assign({}, Asset.prototype.defaults, {
type: 'image',
unitDim: 'px',
height: 0,
width: 0,
}),

});
2 changes: 2 additions & 0 deletions src/asset_manager/view/AssetImageView.js
Expand Up @@ -57,13 +57,15 @@ module.exports = require('./AssetView').extend({
* @private
* */
handleDblClick() {
const em = this.em;
var onDblClick = this.config.onDblClick;
var model = this.model;

if (typeof onDblClick === 'function') {
onDblClick(model);
} else {
this.updateTarget(model.get('src'));
em && em.get('Modal').close();
}

var onSelect = this.collection.onSelect;
Expand Down
8 changes: 5 additions & 3 deletions src/asset_manager/view/AssetView.js
Expand Up @@ -3,9 +3,11 @@ module.exports = Backbone.View.extend({
initialize(o = {}) {
this.options = o;
this.collection = o.collection;
this.config = o.config || {};
this.pfx = this.config.stylePrefix || '';
this.ppfx = this.config.pStylePrefix || '';
const config = o.config || {};
this.config = config;
this.pfx = config.stylePrefix || '';
this.ppfx = config.pStylePrefix || '';
this.em = config.em;
this.className = this.pfx + 'asset';
this.listenTo(this.model, 'destroy remove', this.remove);
const init = this.init && this.init.bind(this);
Expand Down
22 changes: 13 additions & 9 deletions src/asset_manager/view/AssetsView.js
Expand Up @@ -38,7 +38,7 @@ module.exports = Backbone.View.extend({
this.pfx = this.config.stylePrefix || '';
this.ppfx = this.config.pStylePrefix || '';
const coll = this.collection;
this.listenTo(coll, 'reset', this.render);
this.listenTo(coll, 'reset', this.renderAssets);
this.listenTo(coll, 'add', this.addToAsset);
this.listenTo(coll, 'remove', this.removedAsset);
this.listenTo(coll, 'deselectAll', this.deselectAll);
Expand Down Expand Up @@ -166,18 +166,22 @@ module.exports = Backbone.View.extend({
this.$el.find(`.${pfx}highlight`).removeClass(`${pfx}highlight`);
},

renderAssets() {
const fragment = document.createDocumentFragment();
const assets = this.$el.find(`.${this.pfx}assets`);
assets.empty();
this.toggleNoAssets(this.collection.length);
this.collection.each((model) => this.addAsset(model, fragment));
assets.append(fragment);
},

render() {
const pfx = this.pfx;
const ppfx = this.ppfx;
//const noAssets = this.config.noAssets;
const fuRendered = this.options.fu.render().el;
const fragment = document.createDocumentFragment();
this.$el.empty();
this.collection.each((model) => this.addAsset(model, fragment));
this.$el.append(fuRendered).append(this.template(this));
this.el.className = `${ppfx}asset-manager`;
this.toggleNoAssets(this.collection.length);
this.$el.find(`.${pfx}assets`).append(fragment);
this.el.className = `${this.ppfx}asset-manager`;
this.renderAssets();
this.rendered = 1;
return this;
}
});
29 changes: 16 additions & 13 deletions src/commands/view/OpenAssets.js
@@ -1,21 +1,24 @@
module.exports = {

run(editor, sender, opts) {
var opt = opts || {};
var config = editor.getConfig();
var modal = editor.Modal;
var assetManager = editor.AssetManager;
run(editor, sender, opts = {}) {
const modal = editor.Modal;
const am = editor.AssetManager;
const config = am.getConfig();
const title = opts.modalTitle || config.modalTitle || '';

assetManager.onClick(opt.onClick);
assetManager.onDblClick(opt.onDblClick);
am.setTarget(opts.target);
am.onClick(opts.onClick);
am.onDblClick(opts.onDblClick);
am.onSelect(opts.onSelect);

// old API
assetManager.setTarget(opt.target);
assetManager.onSelect(opt.onSelect);
assetManager.render();
if (!this.rendered) {
console.log('render now');
am.render();
this.rendered = 1;
}

modal.setTitle(opt.modalTitle || 'Select image');
modal.setContent(assetManager.getContainer());
modal.setTitle(title);
modal.setContent(am.getContainer());
modal.open();
},

Expand Down
2 changes: 1 addition & 1 deletion src/style_manager/view/PropertyFileView.js
Expand Up @@ -125,7 +125,7 @@ module.exports = PropertyView.extend({

if(editor) {
this.modal.setTitle('Select image');
this.modal.setContent(this.am.render());
this.modal.setContent(this.am.getContainer());
this.am.setTarget(null);
editor.runCommand('open-assets', {
target: this.model,
Expand Down

0 comments on commit c7a2c85

Please sign in to comment.