-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathItemTable.js
More file actions
399 lines (350 loc) · 13.3 KB
/
ItemTable.js
File metadata and controls
399 lines (350 loc) · 13.3 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
Copyright ©2014 Esri. All rights reserved.
TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the
Copyright Laws of the United States and applicable international
laws, treaties, and conventions.
For additional information, contact:
Attn: Contracts and Legal Department
Environmental Systems Research Institute, Inc.
380 New York Street
Redlands, California, 92373
USA
email: contracts@esri.com
*/
/*
* derived from jimu.js\dijit\_ItemTable.js
*/
/*global console, define, dojo */
define([
'dojo/_base/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dijit/_WidgetsInTemplateMixin',
'dojo/text!./ItemTable.html',
'dojo/Evented',
'dojo/_base/lang',
'dojo/_base/array',
'dojo/_base/html',
'dojo/_base/Deferred',
'dojo/query',
'dojo/NodeList-traverse',
'dojo/on',
'jimu/utils',
'jimu/portalUtils',
'jimu/portalUrlUtils',
'jimu/dijit/LoadingIndicator'
], function (declare,
_WidgetBase,
_TemplatedMixin,
_WidgetsInTemplateMixin,
template,
Evented,
lang,
array,
html,
Deferred,
query,
NodeList,
on,
jimuUtils,
portalUtils,
portalUrlUtils,
LoadingIndicator) {
/*jshint unused: false*/
return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin, Evented], {
templateString: template,
baseClass: "jimu-item-table",
query: null,
filteredQuery: null,
portalUrl: null,
hidden: false,
nls: null,
types: '', //required,array, filter search results,such as ["Feature Service","Map Service"]
typeKeywords: '', //optional, array, filter search results,such as ["Web AppBuilder","Web Map"]
//public methods:
//getSelectedItem
//show
//hide
//searchAllItems
//searchFilteredItems
//clear
//clearAllItemsSection
//clearFilteredItemsSection
//showAllItemsSection
//showFilterItemsSection
//css classes:
//item
//item-border
//item-thumbnail
//item-info
//item-name
//item-type-owner
//item-date
//item-details
//search-none-icon
//search-none-tip
_defaultThumbnail: {
"Web Mapping Application": "webapp.png",
"Mobile Application": "mobileapp.png"
},
postMixInProperties: function () {
this.nls = window.jimuNls.itemSelector;
},
postCreate: function () {
this.inherited(arguments);
if (!(this.types && this.types.length > 0)) {
this.types = [];
}
this.hidden = this.hidden === true;
if (this.hidden) {
this.hide();
}
if (this.portalUrl) {
this.portalUrl = portalUrlUtils.getStandardPortalUrl(this.portalUrl);
}
this.showAllItemsSection();
this.searchAllItems();
},
show: function () {
html.setStyle(this.domNode, 'display', 'block');
},
hide: function () {
html.setStyle(this.domNode, 'display', 'none');
},
searchAllItems: function (newQuery) {
this.showAllItemsSection();
if (newQuery) {
this.query = lang.mixin({}, newQuery);
this.query.start = 1;
this.clearAllItemsSection();
}
if (!this.portalUrl || !this.query) {
return;
}
if (this.query.start > 0) {
this.allItemsShelter.show();
var portal = portalUtils.getPortal(this.portalUrl);
var def = portal.queryItems(this.query);
def.then(lang.hitch(this, function (response) {
if (!this.domNode) {
return;
}
this.allItemsShelter.hide();
this.query.start = response.nextStart;
this._createItems(response, this.allItemTbody);
}), lang.hitch(this, function (err) {
console.error(err);
if (!this.domNode) {
return;
}
this.allItemsShelter.hide();
}));
}
},
searchFilteredItems: function ( /*optional*/ newFilteredQuery) {
//if newFilteredQuery is not null or undefined, it means the dijit will search a new query
//otherwise it means this method is called when scroll to bottom of this.filteredItemsTableDiv
this.showFilterItemsSection();
if (newFilteredQuery) {
this.filteredQuery = lang.clone(newFilteredQuery);
this.filteredQuery.start = 1;
this.clearFilteredItemsSection();
}
if (!this.portalUrl || !this.filteredQuery) {
return;
}
if (this.filteredQuery.start > 0) {
this.filteredItemShelter.show();
var portal = portalUtils.getPortal(this.portalUrl);
var def = portal.queryItems(this.filteredQuery);
def.then(lang.hitch(this, function (response) {
if (!this.domNode) {
return;
}
this.showFilterItemsSection();
if (newFilteredQuery) {
this.clearFilteredItemsSection();
}
this.filteredQuery.start = response.nextStart;
this._createItems(response, this.filteredItemsTbody);
this._filterItemCallback();
}), lang.hitch(this, function (err) {
console.error(err);
if (!this.domNode) {
return;
}
this._filterItemCallback();
}));
}
},
_filterItemCallback: function () {
this.filteredItemShelter.hide();
var count = this._getItemCount(this.filteredItemsTbody);
if (count > 0) {
html.setStyle(this.filteredItemsTableDiv, 'display', 'block');
html.setStyle(this.searchNoneTipSection, 'display', 'none');
} else {
html.setStyle(this.filteredItemsTableDiv, 'display', 'none');
html.setStyle(this.searchNoneTipSection, 'display', 'block');
}
},
clear: function () {
this.clearAllItemsSection();
this.clearFilteredItemsSection();
},
clearAllItemsSection: function () {
html.empty(this.allItemTbody);
},
clearFilteredItemsSection: function () {
html.empty(this.filteredItemsTbody);
},
showAllItemsSection: function () {
html.setStyle(this.allItemsSection, 'display', 'block');
html.setStyle(this.filteredItemsSection, 'display', 'none');
},
showFilterItemsSection: function () {
html.setStyle(this.allItemsSection, 'display', 'none');
html.setStyle(this.filteredItemsSection, 'display', 'block');
html.setStyle(this.filteredItemsTableDiv, 'display', 'block');
html.setStyle(this.searchNoneTipSection, 'display', 'none');
},
_onAllItemsSectionScroll: function () {
if (this._isScrollToBottom(this.allItemsTableDiv)) {
this.searchAllItems();
}
},
_onFilteredItemsSectionScroll: function () {
if (this._isScrollToBottom(this.filteredItemsTableDiv)) {
this.searchFilteredItems();
}
},
_isScrollToBottom: function (div) {
return jimuUtils.isScrollToBottom(div);
},
_createItems: function (response, tbody) {
var results = response.results;
var typesLowerCase = array.map(this.types, lang.hitch(this, function (type) {
return type.toLowerCase();
}));
var items = array.filter(results, lang.hitch(this, function (item) {
var type = (item.type && item.type.toLowerCase()) || '';
return array.indexOf(typesLowerCase, type) >= 0;
}));
var countPerRow = 1;
if (items.length === 0) {
return;
}
var itemsHash = {},
itemDiv;
// TODO since not using >1 column, get rid of empty td check
var emptyTds = query('td.empty', tbody);
var i;
if (emptyTds.length > 0) {
var usedEmptyTdCount = Math.min(items.length, emptyTds.length);
var ws = items.splice(0, usedEmptyTdCount);
for (i = 0; i < usedEmptyTdCount; i++) {
var emptyTd = emptyTds[i];
itemDiv = this._createItem(ws[i]);
itemsHash[itemDiv.item.id] = itemDiv;
html.place(itemDiv, emptyTd);
html.removeClass(emptyTd, 'empty');
}
}
if (items.length === 0) {
return;
}
for (i = 0; i < items.length; i++) {
var trDom = html.toDom("<tr><td></td></tr>");
html.place(trDom, tbody);
var td = query('td', trDom)[0];
var item = items[i];
if (item) {
itemDiv = this._createItem(item);
itemsHash[itemDiv.item.id] = itemDiv;
html.place(itemDiv, td);
}
}
},
_getItemCount: function (tbody) {
return query('.item', tbody).length;
},
_createItem: function (item) {
var str = '<div class="item">' +
'<div class="item-thumbnail jimu-auto-vertical">' +
'<div class="none-thumbnail-tip jimu-auto-vertical-content"></div>' +
'</div>' +
'<div class="item-info">' +
'<div class="item-name"></div>' +
'<div class="item-type-owner"></div>' +
'<div class="item-date"></div>' +
'<a class="item-details" target="_blank"></a>' +
'</div>' +
'</div>';
var itemDiv = html.toDom(str);
itemDiv.item = item;
var itemThumbnail = query('.item-thumbnail', itemDiv)[0];
var itemName = query('.item-name', itemDiv)[0];
var itemTypeOwner = query('.item-type-owner', itemDiv)[0];
var itemDate = query('.item-date', itemDiv)[0];
var itemDetails = query('.item-details', itemDiv)[0];
var noneThumbnailTip = query('.none-thumbnail-tip', itemDiv)[0];
if (!item.thumbnailUrl) {
var defaultThumbnail = this._defaultThumbnail[item.type];
if (defaultThumbnail) {
item.thumbnailUrl = require.toUrl('jimu') + '/images/' + defaultThumbnail;
}
}
if (item.thumbnailUrl) {
html.setStyle(itemThumbnail, 'backgroundImage', "url(" + item.thumbnailUrl + ")");
} else {
noneThumbnailTip.innerHTML = this.nls.noneThumbnail;
}
itemName.innerHTML = item.title;
itemName.title = itemName.innerHTML;
itemTypeOwner.innerHTML = ' by ' + item.owner;
itemTypeOwner.title = itemTypeOwner.innerHTML;
var d = new Date();
d.setTime(item.modified);
itemDate.innerHTML = d.toLocaleString();
itemDate.title = itemDate.innerHTML;
itemDetails.innerHTML = this.nls.moreDetails;
itemDetails.href = item.detailsPageUrl || "#";
return itemDiv;
},
/**
* when an item is clicked on the widget, fire the item selected event
* unless it is the item details link
* @param {Object} event [[Description]]
*/
_onItemsTableClicked: function (event) {
var target = event.target || event.srcElement;
if (html.hasClass(target, 'item-details')) {
// do not select if user clicks hyperlink
console.log("ItemTable :: _onItemsTableClicked :: item details clicked");
return;
}
// find the parent item node
var itemDiv = query(target).parents('.item')[0];
if (!itemDiv) {
return;
}
// remove from previously selected item
query('.item.jimu-state-active', this.domNode).removeClass('jimu-state-active');
// add to newly selected item
html.addClass(itemDiv, 'jimu-state-active');
// fire item selected event
this.emit('item-selected', itemDiv.item);
console.log("ItemTable :: _onItemsTableClicked :: item selected", itemDiv.item);
},
getSelectedItem: function () {
var item = null;
var itemDivs = query('.item.jimu-state-active', this.domNode);
if (itemDivs.length > 0) {
var itemDiv = itemDivs[0];
item = lang.mixin({}, itemDiv.item);
}
return item;
}
});
});