Browse Source

Add the possibility to specify position on sector add via API. Closes #2094

refactor-traits
Artur Arseniev 7 years ago
parent
commit
0d1e9324d7
  1. 8
      src/style_manager/index.js
  2. 23
      src/style_manager/view/SectorsView.js
  3. 14
      src/utils/dom.js

8
src/style_manager/index.js

@ -105,19 +105,21 @@ export default () => {
* @param {string} [sector.name=''] Sector's label * @param {string} [sector.name=''] Sector's label
* @param {Boolean} [sector.open=true] Indicates if the sector should be opened * @param {Boolean} [sector.open=true] Indicates if the sector should be opened
* @param {Array<Object>} [sector.properties=[]] Array of properties * @param {Array<Object>} [sector.properties=[]] Array of properties
* @param {Object} [options={}] Options
* @return {Sector} Added Sector * @return {Sector} Added Sector
* @example * @example
* var sector = styleManager.addSector('mySector',{ * var sector = styleManager.addSector('mySector',{
* name: 'My sector', * name: 'My sector',
* open: true, * open: true,
* properties: [{ name: 'My property'}] * properties: [{ name: 'My property'}]
* }); * }, { at: 0 });
* // With `at: 0` we place the new sector at the beginning of the collection
* */ * */
addSector(id, sector) { addSector(id, sector, opts = {}) {
var result = this.getSector(id); var result = this.getSector(id);
if (!result) { if (!result) {
sector.id = id; sector.id = id;
result = sectors.add(sector); result = sectors.add(sector, opts);
} }
return result; return result;
}, },

23
src/style_manager/view/SectorsView.js

@ -1,6 +1,7 @@
import Backbone from 'backbone'; import Backbone from 'backbone';
import { extend, isString } from 'underscore'; import { extend, isString } from 'underscore';
import { isTaggableNode } from 'utils/mixins'; import { isTaggableNode } from 'utils/mixins';
import { appendAtIndex } from 'utils/dom';
import SectorView from './SectorView'; import SectorView from './SectorView';
export default Backbone.View.extend({ export default Backbone.View.extend({
@ -34,8 +35,8 @@ export default Backbone.View.extend({
* @return {Object} * @return {Object}
* @private * @private
* */ * */
addTo(model) { addTo(model, coll, opts = {}) {
this.addToCollection(model); this.addToCollection(model, null, opts);
}, },
/** /**
@ -133,10 +134,10 @@ export default Backbone.View.extend({
* @return {Object} Object created * @return {Object} Object created
* @private * @private
* */ * */
addToCollection(model, fragmentEl) { addToCollection(model, fragmentEl, opts = {}) {
const { pfx, target, propTarget, config } = this; const { pfx, target, propTarget, config, el } = this;
var fragment = fragmentEl || null; const appendTo = fragmentEl || el;
var view = new SectorView({ const rendered = new SectorView({
model, model,
id: `${pfx}${model.get('id')}`, id: `${pfx}${model.get('id')}`,
name: model.get('name'), name: model.get('name'),
@ -144,14 +145,8 @@ export default Backbone.View.extend({
target, target,
propTarget, propTarget,
config config
}); }).render().el;
var rendered = view.render().el; appendAtIndex(appendTo, rendered, opts.at);
if (fragment) {
fragment.appendChild(rendered);
} else {
this.$el.append(rendered);
}
return rendered; return rendered;
}, },

14
src/utils/dom.js

@ -1,5 +1,5 @@
// DOM helpers // DOM helpers
import { each } from 'underscore'; import { each, isUndefined } from 'underscore';
const KEY_TAG = 'tag'; const KEY_TAG = 'tag';
const KEY_ATTR = 'attributes'; const KEY_ATTR = 'attributes';
@ -9,6 +9,18 @@ export const empty = node => {
while (node.firstChild) node.removeChild(node.firstChild); while (node.firstChild) node.removeChild(node.firstChild);
}; };
export const appendAtIndex = (parent, child, index) => {
const { childNodes } = parent;
const total = childNodes.length;
const at = isUndefined(index) ? total : index;
if (at >= total) {
parent.appendChild(child);
} else {
parent.insertBefore(child, childNodes[at]);
}
};
/** /**
* Append an array of vNodes to an element * Append an array of vNodes to an element
* @param {HTMLElement} node HTML element * @param {HTMLElement} node HTML element

Loading…
Cancel
Save