home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / html / infobox / infobox.js < prev    next >
Encoding:
JavaScript  |  2010-07-29  |  14.0 KB  |  510 lines

  1.  
  2. function format_time(sec, how) {
  3.  var unix = parseInt(sec, 10);
  4.  var millis = sec * 1000;
  5.  millis = (millis);
  6.  
  7.  var now = new Date();
  8.  var d = new Date(millis);
  9.  
  10.  if (how === "pretty") {
  11.   return prettyDate(d);
  12.  } else if (how === "smart") {
  13.   if (_isSameMonth(d, now) || _isLastMonth(d, now)) {
  14.     return d.format("shortTime");
  15.   } else if (d.getFullYear() >= (now.getFullYear() - 1)) { // this year or last year
  16.    return d.format("mmmm d");
  17.   } else {
  18.    return d.format("mmmm d");
  19.   }
  20.  } else {
  21.   return d.format(how);
  22.  }
  23. }
  24.  
  25. function convert_timestamps_to_text(tree) {
  26.  if (!tree) { tree = $(); }
  27.  
  28.  var _do_convert = function(node) {
  29.    if (node === parseInt(node,10)) {
  30.     node = $(this);
  31.    } else {
  32.     node = $(node);
  33.    }
  34.    var tx_text = null;
  35.    try {
  36.  
  37.      ts_text = format_time(node.attr("timestamp"), node.attr("timestyle"));
  38.    } catch (e) {
  39.        console.log("error formatting text:" + e + "/" + node.attr("timestamp"));
  40.    }
  41.  
  42.    try {
  43.      node.find(".time-container").text(ts_text);
  44.    } catch (e) {
  45.        console.log("error attaching text:" + e);
  46.    }
  47.   }
  48.  
  49.  if (tree.attr("timestamp")) _do_convert(tree);
  50.  else tree.find("[timestamp]").each(_do_convert);
  51. }
  52.  
  53. function get_today() {
  54.   return dayOfDate(new Date());
  55. }
  56.  
  57. function _isSameMonth(t1, t2) {
  58.     if (!t2) {
  59.         t2 = new Date();
  60.     }
  61.  
  62.     return ((t1.getFullYear() == t2.getFullYear()) && (t1.getMonth() == t2.getMonth()));
  63. }
  64.  
  65. function _isLastMonth(t1, sinceWhen) {
  66.     if (!sinceWhen) {
  67.         sinceWhen = new Date();
  68.     }
  69.  
  70.     t1 = dayOfDate(t1);
  71.     t2 = dayOfDate(sinceWhen);
  72.  
  73.     t2.setDate(0);
  74.     t2 = new Date(t2.valueOf() - 1);
  75.     // t2 is now the last millisecond of the previous month
  76.  
  77.     return _isSameMonth(t1, t2);
  78. }
  79.  
  80. function relevant_time_diff(t1, t2) {
  81.     // Return a timestamp with granularity appropriate for the difference between
  82.     // the two provided timestamps.
  83.     t1 = dayOfDate(t1);
  84.     if (!t2) t2 = new Date();
  85.     t2 = dayOfDate(t2);
  86.  
  87.     var t1v = t1.valueOf();
  88.     var t2v = t2.valueOf();
  89.  
  90.     var diff_v = t2v - t1v;
  91.  
  92.     // was it in the same day?
  93.     if (diff_v === 0) {
  94.         return t1;
  95.     // yesterday?
  96.     } else if (diff_v <= (1000 * 60 * 60 * 24)) {
  97.         return t1;
  98.     } else if (_isSameMonth(t1, t2)) {
  99.         return t1;
  100.     // last month?
  101.     } else if (_isLastMonth(t1, t2)) {
  102.         // timeframe is the first of the month
  103.         return t1;
  104.     } else if (t1.getFullYear() >= (t2.getFullYear() - 1)) {
  105.         t1.setDate(1)
  106.         return t1;
  107.     } else {
  108.         // timeframe is first of january
  109.         t1.setMonth(0);
  110.         t1.setDate(1); // this must be the fabled "poor date handling" in javascript. starts at 1?
  111.         return t1;
  112.     }
  113. }
  114.  
  115. function _get_timestamp(el) {
  116.   return new Date(parseInt($(el).attr("timestamp"), 10)*1000);
  117. }
  118.  
  119. var separator_count = 0;
  120. function _insert_separator(el, the_date, today_text, today, do_count, initial) {
  121.     do_count = do_count || (do_count === null) || (do_count === undefined);
  122.  
  123.     var date_sep = document.createElement("span");
  124.     $(date_sep).addClass("date-separator");
  125.     $(date_sep).addClass("title");
  126.  
  127.     the_date = dayOfDate(the_date);
  128.     if (today === null || today === undefined) {
  129.         today = get_today();
  130.     }
  131.     var text = null;
  132.     var is_today = the_date.valueOf() == today.valueOf();
  133.  
  134.     if (is_today) {
  135.         $(date_sep).addClass("today");
  136.     }
  137.  
  138.     if (today_text === null || today_text === undefined){
  139.         today_text = TODAY_TEXT;
  140.     }
  141.  
  142.     if (do_count && (separator_count === 0) && today_text) {
  143.         text = today_text || TODAY_TEXT;
  144.     } else if ((!do_count) && initial && today_text) {
  145.         text = today_text || TODAY_TEXT;
  146.     } else if (is_today) {
  147.         text = "Today";
  148.     } else if (_isSameMonth(the_date, today) || _isLastMonth(the_date, today)) {
  149.         if ((today.valueOf() - the_date.valueOf()) <= (1000 * 60 * 60 * 24)) {
  150.             text = "Yesterday";
  151.         } else {
  152.             text = the_date.format("mmmm d");
  153.         }
  154.     } else {
  155.        text = the_date.format("mmmm");
  156.     }
  157.  
  158.    if (do_count){
  159.        separator_count++;
  160.    }
  161.    if (!text) { return };
  162.    $(date_sep).text(text);
  163.  
  164.    $(date_sep).insertBefore($(el));
  165. }
  166.  
  167. function add_date_separators(selector, do_count, initial) {
  168.  if ($(".date-separator:not(.static)").length) return; // don't add date seperators more than once.
  169.  // Get the last time that's still today (i.e. tomorrow - 1)
  170.  var today = get_today();
  171.  var first_event_of_time_period = null;
  172.  var current_time_period = null;
  173.  
  174.  var acts = $(selector);
  175.  for (var i = 0; i < acts.length; i++) {
  176.    var ts = _get_timestamp(acts[i]);
  177.    if (!ts) continue;
  178.    var time_period = relevant_time_diff(ts, today);
  179.    if ((!current_time_period) ||
  180.        (time_period.valueOf() != current_time_period.valueOf())) {
  181.     first_event_of_time_period = acts[i];
  182.     current_time_period = time_period;
  183.     _insert_separator(acts[i],
  184.            //time_period,
  185.            _get_timestamp(acts[i]),
  186.            undefined, undefined, do_count, initial);
  187.    }
  188.  }
  189. }
  190.  
  191. function clear_previous_date_separators(tree) {
  192.  if (!tree){
  193.         tree = $();
  194.  };
  195.  separator_count = 0;
  196.  to_remove = tree.find(".date-separator:not(.static)");
  197.  if (to_remove) to_remove.remove();
  198. }
  199.  
  200. function add_time_containers(to_what) {
  201.  $(to_what).each(function() {
  202.   var myspan = document.createElement("span");
  203.   $(myspan).addClass("time-container");
  204.   $(myspan).addClass("minor");
  205.   $(this).append(" ");
  206.   $(this).after(myspan);
  207.  });
  208. }
  209.  
  210. function clear_previous_time_containers() {
  211.  $(".time-container").remove();
  212. }
  213.  
  214. function add_link_class_to_a_tags(tree) {
  215.  if (!tree) { tree = $(); }
  216.  tree.find("a").each(function(){
  217.      $(this).addClass("link");
  218.  });
  219. }
  220.  
  221. var DIGSBY_LIB_CLASS = 'digsbylib';
  222.  
  223. if (window.digsbyLoadedLibs === undefined){
  224.     window.digsbyLoadedLibs = {};
  225. }
  226.  
  227. function ensureLibrariesLoaded(libraries) {
  228.     var head = document.getElementsByTagName('head')[0];
  229.  
  230.     jQuery.each(libraries, function (i, libsrc) {
  231.         if (!digsbyLoadedLibs[libsrc]) {
  232.             console.log('loading script:' + libsrc);
  233.             $(head).append('<script type="text/javascript" src="' + libsrc + '"/>');
  234.             digsbyLoadedLibs[libsrc] = true;
  235.         }
  236.     });
  237. }
  238.  
  239. var contentFragments = {};
  240. var currentContentName = undefined;
  241. function contentContainer() {
  242.     return document.getElementById('digsby_app_content');
  243. }
  244.  
  245. /**
  246.  * Swap visible content to the one keyed by contentName.
  247.  *
  248.  * updateContent(contentName) must have been called prior.
  249.  */
  250.  
  251. function swapToContent(contentName) {
  252.     var container = contentContainer();
  253.  
  254.     if (currentContentName === contentName) {
  255.         console.log('ignoring swapToContent("' + contentName + '") because it is currently active');
  256.         return;
  257.     }
  258.  
  259.     if (!contentFragments[contentName]) {
  260.         console.log('ERROR: "' + contentName + '" content does not exist');
  261.         return;
  262.     }
  263.  
  264.     console.log('switching to content: ' + contentName + ' from: ' + currentContentName);
  265.  
  266.     // store current content in contentFragments
  267.     callSwapOut(currentContentName);
  268.     var fragment = document.createDocumentFragment();
  269.     while (container.childNodes.length)
  270.         fragment.appendChild(container.removeChild(container.firstChild));
  271.     contentFragments[currentContentName] = fragment;
  272.     var switchToNodes = contentFragments[contentName];
  273.  
  274.     while (switchToNodes.childNodes.length)
  275.         container.appendChild(switchToNodes.removeChild(switchToNodes.firstChild));
  276.     // switch to new content
  277.     currentContentName = contentName;
  278.     callSwapIn(contentName);
  279.     console.log('switched to content: ' + contentName);
  280.  
  281. }
  282.  
  283. /**
  284.  * functions for infobox.js clients to register "swap" functions that get called
  285.  * when their content is being swapped in or out.
  286.  */
  287.  
  288. var _swapFunctions = {};
  289.  
  290. function callSwapIn(name) {
  291.     if (name in _swapFunctions)
  292.         jQuery.each(_swapFunctions[name]['in'], function (i, f) { f(); });
  293. }
  294.  
  295. function callSwapOut(name) {
  296.     if (name in _swapFunctions)
  297.         jQuery.each(_swapFunctions[name]['out'], function (i, f) { f(); });
  298. }
  299.  
  300. function maybeCreateSwapDelegates(name) {
  301.     if (!(name in _swapFunctions))
  302.         _swapFunctions[name] = {'in': [], 'out': []};
  303. }
  304.  
  305. function swapIn(func) {
  306.     maybeCreateSwapDelegates(currentContentName);
  307.     _swapFunctions[currentContentName]['in'].push(func);
  308. }
  309.  
  310. function swapOut(func) {
  311.     maybeCreateSwapDelegates(currentContentName);
  312.     _swapFunctions[currentContentName]['out'].push(func);
  313. }
  314.  
  315. /**
  316.  * onHide: registers a function to be called when the infobox is hiding,
  317.  * or when the current app content is being swapped out.
  318.  */
  319. var _onHide = [];
  320.  
  321. function onHide(func) {
  322.     _onHide.push(func);
  323. }
  324.  
  325. function callOnHide() {
  326.     window.scroll(0, 0);
  327.     jQuery.each(_onHide, function (i, f) { f(); });
  328. }
  329.  
  330. function clearOnHide() {
  331.     _onHide = [];
  332. }
  333.  
  334. function jq_init_scripts(node){
  335.     jQuery(node).find('script').each(function(_i, elem){
  336.         jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
  337.         if ( elem.parentNode )
  338.             elem.parentNode.removeChild( elem );
  339.     });
  340. }
  341.  
  342. /**
  343.  * Updates the content keyed by contentName with new data.
  344.  *
  345.  * Content can be HTML or a DocumentFragment.
  346.  */
  347. function updateContent(contentName, content) {
  348.     console.log('updateContent start: ' + contentName);
  349.     // clear swap functions
  350.  
  351.     if (currentContentName === contentName) {
  352.         callSwapOut(currentContentName);
  353.         updateCurrentContent(content);
  354.         delete _swapFunctions[contentName];
  355.         console.log('updateContent end1: ' + contentName);
  356.         return;
  357.     }
  358.  
  359.     delete _swapFunctions[contentName];
  360.  
  361.  
  362.     if (contentFragments[contentName]){
  363.         while (contentFragments[contentName].childNodes.length)
  364.             jQuery(contentFragments[contentName].removeChild(contentFragments[contentName].firstChild)).remove();
  365.         jQuery(contentFragments[contentName]).remove();
  366.     }
  367.     var newContent = document.createElement('div');
  368.     if (typeof content === 'string') {
  369.         console.log('updateContent(string of length ' + content.length + ')');
  370.     } else {
  371.         console.log('updateContent(node of length ' + content.length + ')');
  372.     }
  373.     if (typeof content === 'string')
  374.         newContent.innerHTML = content;
  375.     else
  376.         newContent.appendChild(content);
  377.  
  378.     jq_init_scripts(newContent);
  379.     var fragment = document.createDocumentFragment();
  380.  
  381.     while (newContent.childNodes.length)
  382.         fragment.appendChild(newContent.removeChild(newContent.firstChild));
  383.  
  384.     contentFragments[contentName] = fragment;
  385.     console.log('updateContent end2: ' + contentName);
  386. }
  387.  
  388. /**
  389.  * Updates the currently shown content with HTML or a DocumentFragment.
  390.  */
  391. function updateCurrentContent(content) {
  392.     console.log('updateCurrentContent start');
  393.     var container = contentContainer();
  394.     while (container.childNodes.length)
  395.         jQuery(container.removeChild(container.firstChild)).remove();
  396.  
  397.     if (typeof content === 'string') {
  398.         console.log('updateCurrentContent(string of length ' + content.length + ')');
  399.     } else
  400.         console.log('updateCurrentContent(node of length ' + content.length + ')');
  401.     var newContent = document.createElement('div');
  402.     if (typeof content === 'string')
  403.         newContent.innerHTML = content;
  404.     else
  405.         newContent.appendChild(content);
  406.  
  407.     jq_init_scripts(newContent);
  408.  
  409.     while (newContent.childNodes.length)
  410.         container.appendChild(newContent.removeChild(newContent.firstChild));
  411.  
  412.     console.log('updateCurrentContent end');
  413. }
  414.  
  415. /**
  416.  * Returns the DocumentFragment for contentName.
  417.  */
  418. function getContent(contentName) {
  419.     return contentFragments[contentName];
  420. }
  421.  
  422. /**
  423.  * Export a function called callWithInfoboxOnLoad which replaces $
  424.  * with a fake onload register function that is immediately called after.
  425.  */
  426. (function() {
  427.     var _jq = window.$;
  428.     var callbacks = [];
  429.     var onLoad = function(func) { callbacks.push(func); };
  430.     var fake$ = function(selector, context) {
  431.         return _jq.isFunction(selector) && !context ? onLoad(selector) : _jq(selector, context);
  432.     };
  433.     this.callWithInfoboxOnLoad = function(func) {
  434.         window.$ = fake$;
  435.         try { func(); }
  436.         finally { $ = _jq; }
  437.         for (var i = 0; i < callbacks.length; ++i)
  438.             callbacks[i]();
  439.         callbacks.length = 0;
  440.     };
  441. })();
  442.  
  443. function setDesiredHeightCallback(cb){
  444.     window.desired_height_callback = cb;
  445. }
  446.  
  447. function setInfoboxDesiredHeight(size) {
  448.     window.digsby_infobox_desired_height = size;
  449.     window.desired_height_callback();
  450. }
  451.  
  452. function setViewport(img, dim) {
  453.     var width = img.naturalWidth;
  454.     var height = img.naturalHeight;
  455.     var w = width;
  456.     var h = height;
  457.  
  458.     if (width < 0 | height < 0){
  459.         return;
  460.     }
  461.  
  462.     if (width > height){
  463.         if (height > dim) {
  464.             img.height = dim;
  465.         }
  466.         height = img.height;
  467.         width = img.width = w/h * height;
  468.         x = (width - dim) / 2;
  469.         y = (height - dim) / 2;
  470.     } else {
  471.         if (width > dim) {
  472.             img.width = dim;
  473.         }
  474.         width = img.width;
  475.         height = img.height = h/w * width;
  476.         y = (height/3) - dim;
  477.         x = 0;
  478.     }
  479.  
  480.     width = height = dim;
  481.     if (y < 0) {
  482.         y = 0;
  483.     }
  484.     if (x < 0) {
  485.         x = 0;
  486.     }
  487.  
  488.     img.style.left = "-" + x + "px";
  489.     img.style.top  = "-" + y + "px";
  490.  
  491.     if (width !== undefined) {
  492.         $(img).parents()[0].style.width  = width  + "px";
  493.         $(img).parents()[0].style.height = height + "px";
  494.     }
  495. }
  496.  
  497. function clip_imgs(tree){
  498.     tree.find(".clipped").each(function(){
  499.         $(this).load(function(){
  500.             setViewport(this, 48);
  501.         })
  502.     });
  503.     tree.find(".clipped-small").each(function(){
  504.         $(this).load(function(){
  505.             setViewport(this, 32);
  506.         })
  507.     });
  508. }
  509.  
  510.