Skip to content

Commit

Permalink
Allow to define actions as simple options objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Apr 11, 2015
1 parent 1eed471 commit 4beeae4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ L.Toolbar = L.Class.extend({
appendToContainer: function(container) {
var baseClass = this.constructor.baseClass + '-' + this._calculateDepth(),
className = baseClass + ' ' + this.options.className,
Action, action,
Action, action, actions = [],
i, j, l, m;

this._container = container;
Expand All @@ -71,13 +71,17 @@ L.Toolbar = L.Class.extend({

action = new Action();
action._createIcon(this, this._ul, this._arguments);
actions.push(action);
}
return actions;
},

_getActionConstructor: function(Action) {
var args = this._arguments,
toolbar = this;

if (typeof Action === 'object') { Action = this.options.toolbarActionClass.extend({options: Action}); }

return Action.extend({
initialize: function() {
Action.prototype.initialize.apply(this, args);
Expand Down
46 changes: 23 additions & 23 deletions src/ToolbarAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,45 @@ L.ToolbarAction = L.Handler.extend({
},

options: {
toolbarIcon: {
html: '',
className: '',
tooltip: ''
},
html: '',
className: '',
tooltip: '',
subToolbar: new L.Toolbar()
},

initialize: function(options) {
var defaultIconOptions = L.ToolbarAction.prototype.options.toolbarIcon;

L.setOptions(this, options);
this.options.toolbarIcon = L.extend({}, defaultIconOptions, this.options.toolbarIcon);
},

enable: function() {
if (this._enabled) { return; }
this._enabled = true;

if (this.addHooks) { this.addHooks(); }
// Retrocompat.
L.setOptions(this, this.options.toolbarIcon);
// End retrocompat.
},

disable: function() {
if (!this._enabled) { return; }
this._enabled = false;
addHooks: function () {
if (this.options.onEnable) {
this.options.onEnable.call(this);
}
},

if (this.removeHooks) { this.removeHooks(); }
removeHooks: function () {
if (this.options.onDisable) {
this.options.onDisable.call(this);
}
},

_createIcon: function(toolbar, container, args) {
var iconOptions = this.options.toolbarIcon;

this.toolbar = toolbar;
this._icon = L.DomUtil.create('li', '', container);
this._link = L.DomUtil.create('a', '', this._icon);

this._link.innerHTML = iconOptions.html;
this._link.innerHTML = this.options.html;
this._link.setAttribute('href', '#');
this._link.setAttribute('title', iconOptions.tooltip);
this._link.setAttribute('title', this.options.tooltip);

L.DomUtil.addClass(this._link, this.constructor.baseClass);
if (iconOptions.className) {
L.DomUtil.addClass(this._link, iconOptions.className);
if (this.options.className) {
L.DomUtil.addClass(this._link, this.options.className);
}

L.DomEvent.on(this._link, 'click', this.enable, this);
Expand Down Expand Up @@ -91,3 +87,7 @@ L.toolbarAction = function toolbarAction(options) {
L.ToolbarAction.extendOptions = function(options) {
return this.extend({ options: options });
};

L.Toolbar.mergeOptions({
toolbarActionClass: L.ToolbarAction
});
19 changes: 12 additions & 7 deletions test/src/ToolbarActionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ describe("L.ToolbarAction", function() {

Action = L.ToolbarAction.extend({
options: {
toolbarIcon: {
html: 'Test Icon',
className: 'my-toolbar-icon'
}
html: 'Test Icon',
className: 'my-toolbar-icon'
}
});
toolbar = new L.Toolbar({ actions: [Action] });
Expand All @@ -42,6 +40,13 @@ describe("L.ToolbarAction", function() {
expect(L.DomUtil.hasClass(iconButton, 'leaflet-toolbar-icon')).to.equal(true);
expect(L.DomUtil.hasClass(iconButton, 'my-toolbar-icon')).to.equal(true);
});

it("Support toolbarIcon options for retrocompat", function() {
var toolbar = new L.ToolbarAction({toolbarIcon: {html: 'click me'}});

expect(toolbar.options.html).to.equal('click me');
});

});

describe("#_addSubToolbar", function() {
Expand Down Expand Up @@ -162,17 +167,17 @@ describe("L.ToolbarAction", function() {
expect(h.options.color).to.equal('#d1bd0f');

/* Options of the parent constructor should be retained. */
expect(h.options.toolbarIcon.html).to.equal('');
expect(h.options.html).to.equal('');
});
});
});

describe("L.toolbarAction", function() {
describe("class factory", function() {
it("Creates an L.ToolbarAction instance.", function() {
var options = { toolbarIcon: { html: 'hello' } };
var options = { html: 'hello' };

expect(L.toolbarAction(options)).to.deep.equal(new L.ToolbarAction(options));
});
});
});
});
16 changes: 16 additions & 0 deletions test/src/ToolbarSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ describe("L.Toolbar", function() {
toolbar.addTo(map, 2);
toolbar.appendToContainer(container);
});

it("Should allow defining actions from an options object.", function() {
var spy = sinon.spy();
toolbar = new L.Toolbar({ actions: [{
html: '⚑',
tooltip: 'Back to center',
className: 'custom-class',
onEnable: spy
}] });

toolbar.addTo(map);
var actions = toolbar.appendToContainer(container);
expect(container.querySelectorAll('.custom-class').length).to.equal(1);
actions[0].enable();
expect(spy.calledOnce).to.equal(true);
});
});

describe("#appendToContainer", function() {
Expand Down

0 comments on commit 4beeae4

Please sign in to comment.