2011-10-27 17:26:29 +00:00
|
|
|
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A collection of building blocks for the proxy-management UI.
|
|
|
|
*/
|
|
|
|
|
2011-11-03 23:20:22 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* itemElement
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* Display information about an entity according to the template. The entity
|
|
|
|
* can be either:
|
|
|
|
* a profile -- Individual to be edited.
|
|
|
|
* a proxy -- User Account to do the editing, optionally with info from a
|
|
|
|
* profile associated with that individual.
|
|
|
|
*
|
|
|
|
* You provide:
|
|
|
|
* template -- the HTML text that determines how the element should look.
|
|
|
|
* The template must be a single HTML element, which may contain
|
|
|
|
* any number of sub-elements. It needs to have a single outer
|
|
|
|
* wrapper, however.
|
|
|
|
* uri, label, classLabel, imageUrl -- as described below
|
|
|
|
* remove -- a function that we can call when the user clicks on the remove
|
|
|
|
* link or button. We will pass a reference to this struct.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* The template must inlude a link or button with attribute templatePart="remove"
|
|
|
|
*
|
|
|
|
* The template may include tokens to be replaced, from the following:
|
|
|
|
* %uri% -- the URI of the individual being displayed
|
|
|
|
* %label& -- the label of the individual.
|
|
|
|
* %classLabel% -- the label of the most specific class of the individual.
|
|
|
|
* %imageUrl% -- the URL that will fetch the image of the individual,
|
|
|
|
* or a placeholder image.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* This relies on magic names for the styles:
|
|
|
|
* existingProxyItem -- for an item that was present when the page was loaded
|
|
|
|
* newProxyItem -- for an item that was added since the page was loaded
|
|
|
|
* removedProxyItem -- added to an item when the "remove" link is cheked.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
function itemElement(template, uri, label, classLabel, imageUrl, removeInfo) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.uri = uri;
|
|
|
|
this.label = label;
|
|
|
|
this.classLabel = classLabel;
|
2012-03-21 20:33:34 +00:00
|
|
|
this.imageUrl = imageUrl;
|
2011-11-03 23:20:22 +00:00
|
|
|
this.removeInfo = removeInfo;
|
|
|
|
|
|
|
|
this.toString = function() {
|
2011-11-04 19:41:35 +00:00
|
|
|
return "itemElement: " + content;
|
2011-11-03 23:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.element = function() {
|
|
|
|
var content = template.replace(/%uri%/g, this.uri)
|
|
|
|
.replace(/%label%/g, this.label)
|
|
|
|
.replace(/%classLabel%/g, this.classLabel)
|
|
|
|
.replace(/%imageUrl%/g, this.imageUrl);
|
|
|
|
|
|
|
|
var element = $(content);
|
|
|
|
element.addClass("proxyInfoElement");
|
|
|
|
|
|
|
|
var removeLink = $("[templatePart='remove']", element).first();
|
|
|
|
removeLink.click(function(event) {
|
|
|
|
self.removeInfo(self);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 21:44:34 +00:00
|
|
|
|
2011-10-27 20:50:47 +00:00
|
|
|
/*
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* proxyAutoComplete
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* Attach the autocomplete funcionality that we like in proxy panels.
|
|
|
|
*
|
|
|
|
* You provide:
|
2011-11-10 21:44:34 +00:00
|
|
|
* parms -- a map containing the URL and the action code needed for the AJAX call.
|
2011-10-30 17:15:53 +00:00
|
|
|
* excludedUris -- these URIs are always filtered out of the results.
|
2011-11-04 19:41:35 +00:00
|
|
|
* getProxyInfos -- a function that will return an array of itemElements
|
2011-10-27 20:50:47 +00:00
|
|
|
* that are already present in the list and so should be filtered out of
|
|
|
|
* the autocomplete response.
|
|
|
|
* addProxyInfo -- a function that we can call when an item is selected.
|
2011-11-04 19:41:35 +00:00
|
|
|
* It will take the selection info, build an itemElement, and add
|
2011-10-27 21:55:41 +00:00
|
|
|
* it to the panel.
|
2011-10-28 18:13:21 +00:00
|
|
|
* reportSearchStatus -- a function that we can call when a search is done. It
|
|
|
|
* will accept the length of the search term and the number of results,
|
|
|
|
* and will display it in some way.
|
2011-10-27 20:50:47 +00:00
|
|
|
* ----------------------------------------------------------------------------
|
2011-11-10 21:44:34 +00:00
|
|
|
* The AJAX request will include a "term" parameter, set to the current search term.
|
2011-10-27 20:50:47 +00:00
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* The functionality includes:
|
|
|
|
* -- fetching data for the autocomplete list.
|
|
|
|
* -- cacheing the fetched data
|
|
|
|
* -- filtering as described above.
|
|
|
|
* -- calling addProxyInfo() and clearing the field when a value is selected.
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
2011-10-30 17:15:53 +00:00
|
|
|
function proxyAutocomplete(parms, excludedUris, getProxyInfos, addProxyInfo, reportSearchStatus) {
|
2011-10-27 20:50:47 +00:00
|
|
|
var cache = [];
|
|
|
|
|
|
|
|
var filterResults = function(parsed) {
|
|
|
|
var filtered = [];
|
|
|
|
var existingUris = $.map(getProxyInfos(), function(p) {
|
|
|
|
return p.uri;
|
|
|
|
});
|
|
|
|
$.each(parsed, function(i, p) {
|
2011-10-30 17:15:53 +00:00
|
|
|
if ((-1 == $.inArray(p.uri, existingUris))
|
|
|
|
&& (-1 == $.inArray(p.uri, excludedUris))) {
|
2011-10-27 20:50:47 +00:00
|
|
|
filtered.push(p);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return filtered;
|
|
|
|
}
|
2011-10-28 18:13:21 +00:00
|
|
|
|
|
|
|
var sendResponse = function(request, response, results) {
|
|
|
|
reportSearchStatus(request.term.length, results.length);
|
|
|
|
response(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.minLength = 0,
|
2011-10-27 20:50:47 +00:00
|
|
|
|
|
|
|
this.source = function(request, response) {
|
2011-10-28 18:13:21 +00:00
|
|
|
if (request.term.length < 3) {
|
|
|
|
sendResponse(request, response, []);
|
|
|
|
return;
|
|
|
|
}
|
2011-10-27 20:50:47 +00:00
|
|
|
if (request.term in cache) {
|
2011-10-28 18:13:21 +00:00
|
|
|
sendResponse(request, response, filterResults(cache[request.term]));
|
2011-10-27 20:50:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
url: parms.url,
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
2011-11-10 21:44:34 +00:00
|
|
|
action: parms.action,
|
|
|
|
term: request.term
|
2011-10-27 20:50:47 +00:00
|
|
|
},
|
|
|
|
complete: function(xhr, status) {
|
|
|
|
var results = $.parseJSON(xhr.responseText);
|
2011-11-10 21:44:34 +00:00
|
|
|
cache[request.term] = results;
|
|
|
|
sendResponse(request, response, filterResults(results));
|
2011-10-27 20:50:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.select = function(event, ui) {
|
2011-10-27 21:55:41 +00:00
|
|
|
addProxyInfo(ui.item);
|
2011-10-27 20:50:47 +00:00
|
|
|
event.preventDefault();
|
|
|
|
event.target.value = '';
|
|
|
|
}
|
|
|
|
}
|