forked from BorisMoore/jquery-datalink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.datalink.js
257 lines (245 loc) · 6.27 KB
/
jquery.datalink.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*!
* jQuery Data Link plugin v1.0.0pre
* http://github.com/jquery/jquery-datalink
*
* Copyright Software Freedom Conservancy, Inc.
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
(function( $, undefined ){
var oldcleandata = $.cleanData,
links = [],
fnSetters = {
val: "val",
html: "html",
text: "text"
},
eventNameSetField = "setField",
eventNameChangeField = "changeField";
function getLinks(obj) {
var data = $.data( obj ),
cache,
fn = data._getLinks || (cache={s:[], t:[]}, data._getLinks = function() { return cache; });
return fn();
}
function bind(obj, wrapped, handler) {
wrapped.bind( obj.nodeType ? "change" : eventNameChangeField, handler );
}
function unbind(obj, wrapped, handler) {
wrapped.unbind( obj.nodeType ? "change" : eventNameChangeField, handler );
}
$.extend({
cleanData: function( elems ) {
for ( var j, i = 0, elem; (elem = elems[i]) != null; i++ ) {
// remove any links with this element as the source
// or the target.
var links = $.data( elem, "_getLinks" );
if ( links ) {
links = links();
// links this element is the source of
var self = $(elem);
$.each(links.s, function() {
unbind( elem, self, this.handler );
if ( this.handlerRev ) {
unbind( this.target, $(this.target), this.handlerRev );
}
});
// links this element is the target of
$.each(links.t, function() {
unbind( this.source, $(this.source), this.handler );
if ( this.handlerRev ) {
unbind( elem, self, this.handlerRev );
}
});
links.s = [];
links.t = [];
}
}
oldcleandata( elems );
},
convertFn: {
"!": function(value) {
return !value;
}
},
setField: function(target, field, value) {
if ( target.nodeType ) {
var setter = fnSetters[ field ] || "attr";
$(target)[setter](value);
} else {
var parts = field.split(".");
parts[1] = parts[1] ? "." + parts[1] : "";
var $this = $( target ),
args = [ parts[0], value ];
$this.triggerHandler( eventNameSetField + parts[1] + "!", args );
if ( value !== undefined ) {
target[ field ] = value;
}
$this.triggerHandler( eventNameChangeField + parts[1] + "!", args );
}
}
});
function getMapping(ev, changed, newvalue, map) {
var target = ev.target,
isSetData = ev.type === eventNameChangeField,
mappedName,
convert,
name;
if ( isSetData ) {
name = changed;
if ( ev.namespace ) {
name += "." + ev.namespace;
}
} else {
name = (target.name || target.id);
}
if ( !map ) {
mappedName = name;
} else {
var m = map[ name ];
if ( !m ) {
return null;
}
mappedName = m.name;
convert = m.convert;
if ( typeof convert === "string" ) {
convert = $.convertFn[ convert ];
}
}
return {
name: mappedName,
convert: convert,
value: isSetData ? newvalue : $(target).val()
};
}
$.extend($.fn, {
link: function(target, mapping) {
var self = this;
if ( !target ) {
return self;
}
function matchByName(name) {
var selector = "[name=" + name + "], [id=" + name +"]";
// include elements in this set that match as well a child matches
return self.filter(selector).add(self.find(selector));
}
if ( typeof target === "string" ) {
target = $( target, this.context || null )[ 0 ];
}
var hasTwoWay = !mapping,
map,
mapRev,
handler = function(ev, changed, newvalue) {
// a dom element change event occurred, update the target
var m = getMapping( ev, changed, newvalue, map );
if ( m ) {
var name = m.name,
value = m.value,
convert = m.convert;
if ( convert ) {
value = convert( value, ev.target, target );
}
if ( value !== undefined ) {
$.setField( target, name, value );
}
}
},
handlerRev = function(ev, changed, newvalue) {
// a change or changeData event occurred on the target,
// update the corresponding source elements
var m = getMapping( ev, changed, newvalue, mapRev );
if ( m ) {
var name = m.name,
value = m.value,
convert = m.convert;
// find elements within the original selector
// that have the same name or id as the field that updated
matchByName(name).each(function() {
newvalue = value;
if ( convert ) {
newvalue = convert( newvalue, target, this );
}
if ( newvalue !== undefined ) {
$.setField( this, "val", newvalue );
}
});
}
};
if ( mapping ) {
$.each(mapping, function(n, v) {
var name = v,
convert,
convertBack,
twoWay;
if ( $.isPlainObject( v ) ) {
name = v.name || n;
convert = v.convert;
convertBack = v.convertBack;
twoWay = v.twoWay !== false;
hasTwoWay |= twoWay;
} else {
hasTwoWay = twoWay = true;
}
if ( twoWay ) {
mapRev = mapRev || {};
mapRev[ n ] = {
name: name,
convert: convertBack
};
}
map = map || {};
map[ name ] = { name: n, convert: convert, twoWay: twoWay };
});
}
// associate the link with each source and target so it can be
// removed automaticaly when _either_ side is removed.
self.each(function() {
bind( this, $(this), handler );
var link = {
handler: handler,
handlerRev: hasTwoWay ? handlerRev : null,
target: target,
source: this
};
getLinks( this ).s.push( link );
if ( target.nodeType ) {
getLinks( target ).t.push( link );
}
});
if ( hasTwoWay ) {
bind( target, $(target), handlerRev );
}
return self;
},
unlink: function(target) {
this.each(function() {
var self = $(this),
links = getLinks( this ).s;
for (var i = links.length-1; i >= 0; i--) {
var link = links[ i ];
if ( link.target === target ) {
// unbind the handlers
//wrapped.unbind( obj.nodeType ? "change" : "changeData", handler );
unbind( this, self, link.handler );
if ( link.handlerRev ) {
unbind( link.target, $(link.target), link.handlerRev );
}
// remove from source links
links.splice( i, 1 );
// remove from target links
var targetLinks = getLinks( link.target ).t,
index = $.inArray( link, targetLinks );
if ( index !== -1 ) {
targetLinks.splice( index, 1 );
}
}
}
});
},
setField: function(field, value) {
return this.each(function() {
$.setField( this, field, value );
});
}
});
})(jQuery);