PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/files/socialite/2.0/socialite.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 706 lines | 487 code | 67 blank | 152 comment | 89 complexity | e5414e03b7bb895080fc22e31bc0ab17 MD5 | raw file
  1. /*!
  2. * Socialite v2.0
  3. * http://socialitejs.com
  4. * Copyright (c) 2011 David Bushell
  5. * Dual-licensed under the BSD or MIT licenses: http://socialitejs.com/license.txt
  6. */
  7. window.Socialite = (function(window, document, undefined)
  8. {
  9. 'use strict';
  10. var uid = 0,
  11. instances = [ ],
  12. networks = { },
  13. widgets = { },
  14. rstate = /^($|loaded|complete)/,
  15. euc = window.encodeURIComponent;
  16. var socialite = {
  17. settings: { },
  18. trim: function(str)
  19. {
  20. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g,'');
  21. },
  22. hasClass: function(el, cn)
  23. {
  24. return (' ' + el.className + ' ').indexOf(' ' + cn + ' ') !== -1;
  25. },
  26. addClass: function(el, cn)
  27. {
  28. if (!socialite.hasClass(el, cn)) {
  29. el.className = (el.className === '') ? cn : el.className + ' ' + cn;
  30. }
  31. },
  32. removeClass: function(el, cn)
  33. {
  34. el.className = socialite.trim(' ' + el.className + ' '.replace(' ' + cn + ' ', ' '));
  35. },
  36. /**
  37. * Copy properties of one object to another
  38. */
  39. extendObject: function(to, from, overwrite)
  40. {
  41. for (var prop in from) {
  42. var hasProp = to[prop] !== undefined;
  43. if (hasProp && typeof from[prop] === 'object') {
  44. socialite.extendObject(to[prop], from[prop], overwrite);
  45. } else if (overwrite || !hasProp) {
  46. to[prop] = from[prop];
  47. }
  48. }
  49. },
  50. /**
  51. * Return elements with a specific class
  52. *
  53. * @param context - containing element to search within
  54. * @param cn - class name to search for
  55. *
  56. */
  57. getElements: function(context, cn)
  58. {
  59. // copy to a new array to avoid a live NodeList
  60. var i = 0,
  61. el = [ ],
  62. gcn = !!context.getElementsByClassName,
  63. all = gcn ? context.getElementsByClassName(cn) : context.getElementsByTagName('*');
  64. for (; i < all.length; i++) {
  65. if (gcn || socialite.hasClass(all[i], cn)) {
  66. el.push(all[i]);
  67. }
  68. }
  69. return el;
  70. },
  71. /**
  72. * Return data-* attributes of element as a query string (or object)
  73. *
  74. * @param el - the element
  75. * @param noprefix - (optional) if true, remove "data-" from attribute names
  76. * @param nostr - (optional) if true, return attributes in an object
  77. *
  78. */
  79. getDataAttributes: function(el, noprefix, nostr)
  80. {
  81. var i = 0,
  82. str = '',
  83. obj = { },
  84. attr = el.attributes;
  85. for (; i < attr.length; i++) {
  86. var key = attr[i].name,
  87. val = attr[i].value;
  88. if (val.length && key.indexOf('data-') === 0) {
  89. if (noprefix) {
  90. key = key.substring(5);
  91. }
  92. if (nostr) {
  93. obj[key] = val;
  94. } else {
  95. str += euc(key) + '=' + euc(val) + '&';
  96. }
  97. }
  98. }
  99. return nostr ? obj : str;
  100. },
  101. /**
  102. * Copy data-* attributes from one element to another
  103. *
  104. * @param from - element to copy from
  105. * @param to - element to copy to
  106. * @param noprefix - (optional) if true, remove "data-" from attribute names
  107. * @param nohyphen - (optional) if true, convert hyphens to underscores in the attribute names
  108. *
  109. */
  110. copyDataAttributes: function(from, to, noprefix, nohyphen)
  111. {
  112. // `nohyphen` was needed for Facebook's <fb:like> elements - remove as no longer used?
  113. var attr = socialite.getDataAttributes(from, noprefix, true);
  114. for (var i in attr) {
  115. to.setAttribute(nohyphen ? i.replace(/-/g, '_') : i, attr[i]);
  116. }
  117. },
  118. /**
  119. * Create iframe element
  120. *
  121. * @param src - iframe URL (src attribute)
  122. * @param instance - (optional) socialite instance to activate on iframe load
  123. *
  124. */
  125. createIframe: function(src, instance)
  126. {
  127. // Socialite v2 has slashed the amount of manual iframe creation, we should aim to avoid this entirely
  128. var iframe = document.createElement('iframe');
  129. iframe.style.cssText = 'overflow: hidden; border: none;';
  130. socialite.extendObject(iframe, { src: src, allowtransparency: 'true', frameborder: '0', scrolling: 'no' }, true);
  131. if (instance) {
  132. iframe.onload = iframe.onreadystatechange = function ()
  133. {
  134. if (rstate.test(iframe.readyState || '')) {
  135. iframe.onload = iframe.onreadystatechange = null;
  136. socialite.activateInstance(instance);
  137. }
  138. };
  139. }
  140. return iframe;
  141. },
  142. /**
  143. * Returns true if network script has loaded
  144. */
  145. networkReady: function(name)
  146. {
  147. return networks[name] ? networks[name].loaded : undefined;
  148. },
  149. /**
  150. * Append network script to the document
  151. */
  152. appendNetwork: function(network)
  153. {
  154. // the activation process is getting a little confusing for some networks
  155. // it would appear a script load event does not mean its global object exists yet
  156. // therefore the first call to `activateAll` may have no effect whereas the second call does, e.g. via `window.twttr.ready`
  157. if (!network || network.appended) {
  158. return;
  159. }
  160. // `network.append` and `network.onload` can cancel progress
  161. if (typeof network.append === 'function' && network.append(network) === false) {
  162. network.appended = network.loaded = true;
  163. socialite.activateAll(network);
  164. return;
  165. }
  166. if (network.script) {
  167. network.el = document.createElement('script');
  168. socialite.extendObject(network.el, network.script, true);
  169. network.el.async = true;
  170. network.el.onload = network.el.onreadystatechange = function()
  171. {
  172. if (rstate.test(network.el.readyState || '')) {
  173. network.el.onload = network.el.onreadystatechange = null;
  174. network.loaded = true;
  175. if (typeof network.onload === 'function' && network.onload(network) === false) {
  176. return;
  177. }
  178. socialite.activateAll(network);
  179. }
  180. };
  181. document.body.appendChild(network.el);
  182. }
  183. network.appended = true;
  184. },
  185. /**
  186. * Remove network script from the document
  187. */
  188. removeNetwork: function(network)
  189. {
  190. if (!socialite.networkReady(network.name)) {
  191. return false;
  192. }
  193. network.el.parentNode.removeChild(network.el);
  194. return !(network.appended = network.loaded = false);
  195. },
  196. /**
  197. * Remove and re-append network script to the document
  198. */
  199. reloadNetwork: function(name)
  200. {
  201. // This is a last-ditch effort for half-baked scripts
  202. var network = networks[name];
  203. if (network && socialite.removeNetwork(network)) {
  204. socialite.appendNetwork(network);
  205. }
  206. },
  207. /**
  208. * Create new Socialite instance
  209. *
  210. * @param el - parent element that will hold the new instance
  211. * @param widget - widget the instance belongs to
  212. *
  213. */
  214. createInstance: function(el, widget)
  215. {
  216. var proceed = true,
  217. instance = {
  218. el : el,
  219. uid : uid++,
  220. widget : widget
  221. };
  222. instances.push(instance);
  223. if (widget.process !== undefined) {
  224. proceed = (typeof widget.process === 'function') ? widget.process(instance) : false;
  225. }
  226. if (proceed) {
  227. socialite.processInstance(instance);
  228. }
  229. instance.el.setAttribute('data-socialite', instance.uid);
  230. instance.el.className = 'socialite ' + widget.name + ' socialite-instance';
  231. return instance;
  232. },
  233. /**
  234. * Process a socialite instance to an intermediate state prior to load
  235. */
  236. processInstance: function(instance)
  237. {
  238. var el = instance.el;
  239. instance.el = document.createElement('div');
  240. instance.el.className = el.className;
  241. socialite.copyDataAttributes(el, instance.el);
  242. // stop over-zealous scripts from activating all instances
  243. if (el.nodeName.toLowerCase() === 'a' && !el.getAttribute('data-default-href')) {
  244. instance.el.setAttribute('data-default-href', el.getAttribute('href'));
  245. }
  246. var parent = el.parentNode;
  247. parent.insertBefore(instance.el, el);
  248. parent.removeChild(el);
  249. },
  250. /**
  251. * Activate a socialite instance
  252. */
  253. activateInstance: function(instance)
  254. {
  255. if (instance && !instance.loaded) {
  256. instance.loaded = true;
  257. if (typeof instance.widget.activate === 'function') {
  258. instance.widget.activate(instance);
  259. }
  260. socialite.addClass(instance.el, 'socialite-loaded');
  261. return instance.onload ? instance.onload(instance.el) : null;
  262. }
  263. },
  264. /**
  265. * Activate all socialite instances belonging to a network
  266. */
  267. activateAll: function(network)
  268. {
  269. if (typeof network === 'string') {
  270. network = networks[network];
  271. }
  272. for (var i = 0; i < instances.length; i++) {
  273. var instance = instances[i];
  274. if (instance.init && instance.widget.network === network) {
  275. socialite.activateInstance(instance);
  276. }
  277. }
  278. },
  279. /**
  280. * Load socialite instances
  281. *
  282. * @param context - (optional) containing element to search within
  283. * @param el - (optional) individual or an array of elements to load
  284. * @param w - (optional) widget name
  285. * @param onload - (optional) function to call after each socialite instance has loaded
  286. * @param process - (optional) process but don't load network (if true)
  287. *
  288. */
  289. load: function(context, el, w, onload, process)
  290. {
  291. // use document as context if unspecified
  292. context = (context && typeof context === 'object' && context.nodeType === 1) ? context : document;
  293. // if no elements search within the context and recurse
  294. if (!el || typeof el !== 'object') {
  295. socialite.load(context, socialite.getElements(context, 'socialite'), w, onload, process);
  296. return;
  297. }
  298. var i;
  299. // if array of elements load each one individually
  300. if (/Array/.test(Object.prototype.toString.call(el))) {
  301. for (i = 0; i < el.length; i++) {
  302. socialite.load(context, el[i], w, onload, process);
  303. }
  304. return;
  305. }
  306. // nothing was found...
  307. if (el.nodeType !== 1) {
  308. return;
  309. }
  310. // if widget name not specified search within the element classes
  311. if (!w || !widgets[w]) {
  312. w = null;
  313. var classes = el.className.split(' ');
  314. for (i = 0; i < classes.length; i++) {
  315. if (widgets[classes[i]]) {
  316. w = classes[i];
  317. break;
  318. }
  319. }
  320. if (!w) {
  321. return;
  322. }
  323. }
  324. // find or create the Socialite instance
  325. var instance,
  326. widget = widgets[w],
  327. sid = parseInt(el.getAttribute('data-socialite'), 10);
  328. if (!isNaN(sid)) {
  329. for (i = 0; i < instances.length; i++) {
  330. if (instances[i].uid === sid) {
  331. instance = instances[i];
  332. break;
  333. }
  334. }
  335. } else {
  336. instance = socialite.createInstance(el, widget);
  337. }
  338. // return if just processing (or no instance found)
  339. if (process || !instance) {
  340. return;
  341. }
  342. // initialise the instance
  343. if (!instance.init) {
  344. instance.init = true;
  345. instance.onload = (typeof onload === 'function') ? onload : null;
  346. widget.init(instance);
  347. }
  348. // append the parent network (all instances will be activated onload)
  349. // or activate immediately if network has already loaded
  350. if (!widget.network.appended) {
  351. socialite.appendNetwork(widget.network);
  352. } else {
  353. if (socialite.networkReady(widget.network.name)) {
  354. socialite.activateInstance(instance);
  355. }
  356. }
  357. },
  358. /**
  359. * Load a single element
  360. *
  361. * @param el - an individual element
  362. * @param w - (optional) widget for this socialite instance
  363. * @param onload - (optional) function to call once each instance has loaded
  364. *
  365. */
  366. activate: function(el, w, onload)
  367. {
  368. // skip the first few steps
  369. window.Socialite.load(null, el, w, onload);
  370. },
  371. /**
  372. * Process elements to an intermediate state prior to load
  373. *
  374. * @param context - containing element to search within
  375. * @param el - (optional) individual or an array of elements to load
  376. * @param w - (optional) widget name
  377. *
  378. */
  379. process: function(context, el, w)
  380. {
  381. // stop before widget initialises instance
  382. window.Socialite.load(context, el, w, null, true);
  383. },
  384. /**
  385. * Add a new social network
  386. *
  387. * @param name - unique name for network
  388. * @param params - additional data and callbacks
  389. *
  390. */
  391. network: function(n, params)
  392. {
  393. networks[n] = {
  394. name : n,
  395. el : null,
  396. appended : false,
  397. loaded : false,
  398. widgets : { }
  399. };
  400. if (params) {
  401. socialite.extendObject(networks[n], params);
  402. }
  403. },
  404. /**
  405. * Add a new social widget
  406. *
  407. * @param name - name of owner network
  408. * @param w - unique name for widget
  409. * @param params - additional data and callbacks
  410. *
  411. */
  412. widget: function(n, w, params)
  413. {
  414. params.name = n + '-' + w;
  415. if (!networks[n] || widgets[params.name]) {
  416. return;
  417. }
  418. params.network = networks[n];
  419. networks[n].widgets[w] = widgets[params.name] = params;
  420. },
  421. /**
  422. * Change the default Socialite settings for each network
  423. */
  424. setup: function(params)
  425. {
  426. socialite.extendObject(socialite.settings, params, true);
  427. }
  428. };
  429. return socialite;
  430. })(window, window.document);
  431. /**
  432. * Socialite Extensions - Pick 'n' Mix!
  433. */
  434. (function(window, document, Socialite, undefined)
  435. {
  436. // default to the Queen's English
  437. Socialite.setup({
  438. facebook: {
  439. lang: 'en_GB',
  440. appId: null
  441. },
  442. twitter: {
  443. lang: 'en'
  444. },
  445. googleplus: {
  446. lang: 'en-GB'
  447. }
  448. });
  449. // Facebook
  450. // http://developers.facebook.com/docs/reference/plugins/like/
  451. // http://developers.facebook.com/docs/reference/javascript/FB.init/
  452. Socialite.network('facebook', {
  453. script: {
  454. src : '//connect.facebook.net/{{language}}/all.js',
  455. id : 'facebook-jssdk'
  456. },
  457. append: function(network)
  458. {
  459. var fb = document.createElement('div'),
  460. settings = Socialite.settings.facebook,
  461. events = { onlike: 'edge.create', onunlike: 'edge.remove', onsend: 'message.send' };
  462. fb.id = 'fb-root';
  463. document.body.appendChild(fb);
  464. network.script.src = network.script.src.replace('{{language}}', settings.lang);
  465. window.fbAsyncInit = function() {
  466. window.FB.init({
  467. appId: settings.appId,
  468. xfbml: true
  469. });
  470. for (var e in events) {
  471. if (typeof settings[e] === 'function') {
  472. window.FB.Event.subscribe(events[e], settings[e]);
  473. }
  474. }
  475. };
  476. }
  477. });
  478. Socialite.widget('facebook', 'like', {
  479. init: function(instance)
  480. {
  481. var el = document.createElement('div');
  482. el.className = 'fb-like';
  483. Socialite.copyDataAttributes(instance.el, el);
  484. instance.el.appendChild(el);
  485. if (window.FB && window.FB.XFBML) {
  486. window.FB.XFBML.parse(instance.el);
  487. }
  488. }
  489. });
  490. // Twitter
  491. // https://dev.twitter.com/docs/tweet-button/
  492. // https://dev.twitter.com/docs/intents/events/
  493. // https://developers.google.com/analytics/devguides/collection/gajs/gaTrackingSocial#twitter
  494. Socialite.network('twitter', {
  495. script: {
  496. src : '//platform.twitter.com/widgets.js',
  497. id : 'twitter-wjs',
  498. charset : 'utf-8'
  499. },
  500. append: function()
  501. {
  502. var notwttr = (typeof window.twttr !== 'object'),
  503. settings = Socialite.settings.twitter,
  504. events = ['click', 'tweet', 'retweet', 'favorite', 'follow'];
  505. if (notwttr) {
  506. window.twttr = (t = { _e: [], ready: function(f) { t._e.push(f); } });
  507. }
  508. window.twttr.ready(function(twttr)
  509. {
  510. for (var i = 0; i < events.length; i++) {
  511. var e = events[i];
  512. if (typeof settings['on' + e] === 'function') {
  513. twttr.events.bind(e, settings['on' + e]);
  514. }
  515. }
  516. Socialite.activateAll('twitter');
  517. });
  518. return notwttr;
  519. }
  520. });
  521. var twitterInit = function(instance)
  522. {
  523. var el = document.createElement('a');
  524. el.className = instance.widget.name + '-button';
  525. Socialite.copyDataAttributes(instance.el, el);
  526. el.setAttribute('href', instance.el.getAttribute('data-default-href'));
  527. el.setAttribute('data-lang', instance.el.getAttribute('data-lang') || Socialite.settings.twitter.lang);
  528. instance.el.appendChild(el);
  529. };
  530. var twitterActivate = function(instance)
  531. {
  532. if (window.twttr && typeof window.twttr.widgets === 'object' && typeof window.twttr.widgets.load === 'function') {
  533. window.twttr.widgets.load();
  534. }
  535. };
  536. Socialite.widget('twitter', 'share', { init: twitterInit, activate: twitterActivate });
  537. Socialite.widget('twitter', 'follow', { init: twitterInit, activate: twitterActivate });
  538. Socialite.widget('twitter', 'hashtag', { init: twitterInit, activate: twitterActivate });
  539. Socialite.widget('twitter', 'mention', { init: twitterInit, activate: twitterActivate });
  540. Socialite.widget('twitter', 'embed', {
  541. process: function(instance)
  542. {
  543. instance.innerEl = instance.el;
  544. if (!instance.innerEl.getAttribute('data-lang')) {
  545. instance.innerEl.setAttribute('data-lang', Socialite.settings.twitter.lang);
  546. }
  547. instance.el = document.createElement('div');
  548. instance.el.className = instance.innerEl.className;
  549. instance.innerEl.className = '';
  550. instance.innerEl.parentNode.insertBefore(instance.el, instance.innerEl);
  551. instance.el.appendChild(instance.innerEl);
  552. },
  553. init: function(instance)
  554. {
  555. instance.innerEl.className = 'twitter-tweet';
  556. },
  557. activate: twitterActivate
  558. });
  559. // Google+
  560. // https://developers.google.com/+/plugins/+1button/
  561. // Google does not support IE7
  562. Socialite.network('googleplus', {
  563. script: {
  564. src: '//apis.google.com/js/plusone.js'
  565. },
  566. append: function(network)
  567. {
  568. if (window.gapi) {
  569. return false;
  570. }
  571. window.___gcfg = {
  572. lang: Socialite.settings.googleplus.lang,
  573. parsetags: 'explicit'
  574. };
  575. }
  576. });
  577. var googleplusInit = function(instance)
  578. {
  579. var el = document.createElement('div');
  580. el.className = 'g-' + instance.widget.gtype;
  581. Socialite.copyDataAttributes(instance.el, el);
  582. instance.el.appendChild(el);
  583. instance.gplusEl = el;
  584. };
  585. var googleplusEvent = function(instance, callback) {
  586. return (typeof callback !== 'function') ? null : function(data) {
  587. callback(instance.el, data);
  588. };
  589. };
  590. var googleplusActivate = function(instance)
  591. {
  592. var type = instance.widget.gtype;
  593. if (window.gapi && window.gapi[type]) {
  594. var settings = Socialite.settings.googleplus,
  595. params = Socialite.getDataAttributes(instance.el, true, true),
  596. events = ['onstartinteraction', 'onendinteraction', 'callback'];
  597. for (var i = 0; i < events.length; i++) {
  598. params[events[i]] = googleplusEvent(instance, settings[events[i]]);
  599. }
  600. window.gapi[type].render(instance.gplusEl, params);
  601. }
  602. };
  603. Socialite.widget('googleplus', 'one', { init: googleplusInit, activate: googleplusActivate, gtype: 'plusone' });
  604. Socialite.widget('googleplus', 'share', { init: googleplusInit, activate: googleplusActivate, gtype: 'plus' });
  605. Socialite.widget('googleplus', 'badge', { init: googleplusInit, activate: googleplusActivate, gtype: 'plus' });
  606. // LinkedIn
  607. // http://developer.linkedin.com/plugins/share-button/
  608. Socialite.network('linkedin', {
  609. script: {
  610. src: '//platform.linkedin.com/in.js'
  611. }
  612. });
  613. var linkedinInit = function(instance)
  614. {
  615. var el = document.createElement('script');
  616. el.type = 'IN/' + instance.widget.intype;
  617. Socialite.copyDataAttributes(instance.el, el);
  618. instance.el.appendChild(el);
  619. if (typeof window.IN === 'object' && typeof window.IN.parse === 'function') {
  620. window.IN.parse(instance.el);
  621. Socialite.activateInstance(instance);
  622. }
  623. };
  624. Socialite.widget('linkedin', 'share', { init: linkedinInit, intype: 'Share' });
  625. Socialite.widget('linkedin', 'recommend', { init: linkedinInit, intype: 'RecommendProduct' });
  626. })(window, window.document, window.Socialite);
  627. /**
  628. * Execute any queued functions (don't enqueue before the document has loaded!)
  629. */
  630. (function() {
  631. var s = window._socialite;
  632. if (/Array/.test(Object.prototype.toString.call(s))) {
  633. for (var i = 0, len = s.length; i < len; i++) {
  634. if (typeof s[i] === 'function') {
  635. s[i]();
  636. }
  637. }
  638. }
  639. })();