-
Notifications
You must be signed in to change notification settings - Fork 0
/
asq-match.html
138 lines (115 loc) · 2.96 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
<link rel="import" href="../asq-base/asq-base.html">
<script>
/**
* @param {!Function} matchCallback
* @constructor
*/
ASQ.Match = function(matchCallback) {
this.x = {};
this.y = {};
this.xMax = 1;
this.yMax = 1;
this.matchCallback = matchCallback;
};
ASQ.Match.prototype = {
/**
* Clears all the selection except the ones indicated.
*
* @method clear
* @param {Array} excludes items to be excluded.
*/
clear: function(excludes) {
Object.keys(this.x).forEach(function(x, index){
var i = this.x[x].length;
while ( i-- ) {
var y = this.x[x][i];
if ( !excludes || !excludes.hasOwnProperty(x)
|| excludes[x].indexOf(y) < 0 ) {
this.setMatched(x, y, false);
}
}
}, this);
},
/**
* Match or dismatch two values, according to the `toBeMatched`.
*
*/
setMatched: function(x, y, toBeMatched) {
if ( x != null && y != null ) {
if ( toBeMatched ) {
this._connect(x, y);
} else {
this._disconnect(x, y);
}
if ( this.matchCallback ) {
this.matchCallback(x, y, toBeMatched);
}
}
},
_connect: function(x, y) {
if ( typeof this.x[x] === 'undefined' ) {
this.x[x] = [];
}
if ( this.x[x].length >= this.yMax ) {
var kicked = this.x[x].splice(0, 1);
var j = this.y[kicked].indexOf(x);
if ( j >= 0 ) {
this.y[kicked].splice(j, 1);
this.matchCallback(x, kicked, false);
}
}
if ( typeof this.y[y] === 'undefined' ) {
this.y[y] = [];
}
if ( this.y[y].length >= this.xMax ) {
var kicked = this.y[y].splice(0, 1);
var i = this.x[kicked].indexOf(y);
if ( i >= 0 ) {
this.x[kicked].splice(i, 1);
this.matchCallback(kicked, y, false);
}
}
this.x[x].push(y);
this.y[y].push(x);
},
_disconnect: function(x, y) {
var i = this.x[x].indexOf(y);
if ( i >= 0 ) this.x[x].splice(i, 1);
var j = this.y[y].indexOf(x);
if ( j >= 0 ) this.y[y].splice(j, 1);
},
/**
* Check if the two values are checked.
*
* @return {Boolean} [description]
*/
isMatched: function(x, y) {
if ( this.x.hasOwnProperty(x) ) {
return this.x[x].indexOf(y) >= 0;
}
return false;
},
/**
* Match or dismatch two values.
*
*/
match: function(x, y) {
this.toggle(x, y);
},
toggle: function(x, y) {
this.setMatched(x, y, !this.isMatched(x, y));
},
isXMatchedByAnyY: function(x) {
if ( this.x.hasOwnProperty(x) && this.x[x].length > 0 ) {
return true
}
return false
},
isYMatchedByAnyX: function(y) {
if ( this.y.hasOwnProperty(y) && this.y[y].length > 0 ) {
return true
}
return false
}
};
</script>