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)
* * [get](#get)
* * [getAll](#getall)
* * [getCategories](#getcategories)
* * [render](#render)
*
* 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'),
BlocksView = require('./view/BlocksView');
var blocks, view;
var categories = [];
return {
@ -55,9 +57,10 @@ module.exports = () => {
c[name] = defaults[name];
}
blocks = new Blocks(c.blocks);
categories = new BlockCategories(),
view = new BlocksView({
collection: blocks,
categories: new BlockCategories(),
categories,
}, c);
return this;
},
@ -68,12 +71,18 @@ module.exports = () => {
* @param {Object} opts Options
* @param {string} opts.label Name of the block
* @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
* @return {Block} Added block
* @example
* blockManager.add('h1-block', {
* label: 'Heading',
* content: '<h1>Put your title here</h1>',
* category: 'Basic',
* attributes: {
* title: 'Insert h1 block'
* }
@ -86,7 +95,7 @@ module.exports = () => {
},
/**
* Return block by id
* Return the block by id
* @param {string} id Block id
* @example
* var block = blockManager.get('h1-block');
@ -109,6 +118,14 @@ module.exports = () => {
return blocks;
},
/**
* Get all available categories
* @return {Array|Collection}
*/
getCategories() {
return categories;
},
/**
* Render blocks
* @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 catId = catModel.get('id');
var catView = this.renderedCategories[catId];
model.set('category', catModel);
if (!catView) {
catView = new CategoryView({

6
test/specs/block_manager/index.js

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

Loading…
Cancel
Save