mirror of https://github.com/artf/grapesjs.git
10 changed files with 189 additions and 2 deletions
@ -0,0 +1,7 @@ |
|||
define(function () { |
|||
return { |
|||
|
|||
// Default classes
|
|||
classes : [], |
|||
}; |
|||
}); |
|||
@ -0,0 +1,47 @@ |
|||
define(function(require) { |
|||
/** |
|||
* @class ClassManager |
|||
* @param {Object} config Configurations |
|||
* |
|||
* */ |
|||
var ClassManager = function(config) |
|||
{ |
|||
var c = config || {}, |
|||
def = require('./config/config'), |
|||
Classes = require('./model/Classes'); |
|||
|
|||
for (var name in def) { |
|||
if (!(name in c)) |
|||
c[name] = def[name]; |
|||
} |
|||
|
|||
this.classes = new Classes(c.classes); |
|||
|
|||
this.Classes = Classes; |
|||
}; |
|||
|
|||
ClassManager.prototype = { |
|||
|
|||
/** |
|||
* Get collection of classes |
|||
* |
|||
* @return {Object} |
|||
* */ |
|||
getClasses : function() { |
|||
return this.classes; |
|||
}, |
|||
|
|||
/** |
|||
* Get class by its name |
|||
* |
|||
* @return {Object|null} |
|||
* */ |
|||
getClass : function(id) { |
|||
var res = this.classes.where({name: id}); |
|||
return res.length ? res[0] : null; |
|||
}, |
|||
}; |
|||
|
|||
return ClassManager; |
|||
|
|||
}); |
|||
@ -0,0 +1,14 @@ |
|||
define(['backbone'], |
|||
function (Backbone) { |
|||
/** |
|||
* @class Class |
|||
* */ |
|||
return Backbone.Model.extend({ |
|||
|
|||
defaults: { |
|||
label: '', |
|||
name: '', |
|||
}, |
|||
|
|||
}); |
|||
}); |
|||
@ -0,0 +1,11 @@ |
|||
define(['backbone','./Class'], |
|||
function (Backbone, Class) { |
|||
/** |
|||
* @class Classes |
|||
* */ |
|||
return Backbone.Collection.extend({ |
|||
|
|||
model: Class, |
|||
|
|||
}); |
|||
}); |
|||
@ -0,0 +1,93 @@ |
|||
define(['backbone', 'text!./../template/classItems.html'], |
|||
function (Backbone, itemsTemplate) { |
|||
/** |
|||
* @class ClassItemsView |
|||
* */ |
|||
return Backbone.View.extend({ |
|||
|
|||
template: _.template(itemsTemplate), |
|||
|
|||
events:{ |
|||
'click .add': 'startNewClass', |
|||
'keyup .input' : 'onInputKeyUp' |
|||
}, |
|||
|
|||
initialize: function(o) { |
|||
this.events['click #'+this.pfx+'add'] = 'addLayer'; |
|||
|
|||
if(!this.layers){ |
|||
this.layers = new Layers(); |
|||
this.model.set('layers', this.layers); |
|||
this.$layers = new LayersView({ |
|||
collection : this.layers, |
|||
stackModel : this.model, |
|||
preview : this.model.get('preview'), |
|||
config : o.config |
|||
}); |
|||
} |
|||
|
|||
this.targetCl = this.target.classes; |
|||
|
|||
this.listenTo( this.targetCl, 'add', this.addClass); |
|||
this.listenTo( this.targetCl, 'reset', this.renderClasses); |
|||
|
|||
this.delegateEvents(); |
|||
}, |
|||
|
|||
onInputKeyUp: function(e){ |
|||
if (e.keyCode === 13) { |
|||
this.addItem(); |
|||
}else{ |
|||
this.searchItem(); |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* Add new object to collection |
|||
* @param Object Model |
|||
* @param Object Fragment collection |
|||
* |
|||
* @return Object Object created |
|||
* */ |
|||
addToClasses: function(model, fragmentEl){ |
|||
var fragment = fragmentEl || null; |
|||
var viewObject = ClassTagView; |
|||
|
|||
var view = new viewObject({ |
|||
model: model, |
|||
config: this.config, |
|||
}); |
|||
var rendered = view.render().el; |
|||
|
|||
if(fragment) |
|||
fragment.appendChild( rendered ); |
|||
else |
|||
this.$classes.append(rendered); |
|||
|
|||
return rendered; |
|||
}, |
|||
|
|||
renderClasses: function() { |
|||
var fragment = document.createDocumentFragment(); |
|||
this.$classes.empty(); |
|||
|
|||
this.collection.each(function(model){ |
|||
this.addToClasses(model, fragment); |
|||
},this); |
|||
|
|||
this.$classes.append(fragment); |
|||
return this; |
|||
}, |
|||
|
|||
|
|||
/** @inheritdoc */ |
|||
render : function(){ |
|||
this.renderLabel(); |
|||
this.renderField(); |
|||
this.renderLayers(); |
|||
this.$el.attr('class', this.className); |
|||
return this; |
|||
}, |
|||
|
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue