PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/files/socialite/1.0.0/socialite.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 457 lines | 362 code | 40 blank | 55 comment | 85 complexity | c4e0dbc98024ab46adebc33572a278b7 MD5 | raw file
  1. /*!
  2. * Socialite v1.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. var Socialite = { },
  10. // internal functions
  11. _socialite = { },
  12. // social networks and callback functions to initialise each instance
  13. networks = { },
  14. // remembers which scripts have been appended
  15. appended = { },
  16. // a collection of URLs for external scripts
  17. sources = { },
  18. // remember loaded scripts
  19. loaded = { },
  20. // all Socialite button instances
  21. cache = { },
  22. sto = window.setTimeout,
  23. euc = encodeURIComponent,
  24. gcn = typeof document.getElementsByClassName === 'function';
  25. // append a known script element once
  26. _socialite.appendScript = function(network, id, callback)
  27. {
  28. if (appended[network] || sources[network] === undefined) {
  29. return false;
  30. }
  31. var js = appended[network] = document.createElement('script');
  32. js.async = true;
  33. js.src = sources[network];
  34. js.onload = js.onreadystatechange = function ()
  35. {
  36. if (_socialite.hasLoaded(network)) {
  37. return;
  38. }
  39. var rs = js.readyState;
  40. if ( ! rs || rs === 'loaded' || rs === 'complete') {
  41. loaded[network] = true;
  42. js.onload = js.onreadystatechange = null;
  43. // activate all instances from cache if no callback is defined
  44. if (callback !== undefined) {
  45. if (typeof callback === 'function') {
  46. callback();
  47. }
  48. } else {
  49. _socialite.activateCache(network);
  50. }
  51. }
  52. };
  53. if (id) {
  54. js.id = id;
  55. }
  56. document.body.appendChild(js);
  57. return true;
  58. };
  59. // check if an appended script has loaded
  60. _socialite.hasLoaded = function(network)
  61. {
  62. return (typeof network !== 'string') ? false : loaded[network] === true;
  63. };
  64. // remove an appended script
  65. _socialite.removeScript = function(network) {
  66. if ( ! _socialite.hasLoaded(network)) {
  67. return false;
  68. }
  69. document.body.removeChild(appended[network]);
  70. appended[network] = loaded[network] = false;
  71. return true;
  72. };
  73. // return an iframe element and activate the instance on load
  74. _socialite.createIframe = function(src, instance)
  75. {
  76. var iframe = document.createElement('iframe');
  77. iframe.style.cssText = 'overflow: hidden; border: none;';
  78. iframe.setAttribute('allowtransparency', 'true');
  79. iframe.setAttribute('frameborder', '0');
  80. iframe.setAttribute('scrolling', 'no');
  81. iframe.setAttribute('src', src);
  82. if (instance !== undefined) {
  83. iframe.onload = iframe.onreadystatechange = function ()
  84. {
  85. var rs = iframe.readyState;
  86. if ( ! rs || rs === 'loaded' || rs === 'complete') {
  87. iframe.onload = iframe.onreadystatechange = null;
  88. _socialite.activateInstance(instance);
  89. }
  90. };
  91. }
  92. return iframe;
  93. };
  94. // called once an instance is ready to display
  95. _socialite.activateInstance = function(instance)
  96. {
  97. if (instance.loaded) {
  98. return;
  99. }
  100. instance.loaded = true;
  101. instance.container.className += ' socialite-loaded';
  102. };
  103. // activate all instances waiting in the cache
  104. _socialite.activateCache = function(network)
  105. {
  106. if (cache[network] !== undefined) {
  107. for (var i = 0; i < cache[network].length; i++) {
  108. _socialite.activateInstance(cache[network][i]);
  109. }
  110. }
  111. };
  112. // copy data-* attributes from one element to another
  113. _socialite.copyDataAttributes = function(from, to)
  114. {
  115. var i, attr = from.attributes;
  116. for (i = 0; i < attr.length; i++) {
  117. var key = attr[i].name,
  118. val = attr[i].value;
  119. if (key.indexOf('data-') === 0 && val.length) {
  120. to.setAttribute(key, val);
  121. }
  122. }
  123. };
  124. // return data-* attributes from an element as a query string or object
  125. _socialite.getDataAttributes = function(from, noprefix, nostr)
  126. {
  127. var i, str = '', obj = {}, attr = from.attributes;
  128. for (i = 0; i < attr.length; i++) {
  129. var key = attr[i].name,
  130. val = attr[i].value;
  131. if (key.indexOf('data-') === 0 && val.length) {
  132. if (noprefix === true) {
  133. key = key.substring(5);
  134. }
  135. if (nostr) {
  136. obj[key] = val;
  137. } else {
  138. str += euc(key) + '=' + euc(val) + '&';
  139. }
  140. }
  141. }
  142. return nostr ? obj : str;
  143. };
  144. // get elements within context with a class name (with fallback for IE < 9)
  145. _socialite.getElements = function(context, name)
  146. {
  147. if (gcn) {
  148. return context.getElementsByClassName(name);
  149. }
  150. var i = 0, elems = [], all = context.getElementsByTagName('*'), len = all.length;
  151. for (i = 0; i < len; i++) {
  152. var cname = ' ' + all[i].className + ' ';
  153. if (cname.indexOf(' ' + name + ' ') !== -1) {
  154. elems.push(all[i]);
  155. }
  156. }
  157. return elems;
  158. };
  159. // load a single button
  160. Socialite.activate = function(elem, network)
  161. {
  162. Socialite.load(null, elem, network);
  163. };
  164. // load and initialise buttons (recursively)
  165. Socialite.load = function(context, elem, network)
  166. {
  167. // if no context use the document
  168. context = (typeof context === 'object' && context !== null && context.nodeType === 1) ? context : document;
  169. // if no element then search the context for instances
  170. if (elem === undefined || elem === null) {
  171. var find = _socialite.getElements(context, 'socialite'),
  172. elems = find,
  173. length = find.length;
  174. if (!length) {
  175. return;
  176. }
  177. // create a new array if we're dealing with a live NodeList
  178. if (typeof elems.item !== undefined) {
  179. elems = [];
  180. for (var i = 0; i < length; i++) {
  181. elems[i] = find[i];
  182. }
  183. }
  184. Socialite.load(context, elems, network);
  185. return;
  186. }
  187. // if an array of elements load individually
  188. if (typeof elem === 'object' && elem.length) {
  189. for (var j = 0; j < elem.length; j++) {
  190. Socialite.load(context, elem[j], network);
  191. }
  192. return;
  193. }
  194. // Not an element? Get outa here!
  195. if (typeof elem !== 'object' || elem.nodeType !== 1) {
  196. return;
  197. }
  198. // if no network is specified or recognised look for one in the class name
  199. if (typeof network !== 'string' || networks[network] === undefined) {
  200. network = null;
  201. var classes = elem.className.split(' ');
  202. for (var k = 0; k < classes.length; k++) {
  203. if (networks[classes[k]] !== undefined) {
  204. network = classes[k];
  205. break;
  206. }
  207. }
  208. if (typeof network !== 'string') {
  209. return;
  210. }
  211. }
  212. if (typeof networks[network] === 'string') {
  213. network = networks[network];
  214. }
  215. if (typeof networks[network] !== 'function') {
  216. return;
  217. }
  218. // create the button elements
  219. var container = document.createElement('div'),
  220. button = document.createElement('div');
  221. container.className = 'socialised ' + network;
  222. button.className = 'socialite-button';
  223. // insert container before parent element, or append to the context
  224. var parent = elem.parentNode;
  225. if (parent === null) {
  226. parent = (context === document) ? document.body : context;
  227. parent.appendChild(container);
  228. } else {
  229. parent.insertBefore(container, elem);
  230. }
  231. // insert button and element into container
  232. container.appendChild(button);
  233. button.appendChild(elem);
  234. // hide element from future loading
  235. elem.className = elem.className.replace(/\bsocialite\b/, '');
  236. // create the button instance and save it in cache
  237. if (cache[network] === undefined) {
  238. cache[network] = [];
  239. }
  240. var instance = {
  241. elem: elem,
  242. button: button,
  243. container: container,
  244. parent: parent,
  245. loaded: false
  246. };
  247. cache[network].push(instance);
  248. // initialise the button
  249. networks[network](instance, _socialite);
  250. };
  251. // extend the array of supported networks
  252. Socialite.extend = function(network, callback, source)
  253. {
  254. if (typeof network !== 'string' || typeof callback !== 'function') {
  255. return false;
  256. }
  257. // split into an array to map multiple classes to one network
  258. network = (network.indexOf(' ') > 0) ? network.split(' ') : [network];
  259. if (networks[network[0]] !== undefined) {
  260. return false;
  261. }
  262. for (var i = 1; i < network.length; i++) {
  263. networks[network[i]] = network[0];
  264. }
  265. if (source !== undefined && typeof source === 'string') {
  266. sources[network[0]] = source;
  267. }
  268. networks[network[0]] = callback;
  269. return true;
  270. };
  271. // boom
  272. return Socialite;
  273. })(window, window.document);
  274. /*
  275. * Socialite Extensions - Pick 'n' Mix!
  276. *
  277. */
  278. (function(window, document, s, undefined)
  279. {
  280. // Twitter
  281. // https://twitter.com/about/resources/
  282. s.extend('twitter tweet', function(instance, _s)
  283. {
  284. var instanceElem = instance.elem,
  285. cn = ' ' + instanceElem.className + ' ';
  286. if (cn.indexOf(' tweet ') !== -1) {
  287. instanceElem.className = 'twitter-tweet';
  288. } else {
  289. var el = document.createElement('a'),
  290. dt = instanceElem.getAttribute('data-type'),
  291. tc = ['share', 'follow', 'hashtag', 'mention'],
  292. ti = 0;
  293. for (var i = 1; i < 4; i++) {
  294. if (dt === tc[i] || cn.indexOf(' ' + tc[i] + ' ') !== -1) {
  295. ti = i;
  296. }
  297. }
  298. el.className = 'twitter-' + tc[ti] + '-button';
  299. if (instanceElem.getAttribute('href') !== undefined) {
  300. el.setAttribute('href', instanceElem.href);
  301. }
  302. _s.copyDataAttributes(instanceElem, el);
  303. instance.button.replaceChild(el, instanceElem);
  304. }
  305. var twttr = window.twttr;
  306. if (typeof twttr === 'object' && typeof twttr.widgets === 'object' && typeof twttr.widgets.load === 'function') {
  307. twttr.widgets.load();
  308. _s.activateInstance(instance);
  309. } else {
  310. if (_s.hasLoaded('twitter')) {
  311. _s.removeScript('twitter');
  312. }
  313. if (_s.appendScript('twitter', 'twitter-wjs', false)) {
  314. window.twttr = {
  315. _e: [function() {
  316. _s.activateCache('twitter');
  317. }]
  318. };
  319. }
  320. }
  321. }, '//platform.twitter.com/widgets.js');
  322. // Google+
  323. // https://developers.google.com/+/plugins/+1button/
  324. s.extend('googleplus', function(instance, _s)
  325. {
  326. var instanceElem = instance.elem,
  327. el = document.createElement('div');
  328. el.className = 'g-plusone';
  329. _s.copyDataAttributes(instanceElem, el);
  330. instance.button.replaceChild(el, instanceElem);
  331. if (typeof window.gapi === 'object' && typeof window.gapi.plusone === 'object' && typeof gapi.plusone.render === 'function') {
  332. window.gapi.plusone.render(instance.button, _s.getDataAttributes(el, true, true));
  333. _s.activateInstance(instance);
  334. } else {
  335. if ( ! _s.hasLoaded('googleplus')) {
  336. _s.appendScript('googleplus');
  337. }
  338. }
  339. }, '//apis.google.com/js/plusone.js');
  340. // Facebook
  341. // http://developers.facebook.com/docs/reference/plugins/like/
  342. s.extend('facebook', function(instance, _s)
  343. {
  344. var instanceElem = instance.elem,
  345. el = document.createElement('div'),
  346. fbElem = document.getElementById('fb-root');
  347. if (!fbElem && !_s.hasLoaded('facebook')) {
  348. fbElem = document.createElement('div');
  349. fbElem.id = 'fb-root';
  350. document.body.appendChild(fbElem);
  351. el.className = 'fb-like';
  352. _s.copyDataAttributes(instanceElem, el);
  353. instance.button.replaceChild(el, instanceElem);
  354. _s.appendScript('facebook', 'facebook-jssdk');
  355. } else {
  356. var src = '//www.facebook.com/plugins/like.php?';
  357. src += _s.getDataAttributes(instanceElem, true);
  358. var iframe = _s.createIframe(src, instance);
  359. instance.button.replaceChild(iframe, instanceElem);
  360. }
  361. }, '//connect.facebook.net/en_US/all.js#xfbml=1');
  362. // LinkedIn
  363. // http://developer.linkedin.com/plugins/share-button/
  364. s.extend('linkedin', function(instance, _s)
  365. {
  366. var instanceElem = instance.elem,
  367. attr = instanceElem.attributes,
  368. el = document.createElement('script');
  369. el.type = 'IN/Share';
  370. _s.copyDataAttributes(instanceElem, el);
  371. instance.button.replaceChild(el, instanceElem);
  372. if (typeof window.IN === 'object' && typeof window.IN.init === 'function') {
  373. window.IN.init();
  374. _s.activateInstance(instance);
  375. } else {
  376. if (!_s.hasLoaded('linkedin')) {
  377. _s.appendScript('linkedin');
  378. }
  379. }
  380. }, '//platform.linkedin.com/in.js');
  381. // Pinterest "pin It" Button
  382. // http://pinterest.com/about/goodies/
  383. s.extend('pinit', function(instance, _s)
  384. {
  385. var instanceElem = instance.elem,
  386. el = document.createElement('a');
  387. el.className = 'pin-it-button';
  388. if (instanceElem.getAttribute('href') !== undefined) {
  389. el.setAttribute('href', instanceElem.href);
  390. }
  391. var layout = instanceElem.getAttribute('data-count-layout') || 'horizontal';
  392. el.setAttribute('count-layout', layout);
  393. instance.button.replaceChild(el, instanceElem);
  394. if (_s.hasLoaded('pinit')) {
  395. _s.removeScript('pinit');
  396. }
  397. _s.appendScript('pinit');
  398. }, '//assets.pinterest.com/js/pinit.js');
  399. // Spotify Play Button
  400. // https://developer.spotify.com/technologies/spotify-play-button/
  401. s.extend('spotify-play', function(instance, _s)
  402. {
  403. var instanceElem = instance.elem,
  404. src = 'https://embed.spotify.com/?',
  405. width = parseInt(instanceElem.getAttribute('data-width'), 10),
  406. height = parseInt(instanceElem.getAttribute('data-height'), 10);
  407. instanceElem.removeAttribute('data-width');
  408. instanceElem.removeAttribute('data-height');
  409. src += 'uri=' + instanceElem.getAttribute('href') + '&';
  410. src += _s.getDataAttributes(instanceElem, true);
  411. var iframe = _s.createIframe(src, instance);
  412. iframe.style.width = (isNaN(width) ? 300 : width) + 'px';
  413. iframe.style.height = (isNaN(height) ? 380 : height) + 'px';
  414. instance.button.replaceChild(iframe, instanceElem);
  415. _s.activateInstance(instance);
  416. }, '');
  417. })(window, window.document, window.Socialite);