FANDOM


/*!

* Socialite v2.0
* http://socialitejs.com
* Copyright (c) 2011 David Bushell
* Dual-licensed under the BSD or MIT licenses: http://socialitejs.com/license.txt
*/

window.Socialite = (function(window, document, undefined) {

   'use strict';
   var uid       = 0,
       instances = [ ],
       networks  = { },
       widgets   = { },
       rstate    = /^($|loaded|complete)/,
       euc       = window.encodeURIComponent;
   var socialite = {
       settings: { },
       trim: function(str)
       {
           return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g,);
       },
       hasClass: function(el, cn)
       {
           return (' ' + el.className + ' ').indexOf(' ' + cn + ' ') !== -1;
       },
       addClass: function(el, cn)
       {
           if (!socialite.hasClass(el, cn)) {
               el.className = (el.className === ) ? cn : el.className + ' ' + cn;
           }
       },
       removeClass: function(el, cn)
       {
           el.className = socialite.trim(' ' + el.className + ' '.replace(' ' + cn + ' ', ' '));
       },
       /**
        * Copy properties of one object to another
        */
       extendObject: function(to, from, overwrite)
       {
           for (var prop in from) {
               var hasProp = to[prop] !== undefined;
               if (hasProp && typeof from[prop] === 'object') {
                   socialite.extendObject(to[prop], from[prop], overwrite);
               } else if (overwrite || !hasProp) {
                   to[prop] = from[prop];
               }
           }
       },
       /**
        * Return elements with a specific class
        *
        * @param context - containing element to search within
        * @param cn      - class name to search for
        *
        */
       getElements: function(context, cn)
       {
           // copy to a new array to avoid a live NodeList
           var i   = 0,
               el  = [ ],
               gcn = !!context.getElementsByClassName,
               all = gcn ? context.getElementsByClassName(cn) : context.getElementsByTagName('*');
           for (; i < all.length; i++) {
               if (gcn || socialite.hasClass(all[i], cn)) {
                   el.push(all[i]);
               }
           }
           return el;
       },
       /**
        * Return data-* attributes of element as a query string (or object)
        *
        * @param el       - the element
        * @param noprefix - (optional) if true, remove "data-" from attribute names
        * @param nostr    - (optional) if true, return attributes in an object
        *
        */
       getDataAttributes: function(el, noprefix, nostr)
       {
           var i    = 0,
               str  = ,
               obj  = { },
               attr = el.attributes;
           for (; i < attr.length; i++) {
               var key = attr[i].name,
                   val = attr[i].value;
               if (val.length && key.indexOf('data-') === 0) {
                   if (noprefix) {
                       key = key.substring(5);
                   }
                   if (nostr) {
                       obj[key] = val;
                   } else {
                       str += euc(key) + '=' + euc(val) + '&';
                   }
               }
           }
           return nostr ? obj : str;
       },
       /**
        * Copy data-* attributes from one element to another
        *
        * @param from     - element to copy from
        * @param to       - element to copy to
        * @param noprefix - (optional) if true, remove "data-" from attribute names
        * @param nohyphen - (optional) if true, convert hyphens to underscores in the attribute names
        *
        */
       copyDataAttributes: function(from, to, noprefix, nohyphen)
       {
           // `nohyphen` was needed for Facebook's