PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/files/zeroclipboard/1.2.3/ZeroClipboard.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 474 lines | 460 code | 6 blank | 8 comment | 140 complexity | 7d6de6f522d2cff60a5d4c47367cc74b MD5 | raw file
  1. /*!
  2. * ZeroClipboard
  3. * The ZeroClipboard library provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie and a JavaScript interface.
  4. * Copyright (c) 2013 Jon Rohan, James M. Greene
  5. * Licensed MIT
  6. * http://zeroclipboard.org/
  7. * v1.2.3
  8. */
  9. (function() {
  10. "use strict";
  11. var _camelizeCssPropName = function() {
  12. var matcherRegex = /\-([a-z])/g, replacerFn = function(match, group) {
  13. return group.toUpperCase();
  14. };
  15. return function(prop) {
  16. return prop.replace(matcherRegex, replacerFn);
  17. };
  18. }();
  19. var _getStyle = function(el, prop) {
  20. var value, camelProp, tagName, possiblePointers, i, len;
  21. if (window.getComputedStyle) {
  22. value = window.getComputedStyle(el, null).getPropertyValue(prop);
  23. } else {
  24. camelProp = _camelizeCssPropName(prop);
  25. if (el.currentStyle) {
  26. value = el.currentStyle[camelProp];
  27. } else {
  28. value = el.style[camelProp];
  29. }
  30. }
  31. if (prop === "cursor") {
  32. if (!value || value === "auto") {
  33. tagName = el.tagName.toLowerCase();
  34. possiblePointers = [ "a" ];
  35. for (i = 0, len = possiblePointers.length; i < len; i++) {
  36. if (tagName === possiblePointers[i]) {
  37. return "pointer";
  38. }
  39. }
  40. }
  41. }
  42. return value;
  43. };
  44. var _elementMouseOver = function(event) {
  45. if (!ZeroClipboard.prototype._singleton) return;
  46. if (!event) {
  47. event = window.event;
  48. }
  49. var target;
  50. if (this !== window) {
  51. target = this;
  52. } else if (event.target) {
  53. target = event.target;
  54. } else if (event.srcElement) {
  55. target = event.srcElement;
  56. }
  57. ZeroClipboard.prototype._singleton.setCurrent(target);
  58. };
  59. var _addEventHandler = function(element, method, func) {
  60. if (element.addEventListener) {
  61. element.addEventListener(method, func, false);
  62. } else if (element.attachEvent) {
  63. element.attachEvent("on" + method, func);
  64. }
  65. };
  66. var _removeEventHandler = function(element, method, func) {
  67. if (element.removeEventListener) {
  68. element.removeEventListener(method, func, false);
  69. } else if (element.detachEvent) {
  70. element.detachEvent("on" + method, func);
  71. }
  72. };
  73. var _addClass = function(element, value) {
  74. if (element.addClass) {
  75. element.addClass(value);
  76. return element;
  77. }
  78. if (value && typeof value === "string") {
  79. var classNames = (value || "").split(/\s+/);
  80. if (element.nodeType === 1) {
  81. if (!element.className) {
  82. element.className = value;
  83. } else {
  84. var className = " " + element.className + " ", setClass = element.className;
  85. for (var c = 0, cl = classNames.length; c < cl; c++) {
  86. if (className.indexOf(" " + classNames[c] + " ") < 0) {
  87. setClass += " " + classNames[c];
  88. }
  89. }
  90. element.className = setClass.replace(/^\s+|\s+$/g, "");
  91. }
  92. }
  93. }
  94. return element;
  95. };
  96. var _removeClass = function(element, value) {
  97. if (element.removeClass) {
  98. element.removeClass(value);
  99. return element;
  100. }
  101. if (value && typeof value === "string" || value === undefined) {
  102. var classNames = (value || "").split(/\s+/);
  103. if (element.nodeType === 1 && element.className) {
  104. if (value) {
  105. var className = (" " + element.className + " ").replace(/[\n\t]/g, " ");
  106. for (var c = 0, cl = classNames.length; c < cl; c++) {
  107. className = className.replace(" " + classNames[c] + " ", " ");
  108. }
  109. element.className = className.replace(/^\s+|\s+$/g, "");
  110. } else {
  111. element.className = "";
  112. }
  113. }
  114. }
  115. return element;
  116. };
  117. var _getZoomFactor = function() {
  118. var rect, physicalWidth, logicalWidth, zoomFactor = 1;
  119. if (typeof document.body.getBoundingClientRect === "function") {
  120. rect = document.body.getBoundingClientRect();
  121. physicalWidth = rect.right - rect.left;
  122. logicalWidth = document.body.offsetWidth;
  123. zoomFactor = Math.round(physicalWidth / logicalWidth * 100) / 100;
  124. }
  125. return zoomFactor;
  126. };
  127. var _getDOMObjectPosition = function(obj) {
  128. var info = {
  129. left: 0,
  130. top: 0,
  131. width: 0,
  132. height: 0,
  133. zIndex: 999999999
  134. };
  135. var zi = _getStyle(obj, "z-index");
  136. if (zi && zi !== "auto") {
  137. info.zIndex = parseInt(zi, 10);
  138. }
  139. if (obj.getBoundingClientRect) {
  140. var rect = obj.getBoundingClientRect();
  141. var pageXOffset, pageYOffset, zoomFactor;
  142. if ("pageXOffset" in window && "pageYOffset" in window) {
  143. pageXOffset = window.pageXOffset;
  144. pageYOffset = window.pageYOffset;
  145. } else {
  146. zoomFactor = _getZoomFactor();
  147. pageXOffset = Math.round(document.documentElement.scrollLeft / zoomFactor);
  148. pageYOffset = Math.round(document.documentElement.scrollTop / zoomFactor);
  149. }
  150. var leftBorderWidth = document.documentElement.clientLeft || 0;
  151. var topBorderWidth = document.documentElement.clientTop || 0;
  152. info.left = rect.left + pageXOffset - leftBorderWidth;
  153. info.top = rect.top + pageYOffset - topBorderWidth;
  154. info.width = "width" in rect ? rect.width : rect.right - rect.left;
  155. info.height = "height" in rect ? rect.height : rect.bottom - rect.top;
  156. }
  157. return info;
  158. };
  159. var _noCache = function(path, options) {
  160. var useNoCache = !(options && options.useNoCache === false);
  161. if (useNoCache) {
  162. return (path.indexOf("?") === -1 ? "?" : "&") + "nocache=" + new Date().getTime();
  163. } else {
  164. return "";
  165. }
  166. };
  167. var _vars = function(options) {
  168. var str = [];
  169. var origins = [];
  170. if (options.trustedOrigins) {
  171. if (typeof options.trustedOrigins === "string") {
  172. origins.push(options.trustedOrigins);
  173. } else if (typeof options.trustedOrigins === "object" && "length" in options.trustedOrigins) {
  174. origins = origins.concat(options.trustedOrigins);
  175. }
  176. }
  177. if (options.trustedDomains) {
  178. if (typeof options.trustedDomains === "string") {
  179. origins.push(options.trustedDomains);
  180. } else if (typeof options.trustedDomains === "object" && "length" in options.trustedDomains) {
  181. origins = origins.concat(options.trustedDomains);
  182. }
  183. }
  184. if (origins.length) {
  185. str.push("trustedOrigins=" + encodeURIComponent(origins.join(",")));
  186. }
  187. if (typeof options.amdModuleId === "string" && options.amdModuleId) {
  188. str.push("amdModuleId=" + encodeURIComponent(options.amdModuleId));
  189. }
  190. if (typeof options.cjsModuleId === "string" && options.cjsModuleId) {
  191. str.push("cjsModuleId=" + encodeURIComponent(options.cjsModuleId));
  192. }
  193. return str.join("&");
  194. };
  195. var _inArray = function(elem, array) {
  196. if (array.indexOf) {
  197. return array.indexOf(elem);
  198. }
  199. for (var i = 0, length = array.length; i < length; i++) {
  200. if (array[i] === elem) {
  201. return i;
  202. }
  203. }
  204. return -1;
  205. };
  206. var _prepGlue = function(elements) {
  207. if (typeof elements === "string") throw new TypeError("ZeroClipboard doesn't accept query strings.");
  208. if (!elements.length) return [ elements ];
  209. return elements;
  210. };
  211. var _dispatchCallback = function(func, element, instance, args, async) {
  212. if (async) {
  213. window.setTimeout(function() {
  214. func.call(element, instance, args);
  215. }, 0);
  216. } else {
  217. func.call(element, instance, args);
  218. }
  219. };
  220. var ZeroClipboard = function(elements, options) {
  221. if (elements) (ZeroClipboard.prototype._singleton || this).glue(elements);
  222. if (ZeroClipboard.prototype._singleton) return ZeroClipboard.prototype._singleton;
  223. ZeroClipboard.prototype._singleton = this;
  224. this.options = {};
  225. for (var kd in _defaults) this.options[kd] = _defaults[kd];
  226. for (var ko in options) this.options[ko] = options[ko];
  227. this.handlers = {};
  228. if (ZeroClipboard.detectFlashSupport()) _bridge();
  229. };
  230. var currentElement, gluedElements = [];
  231. ZeroClipboard.prototype.setCurrent = function(element) {
  232. currentElement = element;
  233. this.reposition();
  234. var titleAttr = element.getAttribute("title");
  235. if (titleAttr) {
  236. this.setTitle(titleAttr);
  237. }
  238. var useHandCursor = this.options.forceHandCursor === true || _getStyle(element, "cursor") === "pointer";
  239. _setHandCursor.call(this, useHandCursor);
  240. return this;
  241. };
  242. ZeroClipboard.prototype.setText = function(newText) {
  243. if (newText && newText !== "") {
  244. this.options.text = newText;
  245. if (this.ready()) this.flashBridge.setText(newText);
  246. }
  247. return this;
  248. };
  249. ZeroClipboard.prototype.setTitle = function(newTitle) {
  250. if (newTitle && newTitle !== "") this.htmlBridge.setAttribute("title", newTitle);
  251. return this;
  252. };
  253. ZeroClipboard.prototype.setSize = function(width, height) {
  254. if (this.ready()) this.flashBridge.setSize(width, height);
  255. return this;
  256. };
  257. ZeroClipboard.prototype.setHandCursor = function(enabled) {
  258. enabled = typeof enabled === "boolean" ? enabled : !!enabled;
  259. _setHandCursor.call(this, enabled);
  260. this.options.forceHandCursor = enabled;
  261. return this;
  262. };
  263. var _setHandCursor = function(enabled) {
  264. if (this.ready()) this.flashBridge.setHandCursor(enabled);
  265. };
  266. ZeroClipboard.version = "1.2.3";
  267. var _defaults = {
  268. moviePath: "ZeroClipboard.swf",
  269. trustedOrigins: null,
  270. text: null,
  271. hoverClass: "zeroclipboard-is-hover",
  272. activeClass: "zeroclipboard-is-active",
  273. allowScriptAccess: "sameDomain",
  274. useNoCache: true,
  275. forceHandCursor: false
  276. };
  277. ZeroClipboard.setDefaults = function(options) {
  278. for (var ko in options) _defaults[ko] = options[ko];
  279. };
  280. ZeroClipboard.destroy = function() {
  281. ZeroClipboard.prototype._singleton.unglue(gluedElements);
  282. var bridge = ZeroClipboard.prototype._singleton.htmlBridge;
  283. bridge.parentNode.removeChild(bridge);
  284. delete ZeroClipboard.prototype._singleton;
  285. };
  286. ZeroClipboard.detectFlashSupport = function() {
  287. var hasFlash = false;
  288. if (typeof ActiveXObject === "function") {
  289. try {
  290. if (new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) {
  291. hasFlash = true;
  292. }
  293. } catch (error) {}
  294. }
  295. if (!hasFlash && navigator.mimeTypes["application/x-shockwave-flash"]) {
  296. hasFlash = true;
  297. }
  298. return hasFlash;
  299. };
  300. var _amdModuleId = null;
  301. var _cjsModuleId = null;
  302. var _bridge = function() {
  303. var flashBridge, len;
  304. var client = ZeroClipboard.prototype._singleton;
  305. var container = document.getElementById("global-zeroclipboard-html-bridge");
  306. if (!container) {
  307. var opts = {};
  308. for (var ko in client.options) opts[ko] = client.options[ko];
  309. opts.amdModuleId = _amdModuleId;
  310. opts.cjsModuleId = _cjsModuleId;
  311. var flashvars = _vars(opts);
  312. var html = ' <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="global-zeroclipboard-flash-bridge" width="100%" height="100%"> <param name="movie" value="' + client.options.moviePath + _noCache(client.options.moviePath, client.options) + '"/> <param name="allowScriptAccess" value="' + client.options.allowScriptAccess + '"/> <param name="scale" value="exactfit"/> <param name="loop" value="false"/> <param name="menu" value="false"/> <param name="quality" value="best" /> <param name="bgcolor" value="#ffffff"/> <param name="wmode" value="transparent"/> <param name="flashvars" value="' + flashvars + '"/> <embed src="' + client.options.moviePath + _noCache(client.options.moviePath, client.options) + '" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="100%" height="100%" name="global-zeroclipboard-flash-bridge" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="' + flashvars + '" scale="exactfit"> </embed> </object>';
  313. container = document.createElement("div");
  314. container.id = "global-zeroclipboard-html-bridge";
  315. container.setAttribute("class", "global-zeroclipboard-container");
  316. container.setAttribute("data-clipboard-ready", false);
  317. container.style.position = "absolute";
  318. container.style.left = "-9999px";
  319. container.style.top = "-9999px";
  320. container.style.width = "15px";
  321. container.style.height = "15px";
  322. container.style.zIndex = "9999";
  323. container.innerHTML = html;
  324. document.body.appendChild(container);
  325. }
  326. client.htmlBridge = container;
  327. flashBridge = document["global-zeroclipboard-flash-bridge"];
  328. if (flashBridge && (len = flashBridge.length)) {
  329. flashBridge = flashBridge[len - 1];
  330. }
  331. client.flashBridge = flashBridge || container.children[0].lastElementChild;
  332. };
  333. ZeroClipboard.prototype.resetBridge = function() {
  334. this.htmlBridge.style.left = "-9999px";
  335. this.htmlBridge.style.top = "-9999px";
  336. this.htmlBridge.removeAttribute("title");
  337. this.htmlBridge.removeAttribute("data-clipboard-text");
  338. _removeClass(currentElement, this.options.activeClass);
  339. currentElement = null;
  340. this.options.text = null;
  341. return this;
  342. };
  343. ZeroClipboard.prototype.ready = function() {
  344. var ready = this.htmlBridge.getAttribute("data-clipboard-ready");
  345. return ready === "true" || ready === true;
  346. };
  347. ZeroClipboard.prototype.reposition = function() {
  348. if (!currentElement) return false;
  349. var pos = _getDOMObjectPosition(currentElement);
  350. this.htmlBridge.style.top = pos.top + "px";
  351. this.htmlBridge.style.left = pos.left + "px";
  352. this.htmlBridge.style.width = pos.width + "px";
  353. this.htmlBridge.style.height = pos.height + "px";
  354. this.htmlBridge.style.zIndex = pos.zIndex + 1;
  355. this.setSize(pos.width, pos.height);
  356. return this;
  357. };
  358. ZeroClipboard.dispatch = function(eventName, args) {
  359. ZeroClipboard.prototype._singleton.receiveEvent(eventName, args);
  360. };
  361. ZeroClipboard.prototype.on = function(eventName, func) {
  362. var events = eventName.toString().split(/\s/g);
  363. for (var i = 0; i < events.length; i++) {
  364. eventName = events[i].toLowerCase().replace(/^on/, "");
  365. if (!this.handlers[eventName]) this.handlers[eventName] = func;
  366. }
  367. if (this.handlers.noflash && !ZeroClipboard.detectFlashSupport()) {
  368. this.receiveEvent("onNoFlash", null);
  369. }
  370. return this;
  371. };
  372. ZeroClipboard.prototype.addEventListener = ZeroClipboard.prototype.on;
  373. ZeroClipboard.prototype.off = function(eventName, func) {
  374. var events = eventName.toString().split(/\s/g);
  375. for (var i = 0; i < events.length; i++) {
  376. eventName = events[i].toLowerCase().replace(/^on/, "");
  377. for (var event in this.handlers) {
  378. if (event === eventName && this.handlers[event] === func) {
  379. delete this.handlers[event];
  380. }
  381. }
  382. }
  383. return this;
  384. };
  385. ZeroClipboard.prototype.removeEventListener = ZeroClipboard.prototype.off;
  386. ZeroClipboard.prototype.receiveEvent = function(eventName, args) {
  387. eventName = eventName.toString().toLowerCase().replace(/^on/, "");
  388. var element = currentElement;
  389. var performCallbackAsync = true;
  390. switch (eventName) {
  391. case "load":
  392. if (args && parseFloat(args.flashVersion.replace(",", ".").replace(/[^0-9\.]/gi, "")) < 10) {
  393. this.receiveEvent("onWrongFlash", {
  394. flashVersion: args.flashVersion
  395. });
  396. return;
  397. }
  398. this.htmlBridge.setAttribute("data-clipboard-ready", true);
  399. break;
  400. case "mouseover":
  401. _addClass(element, this.options.hoverClass);
  402. break;
  403. case "mouseout":
  404. _removeClass(element, this.options.hoverClass);
  405. this.resetBridge();
  406. break;
  407. case "mousedown":
  408. _addClass(element, this.options.activeClass);
  409. break;
  410. case "mouseup":
  411. _removeClass(element, this.options.activeClass);
  412. break;
  413. case "datarequested":
  414. var targetId = element.getAttribute("data-clipboard-target"), targetEl = !targetId ? null : document.getElementById(targetId);
  415. if (targetEl) {
  416. var textContent = targetEl.value || targetEl.textContent || targetEl.innerText;
  417. if (textContent) this.setText(textContent);
  418. } else {
  419. var defaultText = element.getAttribute("data-clipboard-text");
  420. if (defaultText) this.setText(defaultText);
  421. }
  422. performCallbackAsync = false;
  423. break;
  424. case "complete":
  425. this.options.text = null;
  426. break;
  427. }
  428. if (this.handlers[eventName]) {
  429. var func = this.handlers[eventName];
  430. if (typeof func === "string" && typeof window[func] === "function") {
  431. func = window[func];
  432. }
  433. if (typeof func === "function") {
  434. _dispatchCallback(func, element, this, args, performCallbackAsync);
  435. }
  436. }
  437. };
  438. ZeroClipboard.prototype.glue = function(elements) {
  439. elements = _prepGlue(elements);
  440. for (var i = 0; i < elements.length; i++) {
  441. if (_inArray(elements[i], gluedElements) == -1) {
  442. gluedElements.push(elements[i]);
  443. _addEventHandler(elements[i], "mouseover", _elementMouseOver);
  444. }
  445. }
  446. return this;
  447. };
  448. ZeroClipboard.prototype.unglue = function(elements) {
  449. elements = _prepGlue(elements);
  450. for (var i = 0; i < elements.length; i++) {
  451. _removeEventHandler(elements[i], "mouseover", _elementMouseOver);
  452. var arrayIndex = _inArray(elements[i], gluedElements);
  453. if (arrayIndex != -1) gluedElements.splice(arrayIndex, 1);
  454. }
  455. return this;
  456. };
  457. if (typeof define === "function" && define.amd) {
  458. define([ "require", "exports", "module" ], function(require, exports, module) {
  459. _amdModuleId = module && module.id || null;
  460. return ZeroClipboard;
  461. });
  462. } else if (typeof module === "object" && module && typeof module.exports === "object" && module.exports) {
  463. _cjsModuleId = module.id || null;
  464. module.exports = ZeroClipboard;
  465. } else {
  466. window.ZeroClipboard = ZeroClipboard;
  467. }
  468. })();