mirror of https://github.com/artf/grapesjs.git
8 changed files with 256 additions and 203 deletions
@ -1,68 +1,20 @@ |
|||
var Backbone = require('backbone'); |
|||
var Asset = require('./Asset'); |
|||
var AssetImage = require('./AssetImage'); |
|||
|
|||
module.exports = Backbone.Collection.extend({ |
|||
|
|||
model: AssetImage, |
|||
|
|||
initialize(models, opt) { |
|||
|
|||
this.model = (attrs, options) => { |
|||
var model; |
|||
switch(attrs.type){ |
|||
default: |
|||
model = new AssetImage(attrs, options); |
|||
} |
|||
return model; |
|||
}; |
|||
|
|||
}, |
|||
|
|||
/** |
|||
* Add new image asset to the collection |
|||
* @param {string} url URL of the image |
|||
* @param {Object} opts Options |
|||
* @return {this} |
|||
* @private |
|||
*/ |
|||
addImg(url, opts) { |
|||
this.add({ |
|||
type: 'image', |
|||
src: url, |
|||
}, opts); |
|||
return this; |
|||
}, |
|||
|
|||
/** |
|||
* Prevent inserting assets with the same 'src' |
|||
* Seems like idAttribute is not working with dynamic model assignament |
|||
* @private |
|||
*/ |
|||
add(models, opt) { |
|||
var mods = []; |
|||
models = models instanceof Array ? models : [models]; |
|||
|
|||
for (var i = 0, len = models.length; i < len; i++) { |
|||
var model = models[i]; |
|||
|
|||
if(typeof model === 'string') |
|||
model = {src: model, type: 'image'}; |
|||
|
|||
if(!model || !model.src) |
|||
continue; |
|||
|
|||
var found = this.where({src: model.src}); |
|||
|
|||
if(!found.length) |
|||
mods.push(model); |
|||
} |
|||
|
|||
if(mods.length == 1) |
|||
mods = mods[0]; |
|||
|
|||
return Backbone.Collection.prototype.add.apply(this, [mods, opt]); |
|||
}, |
|||
|
|||
|
|||
import TypeableCollection from 'domain_abstract/model/TypeableCollection'; |
|||
|
|||
module.exports = require('backbone').Collection.extend(TypeableCollection).extend({ |
|||
getTypes() { |
|||
return [{ |
|||
id: 'image', |
|||
model: require('./AssetImage'), |
|||
view: require('./../view/AssetImageView'), |
|||
isType(value) { |
|||
if (typeof value == 'string') { |
|||
return { |
|||
type: 'image', |
|||
src: value, |
|||
} |
|||
} |
|||
return value; |
|||
} |
|||
}]; |
|||
} |
|||
}); |
|||
|
|||
@ -1,12 +1,23 @@ |
|||
var Backbone = require('backbone'); |
|||
|
|||
module.exports = Backbone.View.extend({ |
|||
initialize(o) { |
|||
|
|||
initialize(o = {}) { |
|||
this.options = o; |
|||
this.collection = o.collection; |
|||
this.config = o.config || {}; |
|||
this.pfx = this.config.stylePrefix || ''; |
|||
this.ppfx = this.config.pStylePrefix || ''; |
|||
this.className = this.pfx + 'asset'; |
|||
this.listenTo( this.model, 'destroy remove', this.remove); |
|||
this.listenTo(this.model, 'destroy remove', this.remove); |
|||
const init = this.init && this.init.bind(this); |
|||
init && init(o); |
|||
}, |
|||
|
|||
render() { |
|||
const el = this.el; |
|||
el.innerHTML = this.template(this, this.model); |
|||
el.className = this.className; |
|||
return this; |
|||
}, |
|||
}); |
|||
|
|||
Loading…
Reference in new issue