mirror of https://github.com/artf/grapesjs.git
14 changed files with 248 additions and 108 deletions
@ -0,0 +1,115 @@ |
|||||
|
define(function(require) { |
||||
|
/** |
||||
|
* @class CssManager |
||||
|
* @param {Object} config Configurations |
||||
|
* |
||||
|
* */ |
||||
|
var CssManager = function(config) |
||||
|
{ |
||||
|
var c = config || {}, |
||||
|
def = require('./config/config'), |
||||
|
CssRule = require('./model/CssRule'), |
||||
|
CssRules = require('./model/CssRules'); |
||||
|
|
||||
|
for (var name in def) { |
||||
|
if (!(name in c)) |
||||
|
c[name] = def[name]; |
||||
|
} |
||||
|
|
||||
|
//this.qset = { '' : CssRules, '340px': CssRules };
|
||||
|
var rules = new CssRules([]); |
||||
|
|
||||
|
return { |
||||
|
|
||||
|
/** |
||||
|
* Add new class to collection only if it's not already exists |
||||
|
* @param {String} name Class name |
||||
|
* |
||||
|
* @return {Object} Model class |
||||
|
* */ |
||||
|
addRule: function(name){ |
||||
|
/* |
||||
|
var label = name; |
||||
|
var c = this.getRule(name); |
||||
|
if(!c) |
||||
|
return this.classes.add({name: name, label: label}); |
||||
|
return c; |
||||
|
*/ |
||||
|
}, |
||||
|
|
||||
|
/** |
||||
|
* Get class by its name |
||||
|
* @param {Array} selectors Array of selectors |
||||
|
* @param {String} state Rule status |
||||
|
* @param {String} set Query set |
||||
|
* |
||||
|
* @return {Object|null} |
||||
|
* */ |
||||
|
getRule : function(selectors, state, set) { |
||||
|
var req = _.pluck(selectors, 'cid'); |
||||
|
fRule = null; |
||||
|
rules.each(function(rule){ |
||||
|
if(fRule) |
||||
|
return; |
||||
|
|
||||
|
var sel = _.pluck(rule.get('selectors'), 'cid'); |
||||
|
|
||||
|
if(this.same(req, sel)) |
||||
|
fRule = rule; |
||||
|
}, this); |
||||
|
return fRule; |
||||
|
}, |
||||
|
|
||||
|
/** |
||||
|
* Create new rule |
||||
|
* @param {Array} selectors Array of selectors |
||||
|
* @param {String} state Rule status |
||||
|
* @param {String} set Query set |
||||
|
* |
||||
|
* @return {Object} |
||||
|
* */ |
||||
|
newRule: function(selectors, state, set) { |
||||
|
var rule = new CssRule({ state: state }); |
||||
|
rule.get('selectors').add(selectors); |
||||
|
return rule; |
||||
|
}, |
||||
|
|
||||
|
/** |
||||
|
* Compare 2 arrays to check if are the same |
||||
|
* @param {Array} arr1 |
||||
|
* @param {Array} arr2 |
||||
|
* @return {Boolean} |
||||
|
*/ |
||||
|
same: function(a1, a2){ |
||||
|
if(a1.length !== a2.length) |
||||
|
return; |
||||
|
|
||||
|
for (var i = 0; i < a1.length; i++) { |
||||
|
var f = 0; |
||||
|
|
||||
|
for (var j = 0; j < a2.length; j++) { |
||||
|
if (a1[i] === a2[j]) |
||||
|
f = 1; |
||||
|
} |
||||
|
|
||||
|
if(f === 0) |
||||
|
return; |
||||
|
} |
||||
|
return true; |
||||
|
}, |
||||
|
|
||||
|
/** |
||||
|
* Get collection of css rules |
||||
|
* |
||||
|
* @return {Object} |
||||
|
* */ |
||||
|
getRules : function() { |
||||
|
return rules; |
||||
|
}, |
||||
|
|
||||
|
}; |
||||
|
}; |
||||
|
|
||||
|
return CssManager; |
||||
|
|
||||
|
}); |
||||
@ -0,0 +1,22 @@ |
|||||
|
define(['backbone', './Selectors'], |
||||
|
function (Backbone, Selectors) { |
||||
|
/** |
||||
|
* @class CssRule |
||||
|
* */ |
||||
|
return Backbone.Model.extend({ |
||||
|
|
||||
|
defaults: { |
||||
|
selectors: {}, |
||||
|
style: {}, |
||||
|
stylable: true, |
||||
|
state: '', |
||||
|
}, |
||||
|
|
||||
|
initialize: function(c, opt) { |
||||
|
this.config = c || {}; |
||||
|
this.slct = this.config.selectors || []; |
||||
|
this.set('selectors', new Selectors(this.slct)); |
||||
|
}, |
||||
|
|
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,29 @@ |
|||||
|
define([ 'backbone', 'require'], |
||||
|
function (Backbone, require) { |
||||
|
/** |
||||
|
* @class Selectors |
||||
|
* */ |
||||
|
|
||||
|
return Backbone.Collection.extend({ |
||||
|
|
||||
|
initialize: function(models, opt){ |
||||
|
|
||||
|
this.model = function(attrs, opts) { |
||||
|
var model; |
||||
|
|
||||
|
switch(1){ |
||||
|
|
||||
|
default: |
||||
|
if(!this.ClassTag) |
||||
|
this.ClassTag = require("ClassManager/model/ClassTag"); |
||||
|
model = new this.ClassTag(attrs, opts); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
return model; |
||||
|
}; |
||||
|
|
||||
|
}, |
||||
|
|
||||
|
}); |
||||
|
}); |
||||
@ -1,64 +0,0 @@ |
|||||
define(function(require) { |
|
||||
/** |
|
||||
* @class CssManager |
|
||||
* @param {Object} config Configurations |
|
||||
* |
|
||||
* */ |
|
||||
var CssManager = function(config) |
|
||||
{ |
|
||||
var c = config || {}, |
|
||||
def = require('./config/config'); |
|
||||
this.CssRules = require('./model/CssRules'); |
|
||||
|
|
||||
for (var name in def) { |
|
||||
if (!(name in c)) |
|
||||
c[name] = def[name]; |
|
||||
} |
|
||||
|
|
||||
this.rules = new this.CssRules([]); |
|
||||
this.config = c; |
|
||||
}; |
|
||||
|
|
||||
CssManager.prototype = { |
|
||||
|
|
||||
/** |
|
||||
* Add new class to collection only if it's not already exists |
|
||||
* @param {String} name Class name |
|
||||
* |
|
||||
* @return {Object} Model class |
|
||||
* */ |
|
||||
addRule: function(name){ |
|
||||
var label = name; |
|
||||
var c = this.getClass(name); |
|
||||
if(!c) |
|
||||
return this.classes.add({name: name, label: label}); |
|
||||
return c; |
|
||||
}, |
|
||||
|
|
||||
/** |
|
||||
* Get class by its name |
|
||||
* @param {Array[String]} ids Array of ids |
|
||||
* @param {String} status Rule status |
|
||||
* @param {String} set Query set |
|
||||
* |
|
||||
* @return {Object|null} |
|
||||
* */ |
|
||||
getRule : function(ids, status, set) { |
|
||||
var res = this.classes.where({name: id}); |
|
||||
return res.length ? res[0] : null; |
|
||||
}, |
|
||||
|
|
||||
/** |
|
||||
* Get collection of css rules |
|
||||
* |
|
||||
* @return {Object} |
|
||||
* */ |
|
||||
getRules : function() { |
|
||||
return this.rules; |
|
||||
}, |
|
||||
|
|
||||
}; |
|
||||
|
|
||||
return ClassManager; |
|
||||
|
|
||||
}); |
|
||||
@ -1,16 +0,0 @@ |
|||||
define(['backbone'], |
|
||||
function (Backbone) { |
|
||||
/** |
|
||||
* @class CssRule |
|
||||
* */ |
|
||||
return Backbone.Model.extend({ |
|
||||
|
|
||||
defaults: { |
|
||||
classes: {}, |
|
||||
style: {}, |
|
||||
stylable: true, |
|
||||
state: '', |
|
||||
}, |
|
||||
|
|
||||
}); |
|
||||
}); |
|
||||
Loading…
Reference in new issue