Browse Source

Update block manager APIs

pull/202/head
Artur Arseniev 9 years ago
parent
commit
cd6d22b61d
  1. 21
      src/block_manager/index.js
  2. 1
      src/block_manager/view/BlocksView.js
  3. 6
      test/specs/block_manager/index.js

21
src/block_manager/index.js

@ -2,6 +2,7 @@
* * [add](#add) * * [add](#add)
* * [get](#get) * * [get](#get)
* * [getAll](#getall) * * [getAll](#getall)
* * [getCategories](#getcategories)
* * [render](#render) * * [render](#render)
* *
* Block manager helps managing various, draggable, piece of contents that could be easily reused inside templates. * Block manager helps managing various, draggable, piece of contents that could be easily reused inside templates.
@ -32,6 +33,7 @@ module.exports = () => {
BlockCategories = require('./model/Categories'), BlockCategories = require('./model/Categories'),
BlocksView = require('./view/BlocksView'); BlocksView = require('./view/BlocksView');
var blocks, view; var blocks, view;
var categories = [];
return { return {
@ -55,9 +57,10 @@ module.exports = () => {
c[name] = defaults[name]; c[name] = defaults[name];
} }
blocks = new Blocks(c.blocks); blocks = new Blocks(c.blocks);
categories = new BlockCategories(),
view = new BlocksView({ view = new BlocksView({
collection: blocks, collection: blocks,
categories: new BlockCategories(), categories,
}, c); }, c);
return this; return this;
}, },
@ -68,12 +71,18 @@ module.exports = () => {
* @param {Object} opts Options * @param {Object} opts Options
* @param {string} opts.label Name of the block * @param {string} opts.label Name of the block
* @param {string} opts.content HTML content * @param {string} opts.content HTML content
* @param {string|Object} opts.category Group the block inside a catgegory.
* You should pass objects with id property, eg:
* {id: 'some-uid', label: 'My category'}
* The string will be converted in:
* 'someid' => {id: 'someid', label: 'someid'}
* @param {Object} [opts.attributes={}] Block attributes * @param {Object} [opts.attributes={}] Block attributes
* @return {Block} Added block * @return {Block} Added block
* @example * @example
* blockManager.add('h1-block', { * blockManager.add('h1-block', {
* label: 'Heading', * label: 'Heading',
* content: '<h1>Put your title here</h1>', * content: '<h1>Put your title here</h1>',
* category: 'Basic',
* attributes: { * attributes: {
* title: 'Insert h1 block' * title: 'Insert h1 block'
* } * }
@ -86,7 +95,7 @@ module.exports = () => {
}, },
/** /**
* Return block by id * Return the block by id
* @param {string} id Block id * @param {string} id Block id
* @example * @example
* var block = blockManager.get('h1-block'); * var block = blockManager.get('h1-block');
@ -109,6 +118,14 @@ module.exports = () => {
return blocks; return blocks;
}, },
/**
* Get all available categories
* @return {Array|Collection}
*/
getCategories() {
return categories;
},
/** /**
* Render blocks * Render blocks
* @return {HTMLElement} * @return {HTMLElement}

1
src/block_manager/view/BlocksView.js

@ -116,6 +116,7 @@ module.exports = Backbone.View.extend({
var catModel = this.categories.add(category); var catModel = this.categories.add(category);
var catId = catModel.get('id'); var catId = catModel.get('id');
var catView = this.renderedCategories[catId]; var catView = this.renderedCategories[catId];
model.set('category', catModel);
if (!catView) { if (!catView) {
catView = new CategoryView({ catView = new CategoryView({

6
test/specs/block_manager/index.js

@ -1,7 +1,7 @@
var BlockManager = require('block_manager'); var BlockManager = require('block_manager');
var BlocksView = require('./view/BlocksView'); var BlocksView = require('./view/BlocksView');
describe('BlockManager', () => { describe.only('BlockManager', () => {
describe('Main', () => { describe('Main', () => {
@ -31,6 +31,10 @@ describe('BlockManager', () => {
expect(obj.getAll().length).toEqual(0); expect(obj.getAll().length).toEqual(0);
}); });
it('No categories inside', () => {
expect(obj.getCategories().length).toEqual(0);
});
it('Add new block', () => { it('Add new block', () => {
var model = obj.add(idTest, optsTest); var model = obj.add(idTest, optsTest);
expect(obj.getAll().length).toEqual(1); expect(obj.getAll().length).toEqual(1);

Loading…
Cancel
Save