/* * c22v2.js: code for Hovers with Ajax content * * $Revision: 1.5 $ * * (c) 1992-2011 Cisco Systems, Inc. All rights reserved. * Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html */ cdc.util.ensureNamespace('cdc.cl'); cdc.cl.c22v2 = { failsafeTimers : {}, storeTemplate : function(templateName, templateContent) { // stores a template by name so that individual triggers can refer to it by name cdc.cl.c22v2[templateName] = new cdc.util.JsTemplate(templateContent); }, makeCachedRequest : function(whichTip, actionURL, template, destination, metricsFnName) { var failsafeTimout = 4100; // need to store a separate timeout per destination //clear previous timers if they exist if ( typeof cdc.cl.c22v2.failsafeTimers[destination] != 'undefined') clearTimeout(cdc.cl.c22v2.failsafeTimers[destination]); cdc.cl.c22v2.failsafeTimers[destination] = setTimeout(function() { cdc.cl.c22v2.throwError(whichTip, destination) }, failsafeTimout); jQuery.Tache.Get({ type : 'GET', url : actionURL, timeout : 4000, dataType : 'jsonp', success : function(data) { clearTimeout(cdc.cl.c22v2.failsafeTimers[destination]); cdc.cl.c22v2.handleResult(whichTip, data, template, destination); }, error : function() { cdc.cl.c22v2.throwError(whichTip, destination); cdc.cl.c22v2.executeFunctionByName(metricsFnName , window,actionURL) } }); }, handleResult : function(whichTip, data, template, destination) { // generateHTML should uses incoming jsTemplate var generateHTML = template.process(data); // compare trigger that came with this request (which might be out of date) with current trigger stored on the shared tooltip if (jQuery(whichTip.getTrigger()).attr('id') == whichTip.getTip().curTrigger) { // append into destination element, a child of current tip jQuery(destination, whichTip.getTip()).html(generateHTML); } else { //console.log('keep your hands off my hover') } }, throwError : function(whichTip, destination) { //console.log(whichTip.getTip()); if (jQuery(whichTip.getTrigger()).attr('id') == whichTip.getTip().curTrigger) { // parent the error into the failing destination var errorHTML = jQuery('.errorTemplate .error', whichTip.getTip()).clone(); jQuery(destination, whichTip.getTip()).html(errorHTML); jQuery(destination + ' .error', whichTip.getTip()).css({ 'display' : 'block' }); } else { //console.log('keep your hands off my hover') } }, makeTip : function(tipClass, tipTemplate, tipBehavior) { // sample object on the trigger looks like this: // {"data": [{"location": "http://tools.cisco.com/software/image/pub/mpd/service/rest/popular?mdfid=&catid=&mpdcount=23&appid=102","target": ".top"},{"location": "http://tools.cisco.com/software/image/pub/mpd/service/rest/popular?mdfid=&catid=&mpdcount=22&appid=102","target": ".bottom"}]} // service location can be omitted, and replaced with direct data, or a reference to an object. The value in Data will be eval()'d // {"data": [{"data": "cdc.local.wpx","target": ".top"},{"location": "http://tools.cisco.com/software/image/pub/mpd/service/rest/popular?mdfid=&catid=&mpdcount=22&appid=102","target": ".bottom"}]} cdc.cl.c22v2.standardConfig = { tip : tipTemplate, //gets the outer tooltip wrapper to use. (can be any of c22 family) onShow : function() { if ( typeof this.getTrigger().attr('data-sources') != 'undefined') { var config = jQuery.parseJSON(this.getTrigger().attr('data-sources')).config; // update current global trigger this.getTip().curTrigger = jQuery(this.getTrigger()).attr('id') for (var each in config) { // decide which template to use if ( typeof config[each]['storedTemplate'] != 'undefined') { // use the one you've stored by name var theTemplate = cdc.cl.c22v2[config[each]['storedTemplate']] } else { // use whatever template is passed in var theTemplate = new cdc.util.JsTemplate(config[each]['template']) } if ( typeof config[each]['location'] != 'undefined') { // if there's a location use service cdc.cl.c22v2.makeCachedRequest(this, config[each]['location'], theTemplate, config[each]['target'],config[each]['metricfnname'] ); } else { if ( typeof config[each]['data'] != 'undefined') { // if there's local data then process it var data = eval(config[each]['data']); //de-serialize the data cdc.cl.c22v2.handleResult(this, data, theTemplate, config[each]['target']); } } } } else { this.getTip().curTrigger = jQuery(this.getTrigger()).attr('id'); var totalRequest = jQuery(this.getTrigger()).attr('data-totalrequest'); for(i=1;i<=totalRequest;i++) { var dataStoredTemplate = jQuery(this.getTrigger()).attr('data-storedtemplate'+i); var dataTemplate = jQuery(this.getTrigger()).attr('data-template'+i); var dataLocation = jQuery(this.getTrigger()).attr('data-location'+i); var dataTarget = jQuery(this.getTrigger()).attr('data-target'+i); var dataContent = jQuery(this.getTrigger()).attr('data-content'+i); var dataMeterics = jQuery(this.getTrigger()).attr('data-metricsfnname'+i); // decide which template to use if ( typeof dataStoredTemplate != 'undefined') { // use the one you've stored by name var theTemplate = cdc.cl.c22v2[dataStoredTemplate]; } else { // use whatever template is passed in var theTemplate = new cdc.util.JsTemplate(dataTemplate); } if ( typeof dataLocation != 'undefined') { // if there's a location use service cdc.cl.c22v2.makeCachedRequest(this, dataLocation, theTemplate, dataTarget, dataMeterics); } else { if ( typeof dataContent != 'undefined') { // if there's local data then process it var data = eval(dataContent); //de-serialize the data cdc.cl.c22v2.handleResult(this, data, theTemplate, dataTarget); } } } } }, onHide : function() { // clear current trigger to null this.getTip().curTrigger = null; // also clear the areas that were updated (designated by ".data" class) //arg, we need to get the config and see if it's an ajax request before adding back spinner if(typeof this.getTrigger().attr('data-sources')!= 'undefined') { var config = jQuery.parseJSON(this.getTrigger().attr('data-sources')).config; for (var each in config) { if ( typeof config[each]['location'] != 'undefined' || typeof config[each]['data'] != 'undefined') { jQuery('.data', this.getTip()).html("
"); } } }else{ var totalRequest = jQuery(this.getTrigger()).attr('data-totalrequest'); for(i=1;i<=totalRequest;i++) { var dataLocation = jQuery(this.getTrigger()).attr('data-location'+i); var dataData = jQuery(this.getTrigger()).attr('data-content'+i); if ( typeof dataLocation != 'undefined' || typeof dataData != 'undefined') { jQuery('.data', this.getTip()).html("
"); } } } } } // extend the custom behaviors into the standard object jQuery.extend(cdc.cl.c22v2.standardConfig, tipBehavior); // invoke the tooltip with the merged config params var api = jQuery(tipClass).tooltip(cdc.cl.c22v2.standardConfig); //.dynamic({classnames:'top1 right1 bottom1 left1'}) //store access to each constructed API (per tip class) cdc.cl.c22v2[tipClass] = api; // respond to close button jQuery(".hoverClose", tipTemplate).click(function() { //cdc.cl.c22v2[tipClass].tooltip().getTip().hide(); jQuery(tipTemplate).hide(); }); }, executeFunctionByName : function (fnName, context) { var args = Array.prototype.slice.call(arguments, 2); var namespaces = fnName.split("."); var func = namespaces.pop(); for (var i = 0; i < namespaces.length; i++) { context = context[namespaces[i]]; } return context[func].apply(context, args); } }