-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemsList.js
More file actions
264 lines (227 loc) · 7.98 KB
/
ItemsList.js
File metadata and controls
264 lines (227 loc) · 7.98 KB
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
/**
* Responsible for displaying selectable items.
* @module ItemsList
*/
/**
* Responsible for displaying selectable items.
* @param {string} containerDivId - The container that holds the items.
* @param {string} containerShadowId - The shadow background.
* @param {string} okbuttonId - The ok button.
*/
class ItemsList extends Subject {
constructor(containerDivId, containerShadowId, okbuttonId) {
super();
this._itemsArray = [];
this.selectedItemsArray = [];
if (containerDivId) {
this._container = $(`#${containerDivId}`);
}
if (containerShadowId) {
this._containerShadow = $(`#${containerShadowId}`);
}
if (okbuttonId) {
this._okbuttonclick = $(`#${okbuttonId}`);
this._okbuttonclick.on("click", function () {
this.hide();
ItemsList.notify('okbuttonclick', this);
}.bind(this));
}
ItemsList.on("selecteditemschanged", function () {
this.redraw();
}.bind(this));
ItemsList.on("itemsarraychanged", function () {
let trigger = false;
for (let i = this.selectedItemsArray.length - 1; i >= 0; i--) {
let item = this.selectedItemsArray[i];
if (this.itemsArray.findIndex((p) => p.id === item.id) === -1) {
this.selectedItemsArray.splice(i, 1);
trigger = true;
}
}
this.redraw();
if (trigger = true) {
ItemsList.notify("selecteditemschanged", this);
}
}.bind(this));
}
/**
* Adds an item to the itemsArray.
* @param {Item} item - It represents the item's caption and id.
* @param {string} Item.id - Identifies an item inside the ItemsArray.
* @param {string} Item.label - Used for displaying the item.
*/
addItem(item) {
//let item = { "id": id, "label": label };
let index = this.itemsArray.findIndex((p) => p.id === item.id);
if (index !== -1) {
console.error(`Duplicated id (${id}) found!`)
return;
}
this.itemsArray.push(item);
ItemsList.notify('itemsarraychanged', this);
}
/**
* Removes an item from the itemsArray.
* @param {string} item - The item's id.
*/
removeItem(id) {
let index = this.itemsArray.findIndex((p) => p.id === id);
if (index === -1) {
console.error(`Item Not Found: ${id}`);
return;
}
this.itemsArray.splice(index, 1);
ItemsList.notify('itemsarraychanged', this);
}
/**
* Shows the shadowContainer and its contents.
*/
show() {
if (this._containerShadow) {
this._containerShadow.show();
}
}
/**
* Hides the shadowContainer and its contents.
*/
hide() {
if (this._containerShadow) {
this._containerShadow.hide();
}
}
get itemsArray() {
return this._itemsArray;
}
/**
* Redraws all the items based on itemsArray and selectecItemsArray.
*/
redraw() {
if (this._container) {
this._container.empty();
for (let i = 0; i < this.itemsArray.length; i++) {
let item = this.itemsArray[i];
let newButton = this._createButton(item);
if (this.selectedItemsArray.findIndex((p) => p.id === item.id) !== -1) {
newButton.addClass("active");
}
this._container.append(newButton);
}
}
}
/**
* Adds an element with a label to the container.
* @private
* @param {string} item - The label.
*/
_createButton(item) {
let newButton = $(document.createElement("button"));
newButton.addClass("btn btn-outline-primary buttondiv");
newButton.html(item.label);
newButton.on("click", function () { this.toggleItemActive(item); }.bind(this));
return newButton;
}
/**
* Adds the "active" class to the item's id
* @param {string} itemId - The item's id.
*/
setItemActive(item) {
let index = this.itemsArray.findIndex((p) => p.id === item.id);
if (index === -1) {
console.log(`Item Not Found: ${item.id}`);
return;
}
let indexSelected = this.selectedItemsArray.findIndex((p) => p.id === item.id);
if (indexSelected !== -1) {
console.log(`Item Already Selected: ${item.id}`);
}
this.selectedItemsArray.push(item);
ItemsList.notify('selecteditemschanged', this);
}
/**
* Removes the "active" class from the item's id
* @param {string} itemId
*/
setItemInactive(id) {
let index = this.itemsArray.findIndex((p) => p.id === id);
if (index === -1) {
console.log(`Item Not Found: ${id}`);
return;
}
let indexSelected = this.selectedItemsArray.findIndex((p) => p.id === id);
if (indexSelected === -1) {
console.log(`itemId Not Found: ${itemId}`);
}
this.selectedItemsArray.splice(indexSelected, 1);
ItemsList.notify('selecteditemschanged', this);
}
/**
* Toggles (adds or removes) the "active" class from the item with the id "itemId"
* @param {string} itemId
*/
toggleItemActive(item) {
let index = this.itemsArray.findIndex((p) => p.id === item.id);
if (index === -1) {
console.log(`itemId Not Found: ${id}`);
return;
}
let indexSelected = this.selectedItemsArray.findIndex((p) => p.id === item.id);
if (indexSelected === -1) {
this.selectedItemsArray.push(item);
}
else {
this.selectedItemsArray.splice(indexSelected, 1);
}
ItemsList.notify('selecteditemschanged', this);
}
}
/**
* Triggered when an item is added or removed from itemsArray.
* @event module:ItemsList~ItemsList#itemsarraychanged
* @type {ItemsList}
* @property {ItemsList} itemsList - The items list instance.
* @property {string[]} itemsList.itemsArray - Holds the items.
* @property {string[]} itemsList.selectecItemsArray - Holds the selected items.
*/
/**
* Triggered when an item is added or removed from selectedItemsArray.
* @event module:ItemsList~ItemsList#selecteditemschanged
* @type {ItemsList}
* @property {ItemsList} itemsList - The items list instance.
* @property {string[]} itemsList.itemsArray - Holds the items.
* @property {string[]} itemsList.selectecItemsArray - Holds the selected items.
*/
/**
* Triggered when the "ok" button is clicked.
* @event module:ItemsList~ItemsList#okbuttonclick
* @type {ItemsList}
* @property {ItemsList} itemsList - The items list instance.
* @property {string[]} itemsList.itemsArray - Holds the items.
* @property {string[]} itemsList.selectecItemsArray - Holds the selected items.
*/
if (!ItemsList.init) {
ItemsList.init = true;
ItemsList.registerEventNames([
'itemsarraychanged',
'selecteditemschanged',
'okbuttonclick',
]);
}
console.log("Started!");
let itemsList = new ItemsList("containerAddress", "containerShadow", "okbutton");
itemsList.addItem({id: "1", label: "Rua X"});
itemsList.addItem({id: "2", label: "Avenida Y"});
itemsList.addItem({id: "3", label: "Travessa Z"});
// let containerAddress = $("#containerAddress");
// let newButton = $(document.createElement("button"));
// newButton.addClass("btn btn-outline-primary buttondiv");
// newButton.html("Rua das Covas");
// containerAddress.append(newButton);
// for(let i = 0; i < itemsList.itemsArray.length; i++)
// {
// let street = itemsList.itemsArray[i];
// let newButton = $(document.createElement("button"));
// newButton.addClass("btn btn-outline-primary buttondiv");
// newButton.html(street);
// containerAddress.append(newButton);
// }
//<button type="button" class="btn btn-outline-primary buttondiv">Travessa Z</button>