/* * @file * There is a lot of code that goes into creating the "label" of a node * This includes editable cards with all node details, and some controls * onCreateLabelHandler is the main function of this file, and the file * also contains a bunch of helper functions * * html and littleHTML are potentially confusing variables * html is the contents of the card shown when you click on a node's label. * littleHTML creates little controls for removing/hiding nodes from the canvas * * This function features PHP-style variable substitution because the strings * are so damn long. Values are identified by $_id_$, and then a regular * expression is substituted in later (for html, in a separate function). */ function onCreateLabelHandler(domElement, node) { var html = generateShowcardHTML(); html = replaceVariables(html, node); var showCard = document.createElement('div'); showCard.className = 'showcard topic_' + node.id; showCard.innerHTML = html; showCard.style.display = "none"; domElement.appendChild(showCard); // Create a 'name' button and add it to the main node label var nameContainer = document.createElement('span'), style = nameContainer.style; nameContainer.className = 'name topic_' + node.id; nameContainer.id = 'topic_' + node.id + '_label'; nameContainer.innerHTML = generateLittleHTML (node); domElement.appendChild(nameContainer); style.fontSize = "0.9em"; style.color = "#222222"; bindCallbacks(showCard, nameContainer, node); } function generateShowcardHTML() { return ' \
\

$_metacode_$

\ $_metacode_$ \ \ $_name_$ \ \ \ \
\
\
\ Added by: $_username_$ \ \
\
\
\ $_desc_$ \
\
\
\ \
\
'; }//generateShowcardHTML function replaceVariables(html, node) { //link is rendered differently if user is logged out or in var go_link, a_tag, close_a_tag; if (userid == null) { go_link = ''; if (node.getData("link") != "") { a_tag = ''; close_a_tag = ''; } else { a_tag = ''; close_a_tag = ''; } } else { go_link = '[go]'; a_tag = ''; close_a_tag = ''; } //create metacode_choices array from imgArray var metacodes = new Array(); for (var key in imgArray) { if (imgArray.hasOwnProperty(key)) { if (key != node.getData("metacode")) { metacodes.push(key); } } } //Arrange it how we want it metacodes.sort(); metacodes.unshift(node.getData("metacode")); var metacode_choices = "'["; for (var i in metacodes) { metacode_choices += '["' + metacodes[i] + '","' + metacodes[i] + '"],'; } //remove trailing comma and add ] metacode_choices = metacode_choices.slice(0, -1); metacode_choices += "]'"; var desc_nil = "Click to add description."; var link_nil = "Click to add link."; html = html.replace(/\$_id_\$/g, node.id); html = html.replace(/\$_metacode_\$/g, node.getData("metacode")); html = html.replace(/\$_imgsrc_\$/g, imgArray[node.getData("metacode")].src); html = html.replace(/\$_name_\$/g, node.name); html = html.replace(/\$_userid_\$/g, node.getData("userid")); html = html.replace(/\$_username_\$/g, node.getData("username")); html = html.replace(/\$_metacode_choices_\$/g, metacode_choices); html = html.replace(/\$_go_link_\$/g, go_link); html = html.replace(/\$_a_tag_\$/g, a_tag); html = html.replace(/\$_close_a_tag_\$/g, close_a_tag); if (node.getData("link") == "" && userid != null) { html = html.replace(/\$_link_\$/g, link_nil); } else { html = html.replace(/\$_link_\$/g, node.getData("link")); } html = html.replace(/\$_desc_nil_\$/g, desc_nil); if (node.getData("desc") == "" && userid != null) { //logged in but desc isn't there so it's invisible html = html.replace(/\$_desc_\$/g, desc_nil); } else { html = html.replace(/\$_desc_\$/g, node.getData("desc")); } return html; } function generateLittleHTML(node) { var littleHTML = ' \
$_name_$
\
'; if ((userid == null || mapid == null) && node.id != Mconsole.root) { //unauthenticated, not on a map: can remove from canvas littleHTML += ' \ \ '; } else if (mapid != null && userid != null && node.id != Mconsole.root) { //not on a map, authenticated, and not looking at root node //(you can't delete the root node of a JIT graph) littleHTML += ' \ \ \ \ '; } if (userid != null && node.id != Mconsole.root) { //logged in, whether you're on a map or not littleHTML += ' \ \ '; } littleHTML += '
'; littleHTML = littleHTML.replace(/\$_id_\$/g, node.id); littleHTML = littleHTML.replace(/\$_mapid_\$/g, mapid); littleHTML = littleHTML.replace(/\$_name_\$/g, node.name); return littleHTML; } function hideCard(node) { var card = '.showcard'; if (node != null) { card += '.topic_' + node.id; } $(card).fadeOut('fast', function(){ node.setData('dim', 25, 'current'); $('.name').show(); Mconsole.plot(); }); } function bindCallbacks(showCard, nameContainer, node) { // add some events to the label $(showCard).find('img.icon').click(function(){ hideCard(node); }); $(showCard).find('.scroll').mCustomScrollbar(); // add some events to the label $(nameContainer).find('.label').click(function(e){ $('.showcard').css('display','none'); $('.name').css('display','block'); $('.name.topic_' + node.id).css('display','none'); $('.showcard.topic_' + node.id).fadeIn('fast'); selectNodeOnClickHandler(node,e); node.setData('dim', 1, 'current'); }); nameContainer.onmouseover = function(){ $('.name.topic_' + node.id + ' .nodeOptions').css('display','block'); } nameContainer.onmouseout = function(){ $('.name.topic_' + node.id + ' .nodeOptions').css('display','none'); } //bind best_in_place ajax callbacks $(showCard).find('.best_in_place_metacode').bind("ajax:success", function() { var metacode = $(this).html(); //changing img alt, img src for top card (topic view page) //and on-canvas card. Also changing image of node $(showCard).find('img.icon').attr('alt', metacode); $(showCard).find('img.icon').attr('src', imgArray[metacode].src); node.setData("metacode", metacode); Mconsole.plot(); }); $(showCard).find('.best_in_place_name').bind("ajax:success", function() { var name = $(this).html(); $(nameContainer).find('.label').html(name); }); //available if you want it :) //$(showCard).find('.best_in_place_desc').bind("ajax:success", function() { // var desc = $(this).html(); //}); $(showCard).find('.best_in_place_link').bind("ajax:success", function() { var link = $(this).html(); $(showCard).find('.go-link').attr('href', link); }); }