mirror of https://github.com/artf/grapesjs.git
9 changed files with 137 additions and 272 deletions
@ -1,254 +1,199 @@ |
|||||
|
/** |
||||
|
* - [addGenerator](#addgenerator) |
||||
|
* - [getGenerator](#getgenerator) |
||||
|
* - [getGenerators](#getgenerators) |
||||
|
* - [addViewer](#addviewer) |
||||
|
* - [getViewer](#getviewer) |
||||
|
* - [getViewers](#getviewers) |
||||
|
* - [updateViewer](#updateviewer) |
||||
|
* - [getCode](#getcode) |
||||
|
* |
||||
|
* |
||||
|
* Before using methods you should get first the module from the editor instance, in this way: |
||||
|
* |
||||
|
* ```js
|
||||
|
* var codeManager = editor.get('CodeManager'); |
||||
|
* ``` |
||||
|
* |
||||
|
* @module CodeManager |
||||
|
*/ |
||||
define(function(require) { |
define(function(require) { |
||||
/** |
|
||||
* @class CodeManager |
var CodeManager = function(config) { |
||||
* @param {Object} Configurations |
|
||||
* |
var c = config || {}, |
||||
* @return {Object} |
defaults = require('./config/config'), |
||||
* */ |
gHtml = require('./model/HtmlGenerator'), |
||||
function CodeManager(config) |
gCss = require('./model/CssGenerator'), |
||||
{ |
gJson = require('./model/JsonGenerator'), |
||||
var c = config || {}, |
eCM = require('./model/CodeMirrorEditor'), |
||||
defaults = require('./config/config'), |
editorView = require('./view/EditorView'); |
||||
gInterface = require('./model/GeneratorInterface'), |
|
||||
gHtml = require('./model/HtmlGenerator'), |
|
||||
gCss = require('./model/CssGenerator'), |
|
||||
gJson = require('./model/JsonGenerator'), |
|
||||
eInterface = require('./model/EditorInterface'), |
|
||||
eCM = require('./model/CodeMirrorEditor'), |
|
||||
editorView = require('./view/EditorView'); |
|
||||
|
|
||||
for (var name in defaults) { |
for (var name in defaults) { |
||||
if (!(name in c)) |
if (!(name in c)) |
||||
c[name] = defaults[name]; |
c[name] = defaults[name]; |
||||
} |
} |
||||
|
|
||||
this.gi = new gInterface(); |
var generators = {}, |
||||
this.generators = {}; |
defGenerators = {}, |
||||
this.defaultGenerators = {}; |
viewers = {}, |
||||
this.currentGenerator = null; |
defViewers = {}, |
||||
|
geHtml = new gHtml(), |
||||
|
geCss = new gCss(), |
||||
|
geJson = new gJson(), |
||||
|
edCM = new eCM(); |
||||
|
|
||||
this.ei = new eInterface(); |
defGenerators.html = geHtml; |
||||
this.editors = {}; |
defGenerators.css = geCss; |
||||
this.defaultEditors = {}; |
defGenerators.json = geJson; |
||||
this.currentEditor = null; |
|
||||
|
|
||||
var geHtml = new gHtml(), |
defViewers.CodeMirror = edCM; |
||||
geCss = new gCss(), |
|
||||
geJson = new gJson(), |
|
||||
edCM = new eCM(); |
|
||||
|
|
||||
this.defaultGenerators[geHtml.getId()] = geHtml; |
return { |
||||
this.defaultGenerators[geCss.getId()] = geCss; |
|
||||
this.defaultGenerators[geJson.getId()] = geJson; |
|
||||
|
|
||||
this.defaultEditors[edCM.getId()] = edCM; |
config: c, |
||||
|
|
||||
this.EditorView = editorView; |
EditorView: editorView, |
||||
this.config = c; |
|
||||
} |
|
||||
|
|
||||
CodeManager.prototype = { |
|
||||
|
|
||||
/** |
/** |
||||
* Add new code generator |
* Add new code generator to the collection |
||||
* @param {GeneratorInterface} generator |
* @param {string} id Code generator ID |
||||
* |
* @param {Object} generator Code generator wrapper |
||||
* @return this |
* @param {Function} generator.build Function that builds the code |
||||
|
* @return {this} |
||||
|
* @example |
||||
|
* codeManager.addGenerator('html7',{ |
||||
|
* build: function(model){ |
||||
|
* return 'myCode'; |
||||
|
* } |
||||
|
* }); |
||||
* */ |
* */ |
||||
addGenerator : function(generator) |
addGenerator: function(id, generator) { |
||||
{ |
generators[id] = generator; |
||||
// Check interface implementation
|
|
||||
for (var method in this.gi) |
|
||||
if(!generator[method]){ |
|
||||
console.warn("addGenerator: method '"+ method +"' was not found"); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
var id = generator.getId(); |
|
||||
this.generators[id] = generator; |
|
||||
|
|
||||
if(!this.currentGenerator) |
|
||||
this.currentGenerator = id; |
|
||||
|
|
||||
return this; |
return this; |
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Returns generator |
* Get code generator by id |
||||
* @param {String}|{Integer} id Generator ID |
* @param {string} id Code generator ID |
||||
* |
* @return {Object|null} |
||||
* @return {GeneratorInterface}|null |
* @example |
||||
|
* var generator = codeManager.getGenerator('html7'); |
||||
|
* generator.build = function(model){ |
||||
|
* //extend
|
||||
|
* }; |
||||
* */ |
* */ |
||||
getGenerator : function(id) |
getGenerator: function(id) { |
||||
{ |
return generators[id] || null; |
||||
if(id && this.generators[id]) |
|
||||
generator = this.generators[id]; |
|
||||
|
|
||||
return generator ? generator : null; |
|
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Returns generators |
* Returns all code generators |
||||
* |
* @return {Array<Object>} |
||||
* @return {Array} |
|
||||
* */ |
* */ |
||||
getGenerators : function() |
getGenerators: function() { |
||||
{ |
return generators; |
||||
return this.generators; |
|
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Get current generator |
* Add new code viewer |
||||
* |
* @param {string} id Code viewer ID |
||||
* @return {GeneratorInterface} |
* @param {Object} viewer Code viewer wrapper |
||||
|
* @param {Function} viewer.init Set element on which viewer will be displayed |
||||
|
* @param {Function} viewer.setContent Set content to the viewer |
||||
|
* @return {this} |
||||
|
* @example |
||||
|
* codeManager.addViewer('ace',{ |
||||
|
* init: function(el){ |
||||
|
* var ace = require('ace-editor'); |
||||
|
* this.editor = ace.edit(el.id); |
||||
|
* }, |
||||
|
* setContent: function(code){ |
||||
|
* this.editor.setValue(code); |
||||
|
* } |
||||
|
* }); |
||||
* */ |
* */ |
||||
getCurrentGenerator : function() |
addViewer: function(id, viewer) { |
||||
{ |
viewers[id] = viewer; |
||||
if(!this.currentGenerator) |
|
||||
this.loadDefaultGenerators(); |
|
||||
return this.getGenerator(this.currentGenerator); |
|
||||
}, |
|
||||
|
|
||||
/** |
|
||||
* Set current generator |
|
||||
* @param {Integer} id Generator ID |
|
||||
* |
|
||||
* @return this |
|
||||
* */ |
|
||||
setCurrentGenerator : function(id) |
|
||||
{ |
|
||||
this.currentGenerator = id; |
|
||||
return this; |
return this; |
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Load default generators |
* Get code viewer by id |
||||
* |
* @param {string} id Code viewer ID |
||||
* @return this |
* @return {Object|null} |
||||
|
* @example |
||||
|
* var viewer = codeManager.getViewer('ace'); |
||||
* */ |
* */ |
||||
loadDefaultGenerators : function() |
getViewer: function(id) { |
||||
{ |
return viewers[id] || null; |
||||
for (var id in this.defaultGenerators) { |
|
||||
this.addGenerator(this.defaultGenerators[id]); |
|
||||
} |
|
||||
|
|
||||
return this; |
|
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Add new editor |
* Returns all code viewers |
||||
* @param {EditorInterface} editor |
* @return {Array<Object>} |
||||
* |
|
||||
* @return this |
|
||||
* */ |
* */ |
||||
addEditor : function(editor) |
getViewers: function() { |
||||
{ |
return viewers; |
||||
// Check interface implementation
|
|
||||
for (var method in this.ei) |
|
||||
if(!editor[method]){ |
|
||||
console.warn("addEditor: method '"+ method +"' was not found"); |
|
||||
return; |
|
||||
} |
|
||||
|
|
||||
var id = editor.getId(); |
|
||||
this.editors[id] = editor; |
|
||||
|
|
||||
if(!this.currentEditor) |
|
||||
this.currentEditor = id; |
|
||||
|
|
||||
return this; |
|
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Returns editor |
* Update code viewer content |
||||
* @param {String}|{Integer} id Editor ID |
* @param {Object} viewer Viewer instance |
||||
* |
* @param {string} code Code string |
||||
* @return {EditorInterface}|null |
* @example |
||||
|
* var AceViewer = codeManager.getViewer('ace'); |
||||
|
* // ...
|
||||
|
* var viewer = AceViewer.init(el); |
||||
|
* // ...
|
||||
|
* codeManager.updateViewer(AceViewer, 'code'); |
||||
* */ |
* */ |
||||
getEditor : function(id) |
updateViewer: function(viewer, code) { |
||||
{ |
viewer.setContent(code); |
||||
if(id && this.editors[id]) |
|
||||
editor = this.editors[id]; |
|
||||
|
|
||||
return editor ? editor : null; |
|
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Returns editors |
* Get code from model |
||||
* |
* @param {Object} model Any kind of model that will be passed to the build method of generator |
||||
* @return {Array} |
* @param {string} genId Code generator id |
||||
|
* @param {Object} [opt] Options |
||||
|
* @return {string} |
||||
|
* @example |
||||
|
* var codeStr = codeManager.getCode(model, 'html'); |
||||
* */ |
* */ |
||||
getEditors : function() |
getCode: function(model, genId, opt) { |
||||
{ |
var generator = this.getGenerator(genId); |
||||
return this.editors; |
return generator ? generator.build(model, opt) : ''; |
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Get current editor |
* Load default code generators |
||||
* |
* @return {this} |
||||
* @return {EditorInterface} |
* @private |
||||
* */ |
* */ |
||||
getCurrentEditor : function() |
loadDefaultGenerators: function() { |
||||
{ |
for (var id in defGenerators) |
||||
if(!this.currentEditor) |
this.addGenerator(id, defGenerators[id]); |
||||
this.loadDefaultEditors(); |
|
||||
return this.getEditor(this.currentEditor); |
|
||||
}, |
|
||||
|
|
||||
/** |
|
||||
* Set current editor |
|
||||
* @param {Integer} id Editor ID |
|
||||
* |
|
||||
* @return this |
|
||||
* */ |
|
||||
setCurrentEditor : function(id) |
|
||||
{ |
|
||||
this.currentEditor = id; |
|
||||
return this; |
return this; |
||||
}, |
}, |
||||
|
|
||||
/** |
/** |
||||
* Load default editors |
* Load default code viewers |
||||
* |
* @return {this} |
||||
* @return this |
* @private |
||||
* */ |
* */ |
||||
loadDefaultEditors : function() |
loadDefaultViewers: function() { |
||||
{ |
for (var id in defViewers) |
||||
for (var id in this.defaultEditors) { |
this.addViewer(id, defViewers[id]); |
||||
this.addEditor(this.defaultEditors[id]); |
|
||||
} |
|
||||
|
|
||||
return this; |
return this; |
||||
}, |
}, |
||||
|
|
||||
/** |
}; |
||||
* Get code by name |
|
||||
* @param {Backbone.Model} model Model |
|
||||
* @param {String}|{Integer} v Id of code generator |
|
||||
* |
|
||||
* @return {String}|null |
|
||||
* */ |
|
||||
getCode : function(model, v, em) |
|
||||
{ |
|
||||
var id = v || this.currentGenerator, |
|
||||
generator = this.generators[id]; |
|
||||
return generator ? generator.build(model, em) : null; |
|
||||
}, |
|
||||
|
|
||||
/** |
|
||||
* Update editor content |
|
||||
* @param {EditorInteface} editor Editor |
|
||||
* @param {String} code Code value |
|
||||
* |
|
||||
* @return void |
|
||||
* */ |
|
||||
updateEditor : function(editor, code) |
|
||||
{ |
|
||||
editor.setContent(code); |
|
||||
}, |
|
||||
|
|
||||
|
|
||||
}; |
}; |
||||
|
|
||||
return CodeManager; |
return CodeManager; |
||||
|
|
||||
}); |
}); |
||||
@ -1,31 +0,0 @@ |
|||||
define(function() { |
|
||||
/** |
|
||||
* @class EditorInterface |
|
||||
* */ |
|
||||
function EditorInterface() {} |
|
||||
|
|
||||
EditorInterface.prototype = { |
|
||||
|
|
||||
/** |
|
||||
* Get id |
|
||||
* |
|
||||
* @return {String}|{Integer} |
|
||||
* */ |
|
||||
getId : function(){}, |
|
||||
|
|
||||
/** |
|
||||
* Set content |
|
||||
* @param {String} str |
|
||||
* |
|
||||
* */ |
|
||||
setContent : function(str){}, |
|
||||
|
|
||||
/** |
|
||||
* Init editor |
|
||||
* @param {Object} el DOM element |
|
||||
* */ |
|
||||
init : function(el){}, |
|
||||
}; |
|
||||
|
|
||||
return EditorInterface; |
|
||||
}); |
|
||||
@ -1,26 +0,0 @@ |
|||||
define(function() { |
|
||||
/** |
|
||||
* @class GeneratorInterface |
|
||||
* */ |
|
||||
function GeneratorInterface() {} |
|
||||
|
|
||||
GeneratorInterface.prototype = { |
|
||||
|
|
||||
/** |
|
||||
* Get id |
|
||||
* |
|
||||
* @return {String}|{Integer} |
|
||||
* */ |
|
||||
getId : function(){}, |
|
||||
|
|
||||
/** |
|
||||
* Generate code from model |
|
||||
* @param {Backbone.Model} model |
|
||||
* |
|
||||
* @return {String} |
|
||||
* */ |
|
||||
build : function(model){}, |
|
||||
}; |
|
||||
|
|
||||
return GeneratorInterface; |
|
||||
}); |
|
||||
Loading…
Reference in new issue