iShare.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /**
  2. * iShare.js
  3. * @author singsong
  4. * @email zhansingsong@gmail.com
  5. * @date 2016.3.6
  6. */
  7. ;
  8. (function (root, factory) {
  9. if ( typeof define === 'function' && define.amd ) {
  10. define([], factory(root));
  11. } else if ( typeof exports === 'object' ) {
  12. module.exports = factory(root);
  13. } else {
  14. root.iShare = factory(root);
  15. }
  16. })(typeof global !== 'undefined' ? global : this.window || this.global, function (root) {
  17. /**
  18. * 严格模式
  19. */
  20. 'use strict';
  21. /**
  22. * Util 单例工具类
  23. */
  24. var Util = {
  25. /**
  26. * event 事件注册与注销
  27. * addEvent 注册事件
  28. * removeEvent 注销事件
  29. */
  30. event: {
  31. addEvent: function(element, type, handler){
  32. if(element.addEventListener){
  33. element.addEventListener(type, handler, false);
  34. } else if(element.attachEvent){
  35. element.attachEvent('on' + type, handler);
  36. } else {
  37. element['on' + type] = handler;
  38. }
  39. },
  40. removeEvent: function(element, type, handler){
  41. if(element.removeEventListener){
  42. element.removeEventListener(type, handler, false);
  43. } else if(element.detachEvent){
  44. element.detachEvent('on' + type, handler);
  45. } else {
  46. element['on' + type] = null;
  47. }
  48. },
  49. stopPropagation: function(event){
  50. if(event.stopPropagation) {
  51. event.stopPropagation();
  52. }else {
  53. event.cancelBubble = true;
  54. }
  55. },
  56. preventDefault: function(event){
  57. if(event.preventDefault){
  58. event.preventDefault();
  59. } else {
  60. event.returnValue = false;
  61. }
  62. }
  63. },
  64. /**
  65. * trim
  66. * @param {String} str 字符串
  67. * @return {String}
  68. */
  69. trim: function(str){
  70. if(String.prototype.trim){
  71. return str.trim();
  72. }
  73. return str.replace(/^\s+|s+$/g, '');
  74. },
  75. /**
  76. * indexOf
  77. * @param {Array} arr 数组
  78. * @param {Object} item 项
  79. * @return {Number} 索引
  80. */
  81. indexOf: function(arr, item){
  82. if(!this.isArray(arr)){
  83. throw new Error(arr.toString() + ' is a non-Array!');
  84. }
  85. if(Array.prototype.indexOf){
  86. return arr.indexOf(item);
  87. }
  88. for(var i = 0, len = arr.length; i < len; i++){
  89. if(arr[i] === item){
  90. return i;
  91. }
  92. }
  93. },
  94. /**
  95. * isArray 判断是否是数组
  96. * @param {Ojbect} arr 被判断对象
  97. * @return {Boolean}
  98. */
  99. isArray: function(arr) {
  100. if(Array.isArray){
  101. return Array.isArray(arr);
  102. }
  103. return Object.prototype.toString.call(arr) === '[object Array]';
  104. },
  105. /**
  106. * validate 验证用户输入的有效性
  107. * @param {Object} ref 参考对象
  108. * @param {Object} o 验证对象
  109. * @return {Array} 错误队列
  110. */
  111. validate: function(ref, o){
  112. var _key,
  113. _result = [];
  114. if(this.isArray(o)){
  115. for(var i = 0, item; item = o[i++];){
  116. if(this.indexOf(ref, item) < 0){
  117. _result.push(item);
  118. }
  119. }
  120. } else {
  121. for(_key in o){
  122. if(!(_key in ref)){
  123. _result.push(_key);
  124. }
  125. }
  126. }
  127. if(_result.length !== 0){
  128. throw new Error('there is such no property: ' + _result.join(', '));
  129. }
  130. },
  131. /**
  132. * getElementTop 获取元素的offsetTop
  133. * @param {DOMObject} element 元素
  134. * @return {Number} offsetTop值
  135. */
  136. getElementTop: function(element) {
  137. var _actualTop = element.offsetTop,
  138. _current = element.offsetParent;
  139. while (_current !== null) {
  140. _actualTop += _current.offsetTop;
  141. _current = _current.offsetParent;
  142. }
  143. return _actualTop;
  144. },
  145. /**
  146. * getElementLeft 获取元素的offsetLeft
  147. * @param {DOMObject} element 元素
  148. * @return {Number} offsetLeft值
  149. */
  150. getElementLeft: function(element) {
  151. var _actualTop = element.offsetLeft,
  152. _current = element.offsetParent;
  153. while (_current !== null) {
  154. _actualTop += _current.offsetLeft;
  155. _current = _current.offsetParent;
  156. }
  157. return _actualTop;
  158. },
  159. /**
  160. * handleParameters 处理URL参数
  161. * @param {Object} options 配置项
  162. * @return {String}
  163. */
  164. handleParameters: function(options) {
  165. var _str = '';
  166. for(var key in options){
  167. _str = _str + key + '=' + encodeURIComponent(options[key]) + '&';
  168. }
  169. return _str;
  170. },
  171. /**
  172. * extend mix-in
  173. * @return {Ojbect}
  174. */
  175. extend: function() {
  176. var _arg,
  177. _prop,
  178. _child = {};
  179. for(_arg = 0; _arg < arguments.length; _arg++) {
  180. for(_prop in arguments[_arg]){
  181. if(arguments[_arg].hasOwnProperty(_prop)){
  182. _child[_prop] = arguments[_arg][_prop];
  183. }
  184. }
  185. }
  186. return _child;
  187. },
  188. /**
  189. * each 遍历数组
  190. * @param {Array} o 数组
  191. * @param {Function} callback 回调函数
  192. * @return {Object}
  193. */
  194. each: function(o, callback) {
  195. if(!o){
  196. return;
  197. }
  198. var _r;
  199. for(var i = 0, l = o.length; i < l; i++){
  200. _r = callback.call(o[i], i, o[i]);
  201. }
  202. return _r;
  203. },
  204. /**
  205. * getElementByclassN 通过class获取元素
  206. * @param {String} classNameStr 类名
  207. * @param {Node} parent 父元素
  208. * @return {DOMObject}
  209. *
  210. * @example
  211. * getElementByclassN('.test');
  212. */
  213. getElementByclassN: function(classNameStr, parent) {
  214. if(!classNameStr){
  215. return;
  216. }
  217. var _result = [];
  218. if(!parent && document.querySelectorAll){
  219. _result = document.querySelectorAll(classNameStr);
  220. if(_result.length > 0){
  221. return _result;
  222. }
  223. }
  224. var _cnArr = classNameStr.split('.'),
  225. _prefix = _cnArr[0] || '*',
  226. _suffix = _cnArr[1],
  227. _parent = parent ? parent : document.body,
  228. _elements = _parent.getElementsByTagName(_prefix),
  229. _classNames,
  230. _target;
  231. var _me = this;
  232. this.each(_elements, function(index, item){
  233. if( item.nodeType === 1 ){
  234. _classNames = item.className.split(/\s+/g);
  235. _target = item;
  236. _me.each(_classNames, function(cindex, citem){
  237. if((citem + '') === _suffix){
  238. _result.push(_target);
  239. }
  240. });
  241. }
  242. });
  243. return _result;
  244. },
  245. /**
  246. * getmeta 通过name获取对应meta的content值
  247. * @param {String} name meta的name
  248. * @return {String}
  249. */
  250. getmeta: function(name) {
  251. var _metas = document.getElementsByTagName('meta');
  252. for(var i = 0, _item; _item = _metas[i++];){
  253. if(_item.getAttribute('name') === name){
  254. return _item.content;
  255. }
  256. }
  257. },
  258. /**
  259. * getimg 获取页面中第一张图片的URL
  260. * @return {String}
  261. */
  262. getimg: function(){
  263. var _imgs = this.convertToArray(document.body.getElementsByTagName('img'));
  264. if(_imgs.length === 0){
  265. return;
  266. }
  267. return encodeURIComponent(_imgs[0].src);
  268. },
  269. /**
  270. * getElement 获取指定元素
  271. * @param {String} selector 选择器(仅支持class和id)
  272. */
  273. getElement: function(selector){
  274. var _node;
  275. if(selector.charAt(0) === '#'){
  276. _node = document.getElementById(selector);
  277. } else {
  278. _node = this.getElementByclassN(selector)[0];
  279. }
  280. return _node;
  281. },
  282. /**
  283. * parseUrl 解析URL
  284. * @param {Object} tpl 模板
  285. * @param {Object} data 数据
  286. * @return {Object}
  287. */
  288. parseUrl: function(tpl, data){
  289. var _tplStr = {};
  290. for(var _name in tpl){
  291. _tplStr[_name] = tpl[_name].replace(/{{([A-Z]*)}}/g, function(match, p1){
  292. var _key = p1.toLowerCase();
  293. if(data[_key]){
  294. return encodeURIComponent(data[_key]);
  295. } else {
  296. return '';
  297. }
  298. });
  299. }
  300. return _tplStr;
  301. },
  302. /**
  303. * isWeixinBrowser 判断是否在微信中
  304. * @return {Boolean}
  305. */
  306. isWeixinBrowser: function(){
  307. var _ua = navigator.userAgent.toLowerCase();
  308. return (/micromessenger/.test(_ua)) ? true : false ;
  309. },
  310. /**
  311. * convertToArray 转换为数组
  312. * @param {NodeList} nodes Nodes数组
  313. * @return {Array}
  314. */
  315. convertToArray: function(nodes){
  316. var _array = null;
  317. try {
  318. _array = Array.prototype.slice.call(nodes, 0);
  319. } catch (ex){
  320. // 针对IE8及之前版本
  321. _array = new Array();
  322. for(var i = 0, len = nodes.length; i < len; i++) {
  323. _array.push(nodes[i]);
  324. }
  325. }
  326. return _array;
  327. },
  328. /**
  329. * parseClassName 解析类名
  330. * @param {String} className 类名
  331. * @param {Object} tpl 模板数据
  332. * @return {String}
  333. */
  334. parseClassName: function(className, tpl){
  335. var _result = null;
  336. var _arr = className.split(/\s+/);
  337. for(var i = 0, item; item = _arr[i++];){
  338. if(item in tpl){
  339. return tpl[item];
  340. }
  341. }
  342. },
  343. /**
  344. * getWinDimension 获取可视页面的尺寸
  345. * @return {Object}
  346. */
  347. getWinDimension: function(){
  348. var _pageWidth = window.innerWidth,
  349. _pageHeight = window.innerHeight;
  350. if(typeof _pageWidth !== 'number'){
  351. if(document.compatMode === 'CSS1Compat'){
  352. _pageWidth = document.documentElement.clientWidth;
  353. _pageHeight = document.documentElement.clientHeight;
  354. } else {
  355. _pageWidth = document.body.clientWidth;
  356. _pageHeight = document.body.clientHeight;
  357. }
  358. }
  359. return {pageWidth: _pageWidth, pageHeight: _pageHeight};
  360. },
  361. /**
  362. * throttle 节流优化
  363. * @param {Function} fn 回调函数
  364. * @param {Number} delay 时间间隔
  365. */
  366. throttle: function(fn, delay){
  367. var timer = null;
  368. return function(){
  369. var context = this, args = arguments;
  370. clearTimeout(timer);
  371. timer = setTimeout(function(){
  372. fn.apply(context, args);
  373. }, delay);
  374. };
  375. },
  376. /**
  377. * loadjs 加载js文件
  378. * @param {String} url 路径
  379. * @param {Function} callback 回调函数
  380. */
  381. loadjs: function() {
  382. var ready = false,
  383. cb = [];
  384. return function(url, callback){
  385. var head = document.getElementsByTagName('head')[0],
  386. node = document.createElement('script'),
  387. isLoaded = document.getElementById('loaded'),
  388. W3C = document.dispatchEvent;
  389. cb.push(callback);
  390. if(!ready){
  391. node.setAttribute('type', 'text/javascript');
  392. node.setAttribute('id', 'loaded');
  393. node.setAttribute('src', url);
  394. node[W3C ? 'onload' : 'onreadystatechange'] = function(){
  395. if(ready){
  396. return;
  397. }
  398. if(W3C || /loaded|complete/i.test(node.readyState)) {
  399. ready = true;
  400. var temp;
  401. while(temp = cb.pop()){
  402. temp();
  403. }
  404. }
  405. };
  406. (!isLoaded) && (head.appendChild(node));
  407. } else {
  408. if(callback){
  409. callback();
  410. }
  411. }
  412. }
  413. }()
  414. };
  415. /**
  416. * WX 微信类
  417. * @param {DOMObject} element 微信按钮节点
  418. * @param {object} options 配置项
  419. *
  420. */
  421. function WX(element, URL, options) {
  422. this.element = element;
  423. this.wxbox = document.createElement('div');
  424. // 配置项
  425. this.URL = URL;
  426. this.settings = options;
  427. this.style = options.style;
  428. this.bgcolor = options.bgcolor;
  429. this.evenType = options.evenType || 'mouseover'; // 默认触发方式
  430. this.isTitleVisibility = (options.isTitleVisibility === void(0)) ? true : options.isTitleVisibility; // 是否有标题
  431. this.title = options.title || '分享到微信';
  432. this.isTipVisibility = (options.isTipVisibility === void(0)) ? true : options.isTipVisibility; // 是否有提示
  433. this.tip = options.tip || '“扫一扫” 即可将网页分享到朋友圈。';
  434. this.upDownFlag = '';// 保存up|down
  435. this.status = false; // 保存状态
  436. this.visibility = false;// 保存可见性
  437. this.qrcode = null; // 保存二维码
  438. }
  439. WX.prototype = function() {
  440. return{
  441. constructor: WX,
  442. init: function() {
  443. this.render();
  444. this.init = this.show;
  445. this.bindEvent();
  446. },
  447. render: function(){
  448. var _upFlag = '',
  449. _downFlag = '',
  450. // _widthStyle = (!this.isTitleVisibility || !this.isTipVisibility) ? 'width: 110px;' : 'width : 150px;',
  451. _imgStyle = '',//待定
  452. _titleStyle = '',//待定
  453. _tipStyle = '', //待定
  454. _bgcolor = this.bgcolor ? this.bgcolor : '#ddd',
  455. _radius = '';
  456. // 判断上下
  457. if (Util.getWinDimension().pageHeight / 2 < Util.getElementTop(this.element)) {
  458. _downFlag = '';
  459. _upFlag = 'display:none;';
  460. this.upDownFlag = 'down';
  461. _radius = 'border-bottom-left-radius: 0;';
  462. } else {
  463. _downFlag = 'display:none;';
  464. _upFlag = '';
  465. this.upDownFlag = 'up';
  466. _radius = 'border-top-left-radius: 0;';
  467. }
  468. var _containerHTML = '<div style="text-align: center;background-color: ' + _bgcolor + ';box-shadow: 1px 1px 4px #888888;padding: 8px 8px 4px;border-radius: 4px;' + _radius + '">',
  469. _titleHTML = this.isTitleVisibility ? '<p class="tt" style="line-height: 30px;margin:0; text-shadow: 1px 1px rgba(0,0,0,0.1);font-weight: 700;margin-bottom: 4px;' + _titleStyle + '">' + this.title + '</p>' : '',
  470. // _imgHTML = '<img style="font-size: 12px;line-height: 20px; -webkit-user-select: none;box-shadow: 1px 1px 2px rgba(0,0,0,0.4); ' + _imgStyle + '" src="' + this.URL + '">',
  471. _imgHTML = '<div class="qrcode" style="width:' + this.settings.qrcodeW + 'px; height:' + this.settings.qrcodeH + 'px; overflow:hidden;"></div>',
  472. _tipHTML = this.isTipVisibility ? '<p style="font-size: 12px;line-height: 20px; margin: 4px auto;width: 120px;' + _tipStyle + '">' + this.tip + '</p>' : '',
  473. _upArrowHTML = '<div style="' + _upFlag + 'position: relative;height: 0;width: 0;border-style: solid;border-width: 12px;border-color: transparent;border-bottom-color: ' + _bgcolor + ';border-top: none;"></div>',
  474. _downArrowHTML = '</div><div style="' + _downFlag + 'position: relative;height: 0;width: 0;border-style: solid;border-width: 12px;border-color: transparent;border-top-color: ' + _bgcolor + ';border-bottom: none;"></div>';
  475. // 拼接WXHTML
  476. var WXSTR = _upArrowHTML + _containerHTML + _titleHTML + _imgHTML + _tipHTML + _downArrowHTML;
  477. this.wxbox.innerHTML = WXSTR;
  478. this.wxbox.style.cssText = 'position:absolute; left: -99999px;';
  479. document.body.appendChild(this.wxbox);
  480. },
  481. setLocation: function(flag){
  482. // 渲染后再调整位置
  483. var _boxW = this.wxbox.offsetWidth,
  484. _boxH = this.wxbox.offsetHeight,
  485. _eW = this.element.offsetWidth,
  486. _eH = this.element.offsetHeight,
  487. _eTop = Util.getElementTop(this.element),
  488. _eLeft = Util.getElementLeft(this.element),
  489. _boxStyle = 'position:absolute; color: #000;z-index: 99999;';
  490. _boxStyle = _boxStyle + 'left: ' + ( _eW / 2 - 12 + _eLeft) + 'px;';
  491. if(this.upDownFlag === 'down'){
  492. _boxStyle = _boxStyle + 'top: ' + (_eTop - _boxH) + 'px;';
  493. } else {
  494. _boxStyle = _boxStyle + 'top: ' + (_eTop + _eH) + 'px;';
  495. }
  496. this.wxbox.style.cssText = _boxStyle + this.style;
  497. flag && (this.hide());
  498. },
  499. bindEvent: function() {
  500. var _me = this;
  501. if(this.evenType === 'click'){
  502. Util.event.addEvent(this.element, 'click', function(e){
  503. var event = e || window.event;
  504. Util.event.stopPropagation(event);
  505. Util.event.preventDefault(event);
  506. if(!_me.visibility){
  507. _me.show();
  508. } else {
  509. _me.hide();
  510. }
  511. });
  512. } else {
  513. Util.event.addEvent(this.element, 'mouseover', function(e){
  514. var event = e || window.event;
  515. // Util.event.stopPropagation(event);
  516. _me.show();
  517. });
  518. Util.event.addEvent(this.element, 'mouseout', function(e){
  519. var event = e || window.event;
  520. // Util.event.stopPropagation(event);
  521. _me.hide();
  522. });
  523. }
  524. Util.event.addEvent(window, 'resize', Util.throttle(function(){
  525. (_me.status) && (_me.visibility) && (_me.setLocation());
  526. }, 200));
  527. },
  528. startQR: function(){
  529. var me = this;
  530. return function(){
  531. if(!me.qrcode){
  532. me.qrcode = new QRCode(Util.getElementByclassN('.qrcode', me.wxbox)[0], {
  533. text: me.URL,
  534. width: me.settings.qrcodeW,
  535. height: me.settings.qrcodeH,
  536. colorDark : me.settings.qrcodeFgc,
  537. colorLight : me.settings.qrcodeBgc
  538. });
  539. }
  540. }
  541. },
  542. show: function(){
  543. this.status = true;
  544. this.wxbox.style.display = 'block';
  545. this.visibility = true;
  546. this.show = function(){
  547. this.wxbox.style.display = 'block';
  548. this.visibility = true;
  549. }
  550. },
  551. hide: function() {
  552. this.wxbox.style.display = 'none';
  553. this.visibility = false;
  554. }
  555. };
  556. }();
  557. /**
  558. * iShare 分享
  559. */
  560. function iShare(options) {
  561. var defaults = {
  562. title : document.title,
  563. url : location.href,
  564. host : location.origin || '',
  565. description : Util.getmeta('description'),
  566. image : Util.getimg(),
  567. WXoptions : {
  568. qrcodeW: 120,
  569. qrcodeH: 120,
  570. qrcodeBgc: '#fff',
  571. qrcodeFgc: '#000',
  572. bgcolor: '#2BAD13'
  573. }
  574. };
  575. var configuration = options || window.iShare_config;
  576. if(configuration){
  577. if(configuration.container){
  578. if(Util.getElement(configuration.container)){
  579. this.container = Util.getElement(configuration.container);
  580. } else {
  581. throw new Error('there is such no className|id: "' + configuration.container + '".');
  582. }
  583. } else {
  584. throw new Error('container property is required.');
  585. }
  586. } else {
  587. throw new Error('container property is required.');
  588. }
  589. var dataSites = this.container.getAttribute('data-sites'),
  590. dataSitesArr = dataSites ? dataSites.split(/\s*,\s*/g) : null;
  591. /* 验证用户输入的有效性 */
  592. (dataSitesArr) && (Util.validate(defaults.sites, dataSitesArr));
  593. (configuration.config) && (Util.validate(defaults, configuration.config));
  594. (configuration.config.sites) && (Util.validate(defaults.sites, configuration.config.sites));
  595. /* WX */
  596. this.wx = null;
  597. /* 保存defaults */
  598. this.defaults = defaults;
  599. this.dataSites = dataSitesArr ? {sites: dataSitesArr} : {};
  600. this.config = configuration.config ? configuration.config : {};
  601. this.settings = Util.extend(defaults, this.config, this.dataSites);
  602. this.settings.WXoptions = Util.extend(defaults.WXoptions, this.config.WXoptions);
  603. this.init();
  604. }
  605. iShare.prototype = (function(){
  606. var _templates = {
  607. iShare_qq : '',
  608. iShare_qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITLE}}&summary={{DESCRIPTION}}&pics={{IMAGE}}&desc=&site=',
  609. iShare_tencent : 'http://share.v.t.qq.com/index.php?c=share&a=index&title={{TITLE}}&url={{URL}}&pic={{IMAGE}}',
  610. iShare_weibo : 'http://service.weibo.com/share/share.php?url={{URL}}&title={{TITLE}}&pic={{IMAGE}}',
  611. iShare_wechat : '',
  612. iShare_douban : 'http://shuo.douban.com/!service/share?href={{URL}}&name={{TITLE}}&text={{DESCRIPTION}}&image={{IMAGE}}',
  613. iShare_renren : 'http://widget.renren.com/dialog/share?resourceUrl={{URL}}&title={{TITLE}}&pic={{IMAGE}}&description={{DESCRIPTION}}',
  614. iShare_youdaonote : 'http://note.youdao.com/memory/?title={{TITLE}}&pic={{IMAGE}}&summary={{DESCRIPTION}}&url={{URL}}',
  615. iShare_linkedin : 'http://www.linkedin.com/shareArticle?mini=true&ro=true&title={{TITLE}}&url={{URL}}&summary={{DESCRIPTION}}&armin=armin',
  616. iShare_facebook : 'https://www.facebook.com/sharer/sharer.php?s=100&p[title]={{TITLE}}p[summary]={{DESCRIPTION}}&p[url]={{URL}}&p[images]={{IMAGE}}',
  617. iShare_twitter : 'https://twitter.com/intent/tweet?text={{TITLE}}&url={{URL}}',
  618. iShare_googleplus : 'https://plus.google.com/share?url={{URL}}&t={{TITLE}}',
  619. iShare_pinterest : 'https://www.pinterest.com/pin/create/button/?url={{URL}}&description={{DESCRIPTION}}&media={{IMAGE}}',
  620. iShare_tumblr : 'https://www.tumblr.com/widgets/share/tool?shareSource=legacy&canonicalUrl=&url={{URL}}&title={{TITLE}}'
  621. };
  622. /**
  623. * _updateUrl 更新添加分享的A标签
  624. */
  625. function _updateUrl(){
  626. if(this.container === void(0) || !this.container.hasChildNodes()){
  627. return;
  628. }
  629. var _children = this.container.childNodes,
  630. _tempURL;
  631. for(var i = 0, item; item = _children[i++];){
  632. if(item.nodeType === 1){
  633. _tempURL = Util.parseClassName(item.className, Util.parseUrl(_templates, this.settings));
  634. /* 验证是否在微信中 */
  635. if((item.className).indexOf('iShare_wechat') > -1 && !(Util.isWeixinBrowser())){
  636. // this.wx = new WX(item, _tempURL, this.settings.WXoptions);
  637. //this.wx = new WX(item, this.settings.url, this.settings.WXoptions);
  638. return;
  639. }else if((item.className).indexOf('iShare_qq') > -1 && !(Util.isWeixinBrowser())){
  640. return;
  641. }else {
  642. _tempURL && (item.href = _tempURL);
  643. item.target = '_blank';
  644. }
  645. }
  646. }
  647. }
  648. //prototype
  649. return{
  650. constructor: iShare,
  651. init: function() {
  652. _updateUrl.call(this);
  653. if(this.wx){
  654. this.bindEvent();
  655. this.wx.init();
  656. // 加载qrcode库
  657. //Util.loadjs('qrcode.min.js', this.wx.startQR());
  658. }
  659. },
  660. bindEvent: function(){
  661. var me = this;
  662. // 只绑定一次,进行初始化
  663. function mouseenterCB(){
  664. me.wx.setLocation(true);
  665. Util.event.removeEvent(me.container, 'mouseover', mouseenterCB);
  666. }
  667. Util.event.addEvent(this.container, 'mouseover', mouseenterCB);
  668. }
  669. }
  670. })();
  671. if(window.iShare_config){
  672. return (new iShare());
  673. } else {
  674. return iShare;
  675. }
  676. });