/*! * Fancytree Taxonomy Browser * * Copyright (c) 2015, Martin Wendt (https://wwWendt.de) * * Released under the MIT license * https://github.com/mar10/fancytree/wiki/LicenseInfo * * @version @VERSION * @date @DATE */ /* global Handlebars */ /* eslint-disable no-console */ (function($, window, document) { "use strict"; /******************************************************************************* * Private functions and variables */ var taxonTree, searchResultTree, tmplDetails, tmplInfoPane, tmplMedia, timerMap = {}, USER_AGENT = "Fancytree Taxonomy Browser/1.0", GBIF_URL = "//api.gbif.org/v1/", TAXONOMY_KEY = "d7dddbf4-2cf0-4f39-9b2a-bb099caae36c", // GBIF backbone taxonomy SEARCH_PAGE_SIZE = 5, CHILD_NODE_PAGE_SIZE = 200, glyphOpts = { preset: "bootstrap3", map: { expanderClosed: "glyphicon glyphicon-menu-right", // glyphicon-plus-sign expanderLazy: "glyphicon glyphicon-menu-right", // glyphicon-plus-sign expanderOpen: "glyphicon glyphicon-menu-down", // glyphicon-collapse-down }, }; // Load and compile handlebar templates $.get("details.tmpl.html", function(data) { tmplDetails = Handlebars.compile(data); Handlebars.registerPartial("tmplDetails", tmplDetails); }); $.get("media.tmpl.html", function(data) { tmplMedia = Handlebars.compile(data); Handlebars.registerPartial("tmplMedia", tmplMedia); }); $.get("info-pane.tmpl.html", function(data) { tmplInfoPane = Handlebars.compile(data); }); /** Update UI elements according to current status */ function updateControls() { var query = $.trim($("input[name=query]").val()); $("#btnPin").attr("disabled", !taxonTree.getActiveNode()); $("#btnUnpin") .attr("disabled", !taxonTree.isFilterActive()) .toggleClass("btn-success", taxonTree.isFilterActive()); $("#btnResetSearch").attr("disabled", query.length === 0); $("#btnSearch").attr("disabled", query.length < 2); } /** * Invoke callback after `ms` milliseconds. * Any pending action of this type is cancelled before. */ function _delay(tag, ms, callback) { /*jshint -W040:true */ var self = this; tag = "" + (tag || "default"); if (timerMap[tag] != null) { clearTimeout(timerMap[tag]); delete timerMap[tag]; // console.log("Cancel timer '" + tag + "'"); } if (ms == null || callback == null) { return; } // console.log("Start timer '" + tag + "'"); timerMap[tag] = setTimeout(function() { // console.log("Execute timer '" + tag + "'"); callback.call(self); }, +ms); } /** */ function _callWebservice(cmd, data) { return $.ajax({ url: GBIF_URL + cmd, data: $.extend({}, data), cache: true, headers: { "Api-User-Agent": USER_AGENT }, dataType: "jsonp", }); } /** */ function updateItemDetails(key) { $("#tmplDetails").addClass("busy"); $.bbq.pushState({ key: key }); $.when( _callWebservice("species/" + key), _callWebservice("species/" + key + "/speciesProfiles"), _callWebservice("species/" + key + "/synonyms"), _callWebservice("species/" + key + "/descriptions"), _callWebservice("species/" + key + "/media") ).done(function(species, profiles, synonyms, descriptions, media) { // Requests are resolved as: [ data, statusText, jqXHR ] species = species[0]; profiles = profiles[0]; synonyms = synonyms[0]; descriptions = descriptions[0]; media = media[0]; var info = $.extend(species, { profileList: profiles.results, // marine, extinct profile: profiles.results.length === 1 ? profiles.results[0] : null, // marine, extinct synonyms: synonyms.results, descriptions: descriptions.results, descriptionsByLang: {}, media: media.results, now: new Date().toString(), }); $.each(info.descriptions, function(i, o) { if (!info.descriptionsByLang[o.language]) { info.descriptionsByLang[o.language] = []; } info.descriptionsByLang[o.language].push(o); }); console.log("updateItemDetails", info); $("#tmplDetails") // .html(tmplDetails(info)) .removeClass("busy"); $("#tmplMedia") // .html(tmplMedia(info)) .removeClass("busy"); $("#tmplInfoPane") .html(tmplInfoPane(info)) .removeClass("busy"); $("[data-toggle='popover']").popover(); $(".carousel").carousel(); $("#mediaCounter").text("" + (media.results.length || "")); // $("[data-toggle='collapse']").collapse(); updateControls(); }); } /** */ function updateBreadcrumb(key, loadTreeNodes) { var $ol = $("ol.breadcrumb").addClass("busy"), activeNode = taxonTree.getActiveNode(); if (activeNode && activeNode.key !== key) { activeNode.setActive(false); // deactivate, in case the new key is not found } $.when( _callWebservice("species/" + key + "/parents"), _callWebservice("species/" + key) ).done(function(parents, node) { // Both requests resolved (result format: [ data, statusText, jqXHR ]) var nodeList = parents[0], keyList = []; nodeList.push(node[0]); // Display as