jQuery.Disqus.js

Rob Loach's picture
  1. // $Id$
  2.  
  3. /**
  4.  * Disqus jQuery plugin
  5.  * Applies Disqus comments to a page using a simple jQuery call.
  6.  * $.Disqus({domain: "myusername"});
  7.  *
  8.  * Copyright (c) 2008 Rob Loach (http://www.robloach.net)
  9.  * Dual licensed under the MIT (MIT-LICENSE.txt)
  10.  * and GPL (GPL-LICENSE.txt) licenses.
  11.  */
  12.  
  13. /**
  14.  * Disqus Global Variables
  15.  */
  16. var disqus_developer = 1;
  17. var disqus_container_id = 'disqus_container_id';
  18. var disqus_message = '';
  19. var disqus_title = '';
  20. var disqus_url = '';
  21.  
  22. /**
  23.  * Apply Disqus comments to the page.
  24.  *
  25.  * Usage:
  26.  * $(document).ready(function(){
  27.  *   $.Disqus({
  28.  *     domain: "", // REQUIRED -- Your Disqus username/domain
  29.  *     fragment: "disqus_thread", // Optional -- The fragment to associate with number of comment links
  30.  *     title: "", // Optional -- The title of the current thread
  31.  *     url: top.location.href, // Optional -- The url of the current thread
  32.  *     message: "", // Optional -- A description of the current thread
  33.  *     developer: 0, // Optional -- Determines whether or not we're working in a development environment
  34.  *     container_id: "disqus_thread" // The container ID for the thread
  35.  *   });
  36.  * );
  37.  * <div id="disqus_thread"></div>
  38.  * <a href="http://example.com/my/page#disqus_thread">Add a comment</a>
  39.  *
  40.  * This will create the Disqus comment thread, stick it into #disqus_thread, and
  41.  * replace "Add a comment" with the number of comments in that thread.
  42.  */
  43. jQuery.Disqus = function(options) {
  44.   // Load the settings with defaults
  45.   var settings = jQuery.extend({
  46.     fragment: "disqus_thread",
  47.     domain: "",
  48.     title: "",
  49.     url: top.location.href,
  50.     message: "",
  51.     developer: 0,
  52.     container_id: "disqus_thread"
  53.   }, options);
  54.   if (settings.domain != "") {
  55.     // Set the global Disqus variables
  56.     disqus_developer = settings.developer;
  57.     disqus_container_id = settings.container_id;
  58.     disqus_message = settings.message;
  59.     disqus_title = settings.title;
  60.     disqus_url = settings.url;
  61.            
  62.     // Construct the query that will be sent to change the number of comments in links  
  63.     var query = '?';
  64.     $("a").each(function(i, obj){
  65.           if (obj.href.split("#")[1] == settings.fragment) {
  66.                 query += 'url' + i + '=' + encodeURIComponent(obj.href) + '&';
  67.           }
  68.     });
  69.    
  70.     // Call the Disqus script to get the number of replies per link
  71.     $.getScript('http://disqus.com/forums/' + settings.domain + '/get_num_replies.js' + query);
  72.    
  73.     // Call the Disqus script to create the Disqus comment thread
  74.     alert('Attempting to create the comment thread!');
  75.     //$('#' + disqus_container_id).load('http://disqus.com/forums/' + settings.domain + '/embed.js');
  76.     //$('#' + disqus_container_id).text('').append('<script type="text/javascript" src="http://disqus.com/forums/' + settings.domain + '/get_num_replies.js"/>');
  77.     //$.getScript('http://disqus.com/forums/' + settings.domain + '/embed.js');
  78.    
  79.     $('#' + disqus_container_id).appendDom([{
  80.       tagName  :'script',
  81.       type : 'text/javascript',
  82.       src : 'http://disqus.com/forums/' + settings.domain + '/embed.js'
  83.     }]);
  84.  
  85.     alert('Done creating thread, ready for the white screen?!');
  86.   }
  87. }
  88.  
  89.  
  90. /**
  91.  * appendDom - Extremely flexible tool for dynamic dom creation.
  92.  *   http://byron-adams.com/projects/jquery/appendDom
  93.  *
  94.  * Copyright (c) 2007 Byron Adams (http://byron-adams.com)
  95.  * Dual licensed under the MIT (MIT-LICENSE.txt)
  96.  * and GPL (GPL-LICENSE.txt) licenses.
  97.  *
  98.  */
  99. jQuery.fn.appendDom = function(template) {
  100.   return this.each(function() {
  101.     for (element in template) {
  102.       var el = (typeof(template[element].tagName) === 'string') ?
  103.         document.createElement(template[element].tagName): document.createTextNode('');
  104.       delete template[element].tagName;
  105.       for (attrib in template[element]) {
  106.         switch ( typeof(template[element][attrib]) ) {
  107.           case 'string' :
  108.             if ( typeof(el[attrib]) === 'string' ) {          
  109.              el[attrib] = template[element][attrib];
  110.             } else {
  111.               el.setAttribute(attrib, template[element][attrib]);
  112.             }
  113.             break;
  114.           case 'function':
  115.             el[attrib] = template[element][attrib];
  116.             break;
  117.           case 'object' :
  118.             if (attrib === 'childNodes')  {$(el).appendDom(template[element][attrib]);}
  119.             break;
  120.         }
  121.       }
  122.       this.appendChild(el);
  123.     }
  124.   });
  125. };