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) * * [add](#add)
* * [get](#get) * * [get](#get)
* * [getAll](#getall) * * [getAll](#getall)
@ -13,35 +14,57 @@
* ``` * ```
* *
* @module BlockManager * @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) { define(function(require) {
return function(config) { return function() {
var c = config || {}, var c = {},
defaults = require('./config/config'), defaults = require('./config/config'),
Blocks = require('./model/Blocks'), Blocks = require('./model/Blocks'),
BlocksView = require('./view/BlocksView'); BlocksView = require('./view/BlocksView');
var blocks, view;
for (var name in defaults) { return {
if (!(name in c))
c[name] = defaults[name];
}
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. * Add new block to the collection.

13
src/editor/model/Editor.js

@ -76,7 +76,7 @@ define([
this.initCanvas(); this.initCanvas();
this.initUndoManager(); this.initUndoManager();
this.initStyleManager(); this.initStyleManager();
this.initBlockManager(); // Requres utils, canvas this.loadModule('BlockManager'); // Requres utils, canvas
this.on('change:selectedComponent', this.componentSelected, this); this.on('change:selectedComponent', this.componentSelected, this);
}, },
@ -120,17 +120,6 @@ define([
this.set('Editor', editor); 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 * Initialize device manager
* @private * @private

2
test/specs/block_manager/main.js

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

Loading…
Cancel
Save