forked from icanjs/semantic-ui-dropdown-canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropdown.js
70 lines (59 loc) · 1.79 KB
/
dropdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import can from 'can';
import $ from 'jquery';
import 'semantic-ui-transition/transition';
import 'semantic-ui-transition/transition.css';
import 'semantic-ui-dropdown/dropdown.css';
import 'semantic-ui-dropdown/dropdown';
// import 'semantic-ui-transition/transition';
/**
* A can.view.attr wrapper around SemanticUI Dropdown. Settings can be defined as "semantic"-prefixed attributes.
*
* Note: Semantic-UI CSS should be imported separately, but this module includes the dropdown css.
*
* Example:
* With default settings:
* <input type="text" semantic-dropdown />
*/
can.view.attr('semantic-dropdown', function(el, attrData) {
let attrs = getConfigObjFromAttrs(el),
options = getDropdownSettingsFromConfig(attrs),
$el = $(el);
function updateAttributes(ev){
if (ev.attributeName === 'show') {
let attrs = getConfigObjFromAttrs(ev.target);
if (options.show) {
$el.dropdown('show');
} else {
$el.dropdown('hide');
}
}
}
$el.bind('attributes', updateAttributes);
$el.bind('removed', function(){
$el.unbind('attributes', updateAttributes);
$el.dropdown('destroy');
});
$el.dropdown(options);
});
function getConfigObjFromAttrs(el){
let attributes = [].slice.call(el.attributes || []);
let attrObj = {};
attributes.map(attr => {
attrObj[attr.name] = attr.value;
if (attrObj[attr.name] === '') {
attrObj[attr.name] = true;
}
});
return attrObj;
}
function getDropdownSettingsFromConfig(config) {
const dropdownSettings = $.fn.dropdown.settings || {};
let settings = {};
for(let setting in dropdownSettings) {
let hyphenatedSetting = can.hyphenate(setting);
if(config.hasOwnProperty(hyphenatedSetting)) {
settings[setting] = config[hyphenatedSetting];
}
}
return settings;
}