/
organisation.js
417 lines (350 loc) · 13.1 KB
/
organisation.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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* Knockout view model for organisation pages.
* @param props JSON/javascript representation of the organisation.
* @constructor
*/
OrganisationViewModel = function (props) {
var self = $.extend(this, new Documents());
var orgTypesMap = {
aquarium:'Aquarium',
archive:'Archive',
botanicGarden:'Botanic Garden',
conservation:'Conservation',
fieldStation:'Field Station',
government:'Government',
governmentDepartment:'Government Department',
herbarium:'Herbarium',
historicalSociety:'Historical Society',
horticulturalInstitution:'Horticultural Institution',
independentExpert:'Independent Expert',
industry:'Industry',
laboratory:'Laboratory',
library:'Library',
management:'Management',
museum:'Museum',
natureEducationCenter:'Nature Education Center',
nonUniversityCollege:'Non-University College',
park:'Park',
repository:'Repository',
researchInstitute:'Research Institute',
school:'School',
scienceCenter:'Science Center',
society:'Society',
university:'University',
voluntaryObserver:'Voluntary Observer',
zoo:'Zoo'
};
self.organisationId = props.organisationId;
self.orgType = ko.observable(props.orgType);
self.orgTypeDisplayOnly = ko.computed(function() {
return orgTypesMap[self.orgType()] || "Unspecified";
});
self.name = ko.observable(props.name);
self.acronym = ko.observable(props.acronym);
self.description = ko.observable(props.description).extend({markdown:true});
self.url = ko.observable(props.url);
self.newsAndEvents = ko.observable(props.newsAndEvents).extend({markdown:true});;
self.collectoryInstitutionId = props.collectoryInstitutionId;
self.breadcrumbName = ko.computed(function() {
return self.name()?self.name():'New Organisation';
});
self.projects = props.projects;
self.deleteOrganisation = function() {
if (window.confirm("Delete this organisation? Are you sure?")) {
$.post(fcConfig.organisationDeleteUrl).complete(function() {
window.location = fcConfig.organisationListUrl;
}
);
};
};
self.editDescription = function() {
editWithMarkdown('Edit organisation description', self.description);
};
self.editOrganisation = function() {
window.location = fcConfig.organisationEditUrl;
};
self.transients = self.transients || {};
self.transients.orgTypes = [];
for (var ot in orgTypesMap) {
if (orgTypesMap.hasOwnProperty(ot))
self.transients.orgTypes.push({orgType:ot, name:orgTypesMap[ot]});
}
self.toJS = function(includeDocuments) {
var ignore = self.ignore.concat(['breadcrumbName', 'orgTypeDisplayOnly', 'collectoryInstitutionId']);
var js = ko.mapping.toJS(self, {include:['documents'], ignore:ignore} );
if (includeDocuments) {
js.documents = ko.toJS(self.documents);
js.links = ko.mapping.toJS(self.links());
}
return js;
};
self.modelAsJSON = function(includeDocuments) {
var orgJs = self.toJS(includeDocuments);
return JSON.stringify(orgJs);
};
self.save = function() {
if ($('.validationEngineContainer').validationEngine('validate')) {
var orgData = self.modelAsJSON(true);
$.ajax(fcConfig.organisationSaveUrl, {type:'POST', data:orgData, contentType:'application/json'}).done( function(data) {
if (data.errors) {
}
else {
var orgId = self.organisationId?self.organisationId:data.organisationId;
window.location = fcConfig.organisationViewUrl+'/'+orgId;
}
}).fail( function() {
});
}
};
if (props.documents !== undefined && props.documents.length > 0) {
$.each(['logo', 'banner', 'mainImage'], function(i, role){
var document = self.findDocumentByRole(props.documents, role);
if (document) {
self.documents.push(document);
}
});
}
// links
if (props.links) {
$.each(props.links, function(i, link) {
self.addLink(link.role, link.url);
});
}
return self;
};
/**
* Provides the ability to search a user's organisations and other organisations at the same time. The results
* are maintained as separate lists for ease of display (so a users existing organisations can be prioritised).
* @param organisations the organisations not belonging to the user.
* @param userOrganisations the organisations that belong to the user.
* @param (optional) if present, this value should contain the organisationId of an organisation to pre-select.
*/
OrganisationSelectionViewModel = function(organisations, userOrganisations, inititialSelection) {
var self = this;
var userOrgList = new SearchableList(userOrganisations, ['name']);
var otherOrgList = new SearchableList(organisations, ['name']);
self.term = ko.observable('');
self.term.subscribe(function() {
userOrgList.term(self.term());
otherOrgList.term(self.term());
});
self.selection = ko.computed(function() {
return userOrgList.selection() || otherOrgList.selection();
});
self.userOrganisationResults = userOrgList.results;
self.otherResults = otherOrgList.results;
self.clearSelection = function() {
userOrgList.clearSelection();
otherOrgList.clearSelection();
self.term('');
};
self.isSelected = function(value) {
return userOrgList.isSelected(value) || otherOrgList.isSelected(value);
};
self.select = function(value) {
self.term(value['name']);
userOrgList.select(value);
otherOrgList.select(value);
};
self.allViewed = ko.observable(false);
self.scrolled = function(blah, event) {
var elem = event.target;
var scrollPos = elem.scrollTop;
var maxScroll = elem.scrollHeight - elem.clientHeight;
if ((maxScroll - scrollPos) < 9) {
self.allViewed(true);
}
};
self.visibleRows = ko.computed(function() {
var count = 0;
if (self.userOrganisationResults().length) {
count += self.userOrganisationResults().length+1; // +1 for the "user orgs" label.
}
if (self.otherResults().length) {
count += self.otherResults().length;
if (self.userOrganisationResults().length) {
count ++; // +1 for the "other orgs" label (it will only show if the my organisations label is also showing.
}
}
return count;
});
self.visibleRows.subscribe(function() {
if (self.visibleRows() <= 4 && !self.selection()) {
self.allViewed(true);
}
});
self.visibleRows.notifySubscribers();
self.organisationNotPresent = ko.observable();
var findByOrganisationId = function(list, organisationId) {
for (var i=0; i<list.length; i++) {
if (list[i].organisationId === organisationId) {
return list[i];
}
}
return null;
};
if (inititialSelection) {
var userOrg = findByOrganisationId(userOrganisations, inititialSelection);
var orgToSelect = userOrg ? userOrg : findByOrganisationId(organisations, inititialSelection);
if (orgToSelect) {
self.select(orgToSelect);
}
}
};
var ServerSideOrganisationsViewModel = function() {
var self = this;
self.pagination = new PaginationViewModel({}, self);
self.organisations = ko.observableArray([]);
self.searchTerm = ko.observable('').extend({throttle:500});
self.searchTerm.subscribe(function(term) {
self.refreshPage(0);
});
self.refreshPage = function(offset) {
var url = fcConfig.organisationSearchUrl;
var params = {offset:offset, max:self.pagination.resultsPerPage()};
if (self.searchTerm()) {
params.searchTerm = self.searchTerm();
}
else {
params.sort = "nameSort"; // Sort by name unless there is a search term, in which case we sort by relevence.
}
$.get(url, params, function(data) {
if (data.hits) {
var orgs = data.hits.hits || [];
self.organisations($.map(orgs, function(hit) {
if (hit._source.logoUrl) {
hit._source.documents = [{
role:'logo',
url: hit._source.logoUrl
}]
}
return new OrganisationViewModel(hit._source);
}));
}
if (offset == 0) {
self.pagination.loadPagination(0, data.hits.total);
}
});
};
self.refreshPage(0);
};
var OrganisationsViewModel = function(organisations, userOrgIds) {
var self = this;
var userOrgList = [], otherOrgList = [];
for (var i=0; i<organisations.length; i++) {
// Attach images to each organisations for display
var orgView = new OrganisationViewModel(organisations[i]);
orgView.searchableName = organisations[i].name;
orgView.searchableDescription = organisations[i].description;
if (userOrgIds && userOrgIds.indexOf(organisations[i].organisationId) >= 0) {
userOrgList.push(orgView);
}
else {
otherOrgList.push(orgView)
}
}
var searchableUserList, searchableOtherList;
self.searchTerm = ko.observable('');
self.searchName = ko.observable(true);
self.searchDescription = ko.observable(false);
self.caseSensitive = ko.observable(false);
var buildSearch = function() {
var keys = [];
if (self.searchName()) {
keys.push('searchableName');
}
if (self.searchDescription()) {
keys.push('searchableDescription');
}
var options = {keys:keys, caseSensitive:self.caseSensitive()};
searchableUserList = new SearchableList(userOrgList, keys, options);
searchableOtherList = new SearchableList(otherOrgList, keys, options);
};
buildSearch();
self.delayedSearchTerm = ko.pureComputed(self.searchTerm).extend({rateLimit:{method:'notifyWhenChangesStop', timeout:400}});
self.delayedSearchTerm.subscribe(function(term) {
searchableUserList.term(term);
searchableOtherList.term(term);
self.pageNum(1);
self.pageList(buildPageList());
});
this.userOrganisations = searchableUserList.results;
this.otherOrganisations = searchableOtherList.results;
this.pageNum = ko.observable(1);
this.organisationsPerPage = 20;
var maxPageButtons = 10;
this.totalPages = ko.computed(function() {
var count = self.userOrganisations().length + self.otherOrganisations().length;
var pageCount = Math.floor(count / self.organisationsPerPage);
return count % self.organisationsPerPage > 0 ? pageCount + 1 : pageCount;
});
this.currentPage = ko.computed(function() {
var results = [].concat(self.userOrganisations(), self.otherOrganisations());
var first = (self.pageNum()-1) * self.organisationsPerPage;
return results.slice(first, first+self.organisationsPerPage);
});
function buildPageList() {
var pages = [];
var i;
var currentPage = self.pageNum();
var total = self.totalPages();
if (total <= maxPageButtons) {
for (i=1; i<=total; i++) {
pages.push(i);
}
return pages;
}
if (currentPage <= (maxPageButtons / 2) + 1) {
for (i=1; i<maxPageButtons; i++) {
pages.push(i);
}
pages.push('..');
pages.push(total);
return pages;
}
if (currentPage > (total - (maxPageButtons / 2))) {
pages.push(1);
pages.push('..');
for (i=total - maxPageButtons+2; i<=total; i++) {
pages.push(i);
}
return pages;
}
pages.push(1);
pages.push('..');
var start = currentPage-(maxPageButtons/2)+1;
for (i=start; i<start+maxPageButtons-2; i++) {
pages.push(i);
}
pages.push('..');
pages.push(total);
return pages;
};
this.pageList = ko.observableArray(buildPageList());
this.hasPrev = ko.computed(function() {
return self.pageNum() > 1;
});
this.hasNext = ko.computed(function() {
return self.pageNum() < self.totalPages();
});
this.next = function() {
if (self.hasNext()) {
self.gotoPage(self.pageNum()+1);
}
};
this.prev = function() {
if (self.hasPrev()) {
self.gotoPage(self.pageNum()-1);
}
};
this.gotoPage = function(page) {
if (page != '..') {
self.pageNum(page);
self.pageList(buildPageList());
self.pageList.notifySubscribers();
}
};
this.addOrganisation = function() {
window.location = fcConfig.createOrganisationUrl;
};
};