diff --git a/src/block_manager/index.js b/src/block_manager/index.js index 4e07ccd01..bcb21fa29 100644 --- a/src/block_manager/index.js +++ b/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: '

Put your title here

', + * 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} diff --git a/src/block_manager/view/BlocksView.js b/src/block_manager/view/BlocksView.js index 8dd9fa2dd..dfd2e1ba8 100644 --- a/src/block_manager/view/BlocksView.js +++ b/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({ diff --git a/test/specs/block_manager/index.js b/test/specs/block_manager/index.js index 5c2d2f794..340613e1c 100644 --- a/test/specs/block_manager/index.js +++ b/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);