-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New delegator: set/erase/toggleProperty
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
--- | ||
description: Provides methods to set or toggle properties on target elements. | ||
provides: [Delegator.setProperty, eraseProperty, toggleProperty] | ||
requires: [Behavior/Delegator, Core/Element] | ||
script: Delegator.SetProperty.js | ||
name: Delegator.SetProperty | ||
... | ||
*/ | ||
(function(){ | ||
var triggers = {}; | ||
|
||
['set', 'erase', 'toggle'].each(function(action){ | ||
|
||
triggers[action + 'Property'] = { | ||
require: ['property'], | ||
handler: function(event, link, api){ | ||
var target = link; | ||
if (api.get('target') && api.get('target') != 'self') { | ||
target = link.getElement(api.get('target')); | ||
if (!target) api.fail('could not locate target element to ' + action + ' its class', link); | ||
} | ||
if (action == 'set' || (action == 'toggle' && target.get(api.get('property')) != api.get('value'))){ | ||
if (api.get('value') === null) api.fail('Could not retrieve eraseproperty-value option from element.'); | ||
target.set(api.get('property'), api.get('value')); | ||
} else { | ||
target.erase(api.get('property')); | ||
} | ||
} | ||
}; | ||
|
||
}); | ||
|
||
Delegator.register('click', triggers); | ||
|
||
})(); |
64 changes: 64 additions & 0 deletions
64
Tests/Specs/more-behaviors/Delegators/Delegator.SetProperty.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
--- | ||
name: Delegator.SetProperty Tests | ||
description: n/a | ||
requires: [More-Behaviors/Delegator.setProperty] | ||
provides: [Delegator.SetProperty.Tests] | ||
... | ||
*/ | ||
|
||
|
||
(function(){ | ||
var dom = new Element('div', {'class':'foo'}).set('html', | ||
'<p>\ | ||
<a id="toggleParent" data-trigger="toggleProperty" data-toggleproperty-options="\'target\':\'!div.foo\', \'property\':\'title\', \'value\':\'set\'">Toggle the title value</a>\ | ||
<a id="toggleSelf" data-trigger="toggleProperty" data-toggleproperty-options="\'property\':\'title\', \'value\':\'set\'">Toggle the title value</a>\ | ||
<a id="setParent" data-trigger="setProperty" data-setproperty-options="\'target\':\'!div.foo\', \'property\':\'title\', \'value\':\'set\'">Add the title value</a>\ | ||
<a id="setSelf" data-trigger="setProperty" data-setproperty-options="\'property\':\'title\', \'value\':\'set\'">Add the title value</a>\ | ||
<a id="eraseParent" data-trigger="eraseProperty" data-eraseproperty-options="\'target\':\'!div.foo\', \'property\':\'title\'">Remove the title value</a>\ | ||
<a id="eraseSelf" data-trigger="eraseProperty" data-eraseproperty-options="\'property\':\'title\'">Remove the title value</a>\ | ||
</p>' | ||
); | ||
|
||
var del = new Delegator().attach(dom), | ||
setParent = dom.getElement('a#setParent'), | ||
setSelf = dom.getElement('a#setSelf'), | ||
eraseParent = dom.getElement('a#eraseParent'), | ||
eraseSelf = dom.getElement('a#eraseSelf'), | ||
toggleParent = dom.getElement('a#toggleParent'), | ||
toggleSelf = dom.getElement('a#toggleSelf'); | ||
|
||
describe('Delegator.SetProperty', function(){ | ||
it('should toggle the title on a target', function(){ | ||
expect(dom.get('title')).toBe(null); | ||
del.trigger('toggleProperty', toggleParent, 'click'); | ||
expect(dom.get('title')).toBe('set'); | ||
del.trigger('toggleProperty', toggleParent, 'click'); | ||
expect(dom.get('title')).toBe(null); | ||
}); | ||
it('should toggle the title of itself', function(){ | ||
expect(toggleSelf.get('title')).toBe(null); | ||
del.trigger('toggleProperty', toggleSelf, 'click'); | ||
expect(toggleSelf.get('title')).toBe('set'); | ||
del.trigger('toggleProperty', toggleSelf, 'click'); | ||
expect(toggleSelf.get('title')).toBe(null); | ||
}); | ||
it('should add and erase the title to a target', function(){ | ||
expect(dom.get('title')).toBe(null); | ||
del.trigger('setProperty', setParent, 'click'); | ||
expect(dom.get('title')).toBe('set'); | ||
del.trigger('eraseProperty', eraseParent, 'click'); | ||
expect(dom.get('title')).toBe(null); | ||
}); | ||
it('should add and erase it\'s own title property', function(){ | ||
expect(setSelf.get('title')).toBe(null); | ||
del.trigger('setProperty', setSelf, 'click'); | ||
expect(setSelf.get('title')).toBe('set'); | ||
setSelf.erase('title'); | ||
eraseSelf.set('title', 'set'); | ||
expect(eraseSelf.get('title')).toBe('set'); | ||
del.trigger('eraseProperty', eraseSelf, 'click'); | ||
expect(eraseSelf.get('title')).toBe(null); | ||
}); | ||
}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters