-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_jump.js
More file actions
299 lines (273 loc) · 11.8 KB
/
Copy pathquick_jump.js
File metadata and controls
299 lines (273 loc) · 11.8 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
// jQuery check, if it's not present then include it
// jQuery check, if it's not present then include it
function bpMinVersion(min, actual) {
function parseVersionString (str) {
if (typeof(str) != 'string') { return false; }
var x = str.split('.');
var maj = parseInt(x[0]) || 0;
var min = parseInt(x[1]) || 0;
var pat = parseInt(x[2]) || 0;
return {
major: maj,
minor: min,
patch: pat
}
}
var minParsed = parseVersionString(min);
var actualParsed = parseVersionString(actual);
if (actualParsed.major > minParsed.major) {
return true;
} else if (actualParsed.major == minParsed.major &&
actualParsed.minor > minParsed.minor) {
return true;
} else if (actualParsed.major == minParsed.major &&
actualParsed.minor == minParsed.minor &&
actualParsed.patch > minParsed.patch) {
return true;
}
return false;
}
if (typeof jQuery == 'undefined') {
var jq, jqMigrate, scriptLoc = document.getElementsByTagName('script')[0].parentElement;
jq = document.createElement('script');
jqMigrate = document.createElement('script');
jq.type = jqMigrate.type = "text/javascript";
jq.src = "//code.jquery.com/jquery-1.11.2.min.js";
jqMigrate.src = "//code.jquery.com/jquery-migrate-1.2.1.min.js";
jq.onload = function() {
jqMigrate.onload = bpQuickJumpOnLoad;
scriptLoc.appendChild(jqMigrate);
}
scriptLoc.appendChild(jq);
} else if (bpMinVersion("1.9", $.fn.jquery)) {
var jqMigrate = document.createElement('script');
jqMigrate.type = "text/javascript";
jqMigrate.src = "//code.jquery.com/jquery-migrate-1.2.1.min.js";
jqMigrate.onload = bpQuickJumpOnLoad;
document.getElementsByTagName('head')[0].appendChild(jqMigrate);
} else {
bpQuickJumpOnLoad();
}
// ***********************************
// Widget-specific code
// ***********************************
// Set the defaults if they haven't been set yet
if (typeof BP_SEARCH_SERVER === 'undefined') {
var BP_SEARCH_SERVER = "http://bioportal.bioontology.org";
}
if (typeof BP_SITE === 'undefined') {
var BP_SITE = "BioPortal";
}
if (typeof BP_ORG === 'undefined') {
var BP_ORG = "NCBO";
}
var BP_ORG_SITE = (BP_ORG == "") ? BP_SITE : BP_ORG + " " + BP_SITE;
if (BP_ontology_id == undefined || BP_ontology_id == "all") {
var BP_ontology_id = ""
}
if (BP_search_branch == undefined) {
var BP_search_branch = ""
}
if (typeof BP_include_definitions === 'undefined' || BP_include_definitions !== true) {
var BP_include_definitions = false;
}
function determineHTTPS(url) {
return url.replace("http:", ('https:' == document.location.protocol ? 'https:' : 'http:'));
}
BP_SEARCH_SERVER = determineHTTPS(BP_SEARCH_SERVER);
var jumpTo_searchBoxID = "BP_search_box",
jumpTo_searchBoxSelector = "#" + jumpTo_searchBoxID,
jumpTo_searchBox = null;
// Process after document is fully loaded
function bpQuickJumpOnLoad() {
jQuery(document).ready(function() {
// Install any CSS we need (check to make sure it hasn't been loaded)
if (jQuery('link[href$="' + BP_SEARCH_SERVER + '/javascripts/JqueryPlugins/autocomplete/jquery.autocomplete.css"]')) {
jQuery("head").append("<link>");
css = jQuery("head").children(":last");
css.attr({
rel: "stylesheet",
type: "text/css",
href: BP_SEARCH_SERVER + "/javascripts/JqueryPlugins/autocomplete/jquery.autocomplete.css"
});
}
jQuery("#bp_quick_jump").append("Jump To: <input type=\"textbox\" id=\"" + jumpTo_searchBoxID + "\" size=\"30\"> <input type=\"button\" value=\"Go to " + BP_SITE + "\" onclick=\"jumpTo_jump_clicked();\">");
jQuery("#bp_quick_jump").append("<input type='hidden' id='jump_to_concept_id'>");
jQuery("#bp_quick_jump").append("<input type='hidden' id='jump_to_ontology_id'>");
// Grab the specific scripts we need and fires it start event
jQuery.getScript(BP_SEARCH_SERVER + "/javascripts/JqueryPlugins/autocomplete/crossdomain_autocomplete.js", function() {
jumpTo_setup_functions();
});
});
}
function jumpTo_jumpToValue(li) {
if (jQuery("#jump_to_concept_id") == null && jQuery("#jump_to_ontology_id") == null) {
var search = confirm("Class could not be found or is not browsable in " + BP_SITE + ".\n\nPress OK to go to the " + BP_SITE + " Search page or Cancel to try again");
if (search) {
document.location = BP_SEARCH_SERVER + "/search/";
return;
}
}
if (jQuery("#jump_to_concept_id") != null && jQuery("#jump_to_ontology_id") != null) {
var sValue = jQuery("#jump_to_concept_id").val();
var ontology_id = jQuery("#jump_to_ontology_id").val();
document.location = BP_SEARCH_SERVER + "/ontologies/" + ontology_id + "/?p=classes&conceptid=" + encodeURIComponent(sValue);
return;
}
}
// Formats the Jump To search results
function jumpTo_formatItem(row) {
var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"), // .*+?|()[]{}\
keywords = jQuery(jumpTo_searchBoxSelector).val().trim().replace(specials, "\\$&").split(' ').join('|'),
regex = new RegExp('(' + keywords + ')', 'gi');
var resultTypeSpan = jQuery("<span>");
resultTypeSpan.attr("style","font-size:9px;color:blue;");
if (typeof row[2] !== "undefined" && row[2] !== "") {
resultTypeSpan.text(row[2]);
}
if (row[0].match(regex) == null) {
var contents = row[6].split("\t");
var synonym = contents[0] || "";
synonym = synonym.split(";");
if (synonym !== "") {
var matchSynonym = jQuery.grep(synonym, function(e) {
return e.match(regex) != null;
});
row[0] = row[0] + " (synonyms: " + matchSynonym.join(", ") + ")";
}
}
// Cleanup obsolete class tag before markup for search keywords.
if (row[0].indexOf("[obsolete]") != -1) {
row[0] = row[0].replace("[obsolete]", "");
obsolete_prefix = "<span class='obsolete_class' title='obsolete class'>";
obsolete_suffix = "</span>";
} else {
obsolete_prefix = "";
obsolete_suffix = "";
}
// Markup the search keywords.
var resultClass = row[0].replace(regex, "<b><span style='color:#006600;'>$1</span></b>");
// Set wider class name column
var resultClassWidth = "350px";
if (BP_include_definitions) {
resultClassWidth = "150px";
} else if (BP_ontology_id == "") {
resultClassWidth = "300px";
}
var resultClassDiv = jQuery("<div>");
resultClassDiv.addClass("result_class");
resultClassDiv.attr("style", "width: " + resultClassWidth);
resultClassDiv.html(resultClass); // resultClass contains markup, not just text.
var resultDiv = jQuery("<div>");
// row[7] is the ontology_id, only included when searching multiple ontologies
var result_ont_version = row[3],
result_uri = row[4];
if (BP_ontology_id !== "") {
if (BP_include_definitions) {
resultDiv.append(definitionDiv(result_ont_version, result_uri));
}
resultDiv.append(resultClassDiv);
resultDiv.append(resultTypeSpan.attr("style", "overflow: hidden; float: none;"));
} else {
resultDiv.append(resultClassDiv);
if (BP_include_definitions) {
resultDiv.append(definitionDiv(result_ont_version, result_uri));
}
resultDiv.append(resultTypeSpan);
var resultOnt = row[7];
var resultOntDiv = jQuery("<div>");
resultOntDiv.addClass("result_ontology");
resultOntDiv.attr("style", "overflow: hidden;");
resultOntDiv.text(truncateText(resultOnt, 30));
resultDiv.append(resultOntDiv);
}
return obsolete_prefix + resultDiv.html() + obsolete_suffix;
}
function definitionDiv(ont, concept) {
var definitionAjax = jQuery("<a>");
definitionAjax.addClass("get_definition_via_ajax");
definitionAjax.attr("href", BP_SEARCH_SERVER + "/ajax/json_class?callback=?&ontologyid=" + ont + "&conceptid=" + encodeURIComponent(concept));
var definitionDiv = jQuery("<div>");
definitionDiv.addClass('result_definition');
definitionDiv.text("retreiving definitions...");
definitionDiv.append(definitionAjax);
return definitionDiv;
}
function jumpTo_setup_functions() {
var extra_params = {
subtreerootconceptid: encodeURIComponent(BP_search_branch)
};
var result_width = 350;
// Add extra space for definition
if (BP_include_definitions) {
result_width += 300;
}
// Add space for ontology name
if (BP_ontology_id === "") {
result_width += 250;
} else {
result_width += 100;
}
jQuery(jumpTo_searchBoxSelector).bioportal_autocomplete(BP_SEARCH_SERVER + "/search/json_search/" + BP_ontology_id, {
extraParams: extra_params,
lineSeparator: "~!~",
matchSubset: 0,
minChars: 3,
maxItemsToShow: 20,
onFindValue: jumpTo_jumpToValue,
onItemSelect: jumpTo_jumpToSelect,
width: result_width,
footer: '<div style="color: grey; font-size: 8pt; font-family: Verdana; padding: .8em .5em .3em;">Results provided by <a style="color: grey;" href="' + BP_SEARCH_SERVER + '">' + BP_ORG_SITE + '</a></div>',
formatItem: jumpTo_formatItem
});
// Setup polling to get definitions
if (BP_include_definitions) {
getWidgetAjaxContent();
}
}
// Poll for potential definitions returned with results
function getWidgetAjaxContent() {
// Look for anchors with a get_via_ajax class and replace the parent with the resulting ajax call
$(".get_definition_via_ajax").each(function() {
var def_link = $(this);
if (typeof def_link.attr("getting_content") === 'undefined') {
def_link.attr("getting_content", true);
$.getJSON(def_link.attr("href"), function(data) {
var definition = (typeof data.definition === 'undefined') ? "" : data.definition.join(" ");
def_link.parent().html(truncateText(decodeURIComponent(definition.replace(/\+/g, " "))));
});
}
});
setTimeout(getWidgetAjaxContent, 100);
}
// Sets a hidden form value that records the concept id when a concept is chosen in the jump to
// This is a workaround because the default autocomplete search method cannot distinguish between two
// concepts that have the same preferred name but different ids.
function jumpTo_jumpToSelect(li) {
jQuery("#jump_to_concept_id").val(li.extra[0]);
jQuery("#jump_to_ontology_id").val(li.extra[2]);
}
function jumpTo_jump_clicked() {
jQuery("#BP_search_box")[0].autocompleter.findValue();
}
function truncateText(text, max_length) {
if (typeof max_length === 'undefined' || max_length == "") {
max_length = 70;
}
var more = '...';
var content_length = $.trim(text).length;
if (content_length <= max_length)
return text; // bail early if not overlong
var actual_max_length = max_length - more.length;
var truncated_node = jQuery("<div>");
var full_node = jQuery("<div>").html(text).hide();
text = text.replace(/^ /, ''); // node had trailing whitespace.
var text_short = text.slice(0, max_length);
// Ensure HTML entities are encoded
// http://debuggable.com/posts/encode-html-entities-with-jquery:480f4dd6-13cc-4ce9-8071-4710cbdd56cb
text_short = $('<div/>').text(text_short).html();
var other_text = text.slice(max_length, text.length);
text_short += "<span class='expand_icon'><b>" + more + "</b></span>";
text_short += "<span class='long_text'>" + other_text + "</span>";
return text_short;
}