-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasq-match.html
355 lines (315 loc) · 9.52 KB
/
asq-match.html
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../asq-base/asq-base.html">
<!--
`asq-match` is a hidden element without UI, which could store matching
relationship between two sets. The relationships supported are
`One to One`, `One to Many`, `Many to One` and `Many to Many`.
##### Example
It should be put it inside an element.
<asq-match mode="1-m"></asq-match>
@element asq-match
@group ASQ Elements
@blurb Element tore matching relationships.
@homepage http://github.com/ASQ-USI/asq-match
-->
<polymer-element name="asq-match" attributes="mode" hidden>
<script>
(function() {
var proto = {
/**
* All possible relationship this.modes.
{
SINGLE: "single",
O2M: "one_to_many",
M2O: "many_to_one",
M2M: 'many_to_many'
}
*
* @attribute modeState
* @type object
*/
modes: {
SINGLE: "single",
O2M: "one_to_many",
M2O: "many_to_one",
M2M: 'many_to_many'
},
/**
* The relationsip mode.
* @attribute mode
* @type '1-1' | '1-m' | 'm-1' | 'm-m'
* @default '1-1'
*/
mode: '',
/**
* Represents the mode by this.modes enum attribute.
this.modes =
{
SINGLE: "single",
O2M: "one_to_many",
M2O: "many_to_one",
M2M: 'many_to_many'
}
*
* @attribute modeState
* @type string
* @default this.modes.SINGLE
*/
modeState: '',
/**
* The array that stores the matches. A match is
* defined as follows:
{
key: <any>,
value: <array>
}
* @attribute matches
* @type Array
* @default []
*/
matches: [],
created: function() {
this.mode = '1-1';
this.modeState = this.modes.SINGLE;
},
ready: function() {
this.clear();
},
/**
* Delete all matches.
* @method clear
*/
clear: function() {
this.matches = [];
},
/**
* Retrieves all the matches. Or retrieves the matches of given `key`.
* @method getMatches
* @param {any} from .
* @returns {Object} Returns the matches. Return null if no matches.
*/
getMatches: function(from) {
if ( from == null ) {
return this.matches;
} else {
var index = this.lookup(from);
if ( index >= 0) {
return this.matches[index].value
} else {
return null;
}
}
},
/**
* Delete all the matches of a given `key` and also delete itself from
* `this.matches`.
* @method deleteItem
* @param {any} from .
*/
deleteItem: function(from) {
var i = this.lookup(from);
if ( i >= 0) {
this.matches.splice(i, 1);
}
},
/**
* Indicates if there is a matching between `from` and `to`.
* @method isMatch
* @param {any} from .
* @param {any} to .
* @returns Returns true if there is matching between `from` and `to`.
*/
isMatch: function(from, to) {
var i = this.lookup(from);
if ( i >= 0) {
return this.matches[i].value.indexOf(to) >= 0 ? true : false;
} else {
return false;
}
},
/**
* Fired after the a new match is set up or a match is broken.
*
* @event asq-match
* @param detail
{
isMatch: <if the match is set up, ture; otherwise, false>
from: <from>
to: <to>
}
*/
/*
* The function that actually does the `match` and `dismatch` things.
* If the param `isMatch` is ture, then a match between `from` and `to`
* will be set up, otherwise `from` and `to` will be dismatched. Finally,
* an event `asq-match` will be fired.
*/
setMatched: function(from, to, isMatch) {
if ( from !== undefined && from !== null && to !== undefined && to !== null) {
if ( isMatch ) {
var index = this.lookup(from);
var m = index >= 0 ? this.matches[index] : {key: from, value: []};
m.value.push(to);
// delete old item first then add a new one
this.deleteItem(from);
this.matches.push(m);
} else {
var i = this.lookup(from);
if (i >= 0) {
if ( Array.isArray(to) ) {
console.log('isArray');
} else {
var j = this.matches[i].value.indexOf(to);
this.matches[i].value.splice(j, 1);
}
}
}
this.fire("asq-match", {isMatch: isMatch, key: from, value: to});
}
},
/**
* The main interface. Toggle a match between `from` and `to`.
* <br>
* <b>NOTE</b> Accoding to the `mode`, some matches may be deleted
* after toggle new match.
* ## Example
* In `1-1` mode, suppose the `matches` is empty at the begining.
this.$.match(k1, v1); // match `k1 <-> v1` would be set up.
this.$.match(k2, v1); // match `k1 <-> v1` would be broken up and then
// match `k2 <-> v1` would be set up.
* @method match
* @param {any} from The key.
* @param {any} to The value.
*/
match: function(from, to) {
if ( from == null || from == undefined || to == null || to == undefined ) {
return;
}
if ( this.modeState == this.modes.O2M ) {
if ( !this.isMatch(from, to) && this.countValue(to) > 0) {
var xs = this.getKeysByValue(to);
xs.forEach(function(e){ this.setMatched(e, to, false);}, this);
}
} else if (this.modeState == this.modes.M2O){
if (this.existMatchesFromKey(from) && !this.isMatch(from, to)) {
this.setMatched(from, this.getMatches(from)[0], false);
}
} else if ( this.modeState == this.modes.SINGLE ) {
if ( ! this.isMatch(from, to) ) {
if (this.existMatchesFromKey(from) ) {
this.setMatched(from, this.getMatches(from)[0], false);
}
var fs = this.getKeysByValue(to);
if (fs.length > 0) {
this.toggle(fs[0], to);
}
}
}
this.toggle(from, to);
},
/**
* Return all th `froms` or `keys` that are matched with given `to` or `value`.
* @method getKeysByValue
* @param {any} to The `to` or `value`.
* @return {array} All th `froms` or `keys` that are matched with `to`.
*/
getKeysByValue: function(to) {
var keys = [];
this.matches.forEach(function(e) {
e.value.forEach(function(t) {
if ( to == t ) {
keys.push(e.key);
}
})
})
return keys;
},
/**
* Count how many `to`s are there in matches.
* @method countValue
* @param {any} to
* @return {int}
*/
countValue: function(to) {
var count = 0;
this.matches.forEach(function(e) {
e.value.forEach(function(f) {
if ( f == to ) {
count++;
}
})
});
return count;
},
/*
* Simply toggle the match between `from` and `to` without any other checkings.
*/
toggle: function(from, to) {
this.setMatched(from, to, !this.isMatch(from ,to));
},
/**
* Find the index of the given `from` or `key`.
* @method lookup
* @param {any} from
* @return {int} The index. In case of not found, return -1.
*/
lookup: function(from) {
var index = -1;
this.matches.forEach(function(e, i) {
if ( e.key == from ) {
index = i;
}
});
return index;
},
/**
* Check if there are mathces for `from`.
* @method existMatchesFrom
* @param {any} from
* @return {boolean}
*/
existMatchesFromKey: function(from) {
if ( this.lookup(from) < 0 ) return false;
return this.getMatches(from).length > 0 ? true : false;
},
/**
* A wrapper function of `countValue`. Check if the given `to` or `value` is existed.
* @method existsValue
* @param {any} to
* @return {boolean}
*/
existsValue: function(to) {
return this.countValue(to) > 0 ? true : false;
},
modeChanged: function() {
switch(this.mode) {
case "1-1":
case "single":
this.modeState = this.modes.SINGLE;
break;
case "1-n":
case "1-m":
this.modeState = this.modes.O2M;
break;
case "n-1":
case "m-1":
this.modeState = this.modes.M2O;
break;
case "m-n":
case "n-m":
case "n-n":
case "m-m":
case "multi":
this.modeState = this.modes.M2M;
break;
default:
this.modeState = this.modes.SINGLE;
this.mode = '1-1';
}
},
}
ASQ.asqify(proto);
Polymer('asq-match', proto);
})();
</script>
</polymer-element>