-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathractive-decorators-select2.js
118 lines (87 loc) · 3.22 KB
/
ractive-decorators-select2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
ractive-decorators-select2
=============================================
Integrate Ractive with Select2
==========================
Troubleshooting: If you're using a module system in your app (AMD or
something more nodey) then you may need to change the paths below,
where it says `require( 'ractive' )` or `define([ 'ractive' ]...)`.
==========================
Usage: Include this file on your page below Ractive, e.g:
<script src='lib/ractive.js'></script>
<script src='lib/ractive-decorators-select2.js'></script>
Or, if you're using a module loader, require this module:
// requiring the plugin will 'activate' it - no need to use
// the return value
require( 'ractive-decorators-select2' );
*/
(function ( global, factory ) {
'use strict';
// Common JS (i.e. browserify) environment
if ( typeof module !== 'undefined' && module.exports && typeof require === 'function' ) {
factory( require( 'ractive' ), require( 'jquery' ) );
}
// AMD?
else if ( typeof define === 'function' && define.amd ) {
define([ 'ractive', 'jquery' ], factory );
}
// browser global
else if ( global.Ractive && global.jQuery) {
factory( global.Ractive, global.jQuery );
}
else {
throw new Error( 'Could not find Ractive or jQuery! They must be loaded before the ractive-decorators-select2 plugin' );
}
}( typeof window !== 'undefined' ? window : this, function ( Ractive, $ ) {
'use strict';
var select2Decorator;
select2Decorator = function (node, type) {
var ractive = node._ractive.root || node._ractive.ractive;
var setting = false;
var observer;
var options = {};
if (type) {
if (!select2Decorator.type.hasOwnProperty(type)) {
throw new Error( 'Ractive Select2 type "' + type + '" is not defined!' );
}
options = select2Decorator.type[type];
if (typeof options === 'function') {
options = options.call(this, node);
}
}
// Push changes from ractive to select2
if (node._ractive.binding) {
var binding = node._ractive.binding;
var keypath = binding.keypath ? binding.keypath.str : binding.model.key;
observer = ractive.observe(keypath, function (newvalue) {
if (!setting) {
setting = true;
window.setTimeout(function () {
if (newvalue === "")
$(node).select2("val", "");
$(node).change();
setting = false;
}, 0);
}
});
}
// Pull changes from select2 to ractive
$(node).select2(options).on('change', function () {
if (!setting) {
setting = true;
ractive.updateModel();
setting = false;
}
});
return {
teardown: function () {
$(node).select2('destroy');
if (observer) {
observer.cancel();
}
}
};
};
select2Decorator.type = {};
Ractive.decorators.select2 = select2Decorator;
}));