Browse Source

Refactor Block Manager

pull/36/head
Artur Arseniev 10 years ago
parent
commit
9ef24588d1
  1. 69
      src/block_manager/main.js
  2. 13
      src/editor/model/Editor.js
  3. 2
      test/specs/block_manager/main.js

69
src/block_manager/main.js

@ -1,4 +1,5 @@
/**
* * [init](#init)
* * [add](#add)
* * [get](#get)
* * [getAll](#getall)
@ -13,35 +14,57 @@
* ```
*
* @module BlockManager
* @param {Object} config Configurations
* @param {Array<Object>} [config.blocks=[]] Default blocks
* @example
* ...
* blockManager: {
* blocks: [
* {id:'h1-block' label: 'Heading', content:'<h1>...</h1>'},
* ...
* ],
* }
* ...
*/
define(function(require) {
return function(config) {
var c = config || {},
defaults = require('./config/config'),
Blocks = require('./model/Blocks'),
BlocksView = require('./view/BlocksView');
return function() {
var c = {},
defaults = require('./config/config'),
Blocks = require('./model/Blocks'),
BlocksView = require('./view/BlocksView');
var blocks, view;
for (var name in defaults) {
if (!(name in c))
c[name] = defaults[name];
}
return {
var blocks = new Blocks(c.blocks);
var view = new BlocksView({ collection: blocks }, c);
/**
* Name of the module
* @type {String}
* @private
*/
name: 'BlockManager',
return {
/**
* Indicates if module is public
* @type {Boolean}
* @private
*/
public: true,
/**
* Initialize module. Automatically called with a new instance of the editor
* @param {Object} config Configurations
* @param {Array<Object>} [config.blocks=[]] Default blocks
* @example
* ...
* {
* blocks: [
* {id:'h1-block' label: 'Heading', content:'<h1>...</h1>'},
* ...
* ],
* }
* ...
* @return {this}
*/
init: function(config) {
c = config || {};
for (var name in defaults) {
if (!(name in c))
c[name] = defaults[name];
}
blocks = new Blocks(c.blocks);
view = new BlocksView({ collection: blocks }, c);
return this;
},
/**
* Add new block to the collection.

13
src/editor/model/Editor.js

@ -76,7 +76,7 @@ define([
this.initCanvas();
this.initUndoManager();
this.initStyleManager();
this.initBlockManager(); // Requres utils, canvas
this.loadModule('BlockManager'); // Requres utils, canvas
this.on('change:selectedComponent', this.componentSelected, this);
},
@ -120,17 +120,6 @@ define([
this.set('Editor', editor);
},
/**
* Initialize block manager
* @private
* */
initBlockManager: function(){
var cfg = this.config.blockManager;
cfg.em = this;
cfg.pStylePrefix = this.config.stylePrefix;
this.set('BlockManager', new BlockManager(cfg));
},
/**
* Initialize device manager
* @private

2
test/specs/block_manager/main.js

@ -19,7 +19,7 @@ define([ 'BlockManager',
label: 'Heading',
content: '<h1>Test</h1>'
};
obj = new BlockManager();
obj = new BlockManager().init();
});
afterEach(function () {

Loading…
Cancel
Save