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

/files/zeroclipboard/1.2.0b3/ZeroClipboard.js

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