From 00dddfeaf3ef16c19a0f82091203edb31121a60e Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Wed, 4 Feb 2015 00:46:55 +0100 Subject: [PATCH] Updated zero.clipboard to fix copy to clipboard function for requirejs. Removed wrapper function. --- src/UI/Content/zero.clipboard.swf | Bin 1923 -> 6580 bytes src/UI/JsLibraries/zero.clipboard.js | 3087 ++++++++++++++++++------ src/UI/Mixins/CopyToClipboard.js | 29 +- src/UI/Settings/General/GeneralView.js | 234 +- 4 files changed, 2461 insertions(+), 889 deletions(-) diff --git a/src/UI/Content/zero.clipboard.swf b/src/UI/Content/zero.clipboard.swf index ed962a56d1173f1cef99d0c2a61e21f9aabae46e..8bad6a3e34f1b0b055da3ee8e506fe627cf75987 100644 GIT binary patch literal 6580 zcmV;l8B69vS5pqYF8}~|+O;|hc$3$4_xw*umW3pYG4CHRmTj(WK^{#o5R7es9YDkn zLI7j^mVQ|RS#tDCek9Nm66e+C)g+`1B~@Q(+R&s)wsqasbxqrKYqw64hv~YlTf24b z-dp$X9_-xz|0UVRr0w^$##i^=bI(2J+;h%7=Uka7B7Bb!*UN;|0qOFrB80qW`8R-& zo#{w)M_*S!nHozZ^&L3d<%?x9<2%~gCMPFbCvR#^rAOPg-G2M+ZT{_T+qbtMMvFe3 z%&1c>Nqw_#w?Uvw)5GcbcqX1oDr~Nfq_UY^KA%N35-t-R&!!Uw$w;_OOK4+SGNZR` zYu$!~BH{iFe6Y;Rh$hA$i=&@Az^rSkWwL}xD9^2VgL1cR}@k~P7-4RKR zXi6ZVO({1k9hLnCwu!=!BNah+&pW2dcDII8V{PN!wIJ~HIrJ1QG&7cgqj@9s-xQO?nA~}d2Yz4X0+X1T3FeBJ5{#( zx7}=t!Js?a7HQjppaFN2F82@loureycJ10R2agt(unE#fjuGSMt3Pgdp%*RwO~)}U zo$5@)$463XIzql!bGC<p-c3I;L$SZqiB^5t(qr`gxNh!bB<_k@u!j2~AB(!I4v1 zI8$vHq%{)P#}n$b)IXk%XEdh?)Fzl|9S2icUF&9JsXvpBCr538Y%XBvK5Y7BuASOeHH4yVXdO`u_N6tc-<3JJm#DL=B&I zrM0otgjSjgVH^m$mWVkhr~dd#iut z;lAL}p6;&U&W-~I_I7mMIefUUJJ55qYIqoZdMd6(hKFl<`-i(a20Cyl*xB9RAM6_r z9C}EIS{6|dF|6r&7oM<@9g`}$al*qUJe3Y4o0R(%B_358Gt=W*DylFvW!J7<$|e(a zlhV*&09zAUax@cDb}N2kk7A)-Ptsu3)UGIJ6-7(v+S24xhGyS^Jcb0NaY9Wiab)6e z$8P6*OayG*YN4949FGqn=HfFWbg5&vvt4I@HW`(TyOd-$kytDsvK&|9Ndu=W5hG}b z8&mb*WDBe%7q_emf38v$t@ua@d%7~S0*wKGe+jhVI*0zysJduefYZe6j9cI9< zUkL0ndovPtrm#|oqc9s5BK0IO#;a)c3C%=Y^A1GT&d(@2j5D%zH50~sE3#r?x=Npn zmSS&hW67Z!jV%aA2Xc57gl-4kgnAvICQa+AujPKti2XA_xrha+ZQYDO6Yf)y-q*190&8Ii+RlGyi7sI4qR zEy8CfTWhjrv32p5c87qM-Es2zsP$6{@ly#+}WmD%njk|Grp*?qY6nZ%RI&%Xvrqk2{oQj zM-rOLghaH_v>MS^>2w536f}u@!t7hd;`$i7Bo0)qwYDsrN=DXa0YxFT=qRbdp ze7>gEOzJ>tQcHKLx`s+IZx&N1Mk?Jn*=@=KJ+Yj>B-sgOD3h+~Xf`Y1J0!Df`UzK2 zFW<#grMp;BiRHU+$Hvh_YD7)6+=o}HT#=javX+r_JTj_DX6*q~#0VbG2$*6tM?Xt% z9f|m8(l!u0Jba)#Fd(XI>M~aPvGCOnq8{ztM>{$Ph66nVcD)318Yp+6Gh+4<_ICFV z$oHBP8+!hDEFRYFBO2zfKs=Ff7>V@mcq9|6GKTv#ti3a~NLrojLoY#PKY{!U(4uo~V__V-2>}nKK6dTEUHq0ssft4GQaV2$ug-3ff8PTGc-6E2j$lR$- zJ1}RC;YGor%$^D+D49v85)2g1q*-s%WAUho&fXoJsVqK@B8VPO*jW;1b~#!e-6I+2 zI4TdDy*a(dn%?7DST4eF)jXHt(T<3i%3^SF91ITjcMl&79_hA*IO?-o!@&Uy*cH6{ zkj)$p9u_+f^mN|2Y*EIoUXYwU*QN5>r7Hl*ynvD!S-sbnhu-T8Ia?_VOR*FyRH{ef zkvJNO1rar2!)k#=!pO3y^|+5x&;BN6ts&9y!eXtG8e5NT*f2Er}E}=FdY@wDfk;bo8D`|75iEZqrLm`~d)U>-i4;WvM>s15YN3og; z_8sgPkWm(cqeV@KWyTU!`v(plun_okCR#no9`u6)0Vaa(#!S3_cpeRLgn_ZN(RHjn zWTO=7>0)1Agilvv@a<=V0W1bI$^3%e*{P1JBXLa4aZTq^x@g!{#4MSelKQk!e135! zPQWOt0V{^Ew)W)lj+SF;%Y8#u?{B$%c*lvB6Rktd^{7%kBge7knhHi?^SUjF6Hr^C zh%lV%xvkxw>a{@9H~OA6R`*B&(Oz{x;P958%jC`#4woK29V!ZjhWJNYyT3 zncL%CUL)I9faH>EL6RM-aAaRCuajWCt7^k9fef4sybOZONiuJf!7hVChAJ7HvREyP zE?HV8i*8x+$f8#km&;;}EUu7IK5?Zi*2&^3SzImK*2p%mY+EbCIvLi>ut5ezhK(|8 zl3}y#sFxjH8Meq$gA6{|)+F1u%C;8S)+XDw$+qpX?IziAv+TG z!NWa@?J*obj{SSFy${DvVE-hxr?5SZ?HO$E$M!6?=deAG?EYuNt|w%-N&ml*u(V1FAp`Q}J)Dg*x>p6mCK;U8iDZAAZL?EeJY-(vea zY=4hy|B3Mb!uDfqUjVu8f3f`^*q8CL^Fhx35GTkm!{KQJJdI#~1n1JDoGd-Y$!p%r z$!niLHcw)E3dc`l{|vVGbN1)hzUMjn1=hcSun*v#4`M$m!;9Em#CD03ogcpsib-{3@h1K(Qu0}lK94ghBiIKuuo#G1f> zA|V_v@x%sBr!fOTC~sqb0+P+Y4w)YoG0ylWnF!Y3i3AQ^pKt&Y!;px;MrBeQ@I2wn zPvDTd@xk*pNiqrA03VoO^*m4Ji;f=#8pl3(sj@}{JN_f!bPysromGT5oz7}PTu$dQ zBR%wtAcnQM^?T+3N+O0tXqI-fJ94IWdG9av9`1dtIPbGp1{a$Bn0?Q94RvgAiQ7xxb5ZTv<17C2p>KjSypd z*M(*ex_VOt#|=1k;n-X-AeS$Ax0QCDrl-u6^UWS~)F$OTO&Cb!`5@0oCk)a&600(a zacqi|wwEZ4(IgGh6a!aijWf&&DS?sNh&vE01XnV((tG#t!S!68rY*+CQT7p`8?Ofm z5WFY*2;P%+f_GUIc)Q~n$orL{LOGEG=A z)ntxN<>+*d-j}2I=jfRnJ)5KNHq7&$96bk?aRLg=%oV*W2nlToZJ`f9F&DTM0yn`q z-i>EM8BmJCe!Q_Z!S1-sBtrnUIc4T5s^dJ+eMbn@eDstuC@_fw9`vwgOwgohkg)A@ z#pqt^3^;n8J_wh+%L&2p%+=7A*}yH}#mG~bRreO7d&-D=OhhNH;DkN|f&HNFDMmZ# z!*IFTBN<4Y4VR6*4QgjG+QoLPmW&NuY8Mk)7CE~k$gWhVC)%stiT#1-LG=*!!RTT2 zt|6gUK)<|Fi1xKWuph9mC(WUEMF+4A29F4ZHc;cLog5$HvjjUJ;Pf+D?yqvM9h;qA5aNM~2xUsP&P zU5wIPv7nv^4Y7TOxxF64zWJ!57*IF_yB%dfa3eR1!eAj`cf4U5=BhFi77RKC?{kFE z9I)Sy0R1S?j{*HS&`%h@F9ZD~&{vG#PZ__T2Kp+{%RoN^gLWiTPr~Bt`LN_}Lor9B z5pg|IE{q6)lVF($C(&W$)eBw>;l)`tF(B&jc1iSn{n|B~WD|?@b<}Anppu+=qDbEW zbtGSmo+z5fuj0`m6)g zSS&EhY+huU6nHUS;t1)XB`ku>8gr&Km}-{s9M9VkBQ%c72ymAH_nH7t6$m0KFHgS& z3Vj=V^vl@3g6*r|8x&)8M#fsjGM1O;MaK(J%052KD`^&NRXrcA3#Fr(a$r=mCS;=F z^7&FYtBYnYTsW^zhSCkz2Ko)ac!a5K%3X_Y?l1U{k>JJl*isI+8EO|;?lW@PHy3H`Pl0~NC>+e`pRuuEj{h7`#oq<`J)nO9^e=&a zALw5J{Q=M)0@LYV1N|GO*5#&Jg9>17JP%k!k_WIXl*xlYc$qwe<)K7YRr7*)kC7e? zY460mmSXcGp#K8&UxEG`D%5t3xfb zXvXtbStws4ih}s8k^NZ!77E^n&^qj#%URS{@}-?vvbp07R2RL^6T(0b2EMxiL&9w6 zVJNcuA9w`NbXP}&ddL-{kAf4=GE4s(a#!CLMg#^~k-v6r=rI_29OyMB^EoazL}&4c zIr;#X>wO%6p67y(0Sne^F{bkFynpDuz=A4&6xPL@2w#U_0SizZu1E3hk&@4q+${7y zv!Z+gf=>b)a?z)tL45+;x3Zfv%Wh6su&$1==)*;0_%wuE8kYe25Jw;80^9lEGk{l1 z*az;W7TX!_{lH47XVFV|vVk%G5?}H7=YV-|S&HW&dI5qj0I`Izg0{U>IX(ds-s46M z$V4#FLmzX z$QW*NBExNYKOHSZv2=TpD@3t;yNDf@a3A6dfsX^JJV0LJiqt(jDB;G<0w;*|m@`73 zfauF;sd_Q+N#L;hq95fhUx;mDnY$3&%;H=Ly&-!pnmAvKfSY zRyA21Ba3Nb3bU*}WG+@jW6%23S72WK5pop*e()j>HrfUgVF9`0 ziwjHmrV(L+k-(K=u$42V@|y6{li(?K4KI|j+PESfx}g-8K4rxvZe7fUEPR!?)F?{4 z@Cu%&p0HPoQLn9bvKLPwa5JmX9;;MZHp3{NHr%{{6_wE|6BGRg#14X#L6Ph4w-iV literal 1923 zcmV-}2YmQLS5pqQ3jhFk+I?1AbJN-t-dmQmCHWHDNlXYg1A$ynVmsk*4k6?ad?A4Z z(>Q@s%EjCAmaT&#xsseqFH`2s^e6P4zVxYY{Q>;}o#`lH+P?Rp)BcIlCD}Of>1jRj z(psOswf4G6@gd^>j!@!v2u)yAN}Wdt{c7}27@-xXsV{GrD%rz5({h)=xFZ-o7x~~r|Xzu$flNKCfj3{=jI9v1qjsCmUY|Nquwg*bWB5|LAc!EyxX?5cL#Kz z&Fdz0+bg+KV&LN$p2=3%ns$R_D<(V4-p;O_`iJZuB_KCX6J0%1CJnq7GFeg5AT)gMmG~A9!kHlKX zF+3(Vx}IlSk_N0DcIYYJx?3;?lWjDWy&yQaYwzSxt?n~N>S<=e+_5k2aR2@PC3_JiCg^;TD$q ztziz)JjaAICAV$pLB7J8 zJ5qZ49-I`1tv%gSspZg4+t6IO!60PCFij=Q^@Y*&+R;#5XGW{-Nlk|yY(xD}LZ?k7 z+H4lvhS?N~n;XR^Nif4!Y#NXtbXeJ{?$*k0tIx~Za$nuu>4T-}i>Fdg-F=qmxAPDR zw5XXX_myxaI_ya_I0-6hQ0*Nbv_-7-wgeur!FEcpX&n=q#CyTB2=6Ys4O`8EZV3lL zEn+(?gNKfrBj1}^d}zR_|5}I44{uRAgEM?anUDn}O=YG9bPi{3P0Ew%|L=5iStnBEeUQc!Q7`f@cYyljktO1-SrnQC`Fs;JQTcZG!I*{1u7Z zC3uv3=Brp<3Fd|8eq!dPS8Y5*Cqv#k$F~pCH z7@x-(k;CX*DuO3cJcr|$z|Q(Ws+MDvV)v<{CxJ;8n520U(he^;`iL&j?-y`J|63r=w*;sXcpvE zdTk16!pV1fk&Zio%xGPr$g@FxvkzfP|&ibCoipFT;VBz%O}5g{TgA2`1!p2yWMaHvh8 zx%v(L4t&3?e#HZn!}RX!Wl*Zi0izz#@A-PHy2AC0`@S9@7^}XX7#Lss`pCd|;Oj{+ z>TBw_ucxaI18vOLQ#~!?>!YCg>T^i8GJ&dN988BNoUF|G`FhdUm1+t5>W_n3)R!Ou zfr@s4%mLX~aa#cofd`MIpZC>dz^qK*UL9e=@|C1~Ep5 zaG3*~0Dc^%paiB2rY;}TB>~^(Xnd1b^S4OW~(YfSMi+es&H29D4xB Je*oi0MM?K&#aaLW diff --git a/src/UI/JsLibraries/zero.clipboard.js b/src/UI/JsLibraries/zero.clipboard.js index 7706edcf5..dd44ac46a 100644 --- a/src/UI/JsLibraries/zero.clipboard.js +++ b/src/UI/JsLibraries/zero.clipboard.js @@ -1,349 +1,1433 @@ /*! -* ZeroClipboard -* The ZeroClipboard library provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie and a JavaScript interface. -* Copyright (c) 2014 Jon Rohan, James M. Greene -* Licensed MIT -* http://zeroclipboard.org/ -* v1.3.2 -*/ -(function() { + * ZeroClipboard + * The ZeroClipboard library provides an easy way to copy text to the clipboard using an invisible Adobe Flash movie and a JavaScript interface. + * Copyright (c) 2009-2014 Jon Rohan, James M. Greene + * Licensed MIT + * http://zeroclipboard.org/ + * v2.2.0 + */ +(function(window, undefined) { "use strict"; - var currentElement; - var flashState = { - bridge: null, - version: "0.0.0", - disabled: null, - outdated: null, - ready: null + /** + * Store references to critically important global functions that may be + * overridden on certain web pages. + */ + var _window = window, _document = _window.document, _navigator = _window.navigator, _setTimeout = _window.setTimeout, _clearTimeout = _window.clearTimeout, _setInterval = _window.setInterval, _clearInterval = _window.clearInterval, _getComputedStyle = _window.getComputedStyle, _encodeURIComponent = _window.encodeURIComponent, _ActiveXObject = _window.ActiveXObject, _Error = _window.Error, _parseInt = _window.Number.parseInt || _window.parseInt, _parseFloat = _window.Number.parseFloat || _window.parseFloat, _isNaN = _window.Number.isNaN || _window.isNaN, _now = _window.Date.now, _keys = _window.Object.keys, _defineProperty = _window.Object.defineProperty, _hasOwn = _window.Object.prototype.hasOwnProperty, _slice = _window.Array.prototype.slice, _unwrap = function() { + var unwrapper = function(el) { + return el; + }; + if (typeof _window.wrap === "function" && typeof _window.unwrap === "function") { + try { + var div = _document.createElement("div"); + var unwrappedDiv = _window.unwrap(div); + if (div.nodeType === 1 && unwrappedDiv && unwrappedDiv.nodeType === 1) { + unwrapper = _window.unwrap; + } + } catch (e) {} + } + return unwrapper; + }(); + /** + * Convert an `arguments` object into an Array. + * + * @returns The arguments as an Array + * @private + */ + var _args = function(argumentsObj) { + return _slice.call(argumentsObj, 0); }; - var _clipData = {}; - var clientIdCounter = 0; - var _clientMeta = {}; - var elementIdCounter = 0; - var _elementMeta = {}; - var _amdModuleId = null; - var _cjsModuleId = null; - var _swfPath = function() { - var i, jsDir, tmpJsPath, jsPath, swfPath = "ZeroClipboard.swf"; - if (document.currentScript && (jsPath = document.currentScript.src)) {} else { - var scripts = document.getElementsByTagName("script"); - if ("readyState" in scripts[0]) { - for (i = scripts.length; i--; ) { - if (scripts[i].readyState === "interactive" && (jsPath = scripts[i].src)) { - break; + /** + * Shallow-copy the owned, enumerable properties of one object over to another, similar to jQuery's `$.extend`. + * + * @returns The target object, augmented + * @private + */ + var _extend = function() { + var i, len, arg, prop, src, copy, args = _args(arguments), target = args[0] || {}; + for (i = 1, len = args.length; i < len; i++) { + if ((arg = args[i]) != null) { + for (prop in arg) { + if (_hasOwn.call(arg, prop)) { + src = target[prop]; + copy = arg[prop]; + if (target !== copy && copy !== undefined) { + target[prop] = copy; + } } } - } else if (document.readyState === "loading") { - jsPath = scripts[scripts.length - 1].src; - } else { - for (i = scripts.length; i--; ) { - tmpJsPath = scripts[i].src; - if (!tmpJsPath) { - jsDir = null; - break; - } - tmpJsPath = tmpJsPath.split("#")[0].split("?")[0]; - tmpJsPath = tmpJsPath.slice(0, tmpJsPath.lastIndexOf("/") + 1); - if (jsDir == null) { - jsDir = tmpJsPath; - } else if (jsDir !== tmpJsPath) { - jsDir = null; - break; - } + } + } + return target; + }; + /** + * Return a deep copy of the source object or array. + * + * @returns Object or Array + * @private + */ + var _deepCopy = function(source) { + var copy, i, len, prop; + if (typeof source !== "object" || source == null || typeof source.nodeType === "number") { + copy = source; + } else if (typeof source.length === "number") { + copy = []; + for (i = 0, len = source.length; i < len; i++) { + if (_hasOwn.call(source, i)) { + copy[i] = _deepCopy(source[i]); } - if (jsDir !== null) { - jsPath = jsDir; + } + } else { + copy = {}; + for (prop in source) { + if (_hasOwn.call(source, prop)) { + copy[prop] = _deepCopy(source[prop]); } } } - if (jsPath) { - jsPath = jsPath.split("#")[0].split("?")[0]; - swfPath = jsPath.slice(0, jsPath.lastIndexOf("/") + 1) + swfPath; + return copy; + }; + /** + * Makes a shallow copy of `obj` (like `_extend`) but filters its properties based on a list of `keys` to keep. + * The inverse of `_omit`, mostly. The big difference is that these properties do NOT need to be enumerable to + * be kept. + * + * @returns A new filtered object. + * @private + */ + var _pick = function(obj, keys) { + var newObj = {}; + for (var i = 0, len = keys.length; i < len; i++) { + if (keys[i] in obj) { + newObj[keys[i]] = obj[keys[i]]; + } } - return swfPath; - }(); - var _camelizeCssPropName = function() { - var matcherRegex = /\-([a-z])/g, replacerFn = function(match, group) { - return group.toUpperCase(); - }; - return function(prop) { - return prop.replace(matcherRegex, replacerFn); - }; - }(); - var _getStyle = function(el, prop) { - var value, camelProp, tagName, possiblePointers, i, len; - if (window.getComputedStyle) { - value = window.getComputedStyle(el, null).getPropertyValue(prop); - } else { - camelProp = _camelizeCssPropName(prop); - if (el.currentStyle) { - value = el.currentStyle[camelProp]; - } else { - value = el.style[camelProp]; + return newObj; + }; + /** + * Makes a shallow copy of `obj` (like `_extend`) but filters its properties based on a list of `keys` to omit. + * The inverse of `_pick`. + * + * @returns A new filtered object. + * @private + */ + var _omit = function(obj, keys) { + var newObj = {}; + for (var prop in obj) { + if (keys.indexOf(prop) === -1) { + newObj[prop] = obj[prop]; } } - if (prop === "cursor") { - if (!value || value === "auto") { - tagName = el.tagName.toLowerCase(); - if (tagName === "a") { - return "pointer"; + return newObj; + }; + /** + * Remove all owned, enumerable properties from an object. + * + * @returns The original object without its owned, enumerable properties. + * @private + */ + var _deleteOwnProperties = function(obj) { + if (obj) { + for (var prop in obj) { + if (_hasOwn.call(obj, prop)) { + delete obj[prop]; } } } - return value; + return obj; }; - var _elementMouseOver = function(event) { - if (!event) { - event = window.event; + /** + * Determine if an element is contained within another element. + * + * @returns Boolean + * @private + */ + var _containedBy = function(el, ancestorEl) { + if (el && el.nodeType === 1 && el.ownerDocument && ancestorEl && (ancestorEl.nodeType === 1 && ancestorEl.ownerDocument && ancestorEl.ownerDocument === el.ownerDocument || ancestorEl.nodeType === 9 && !ancestorEl.ownerDocument && ancestorEl === el.ownerDocument)) { + do { + if (el === ancestorEl) { + return true; + } + el = el.parentNode; + } while (el); } - var target; - if (this !== window) { - target = this; - } else if (event.target) { - target = event.target; - } else if (event.srcElement) { - target = event.srcElement; + return false; + }; + /** + * Get the URL path's parent directory. + * + * @returns String or `undefined` + * @private + */ + var _getDirPathOfUrl = function(url) { + var dir; + if (typeof url === "string" && url) { + dir = url.split("#")[0].split("?")[0]; + dir = url.slice(0, url.lastIndexOf("/") + 1); + } + return dir; + }; + /** + * Get the current script's URL by throwing an `Error` and analyzing it. + * + * @returns String or `undefined` + * @private + */ + var _getCurrentScriptUrlFromErrorStack = function(stack) { + var url, matches; + if (typeof stack === "string" && stack) { + matches = stack.match(/^(?:|[^:@]*@|.+\)@(?=http[s]?|file)|.+?\s+(?: at |@)(?:[^:\(]+ )*[\(]?)((?:http[s]?|file):\/\/[\/]?.+?\/[^:\)]*?)(?::\d+)(?::\d+)?/); + if (matches && matches[1]) { + url = matches[1]; + } else { + matches = stack.match(/\)@((?:http[s]?|file):\/\/[\/]?.+?\/[^:\)]*?)(?::\d+)(?::\d+)?/); + if (matches && matches[1]) { + url = matches[1]; + } + } } - ZeroClipboard.activate(target); + return url; }; - var _addEventHandler = function(element, method, func) { - if (!element || element.nodeType !== 1) { - return; + /** + * Get the current script's URL by throwing an `Error` and analyzing it. + * + * @returns String or `undefined` + * @private + */ + var _getCurrentScriptUrlFromError = function() { + var url, err; + try { + throw new _Error(); + } catch (e) { + err = e; + } + if (err) { + url = err.sourceURL || err.fileName || _getCurrentScriptUrlFromErrorStack(err.stack); + } + return url; + }; + /** + * Get the current script's URL. + * + * @returns String or `undefined` + * @private + */ + var _getCurrentScriptUrl = function() { + var jsPath, scripts, i; + if (_document.currentScript && (jsPath = _document.currentScript.src)) { + return jsPath; + } + scripts = _document.getElementsByTagName("script"); + if (scripts.length === 1) { + return scripts[0].src || undefined; + } + if ("readyState" in scripts[0]) { + for (i = scripts.length; i--; ) { + if (scripts[i].readyState === "interactive" && (jsPath = scripts[i].src)) { + return jsPath; + } + } + } + if (_document.readyState === "loading" && (jsPath = scripts[scripts.length - 1].src)) { + return jsPath; } - if (element.addEventListener) { - element.addEventListener(method, func, false); - } else if (element.attachEvent) { - element.attachEvent("on" + method, func); + if (jsPath = _getCurrentScriptUrlFromError()) { + return jsPath; } + return undefined; }; - var _removeEventHandler = function(element, method, func) { - if (!element || element.nodeType !== 1) { - return; + /** + * Get the unanimous parent directory of ALL script tags. + * If any script tags are either (a) inline or (b) from differing parent + * directories, this method must return `undefined`. + * + * @returns String or `undefined` + * @private + */ + var _getUnanimousScriptParentDir = function() { + var i, jsDir, jsPath, scripts = _document.getElementsByTagName("script"); + for (i = scripts.length; i--; ) { + if (!(jsPath = scripts[i].src)) { + jsDir = null; + break; + } + jsPath = _getDirPathOfUrl(jsPath); + if (jsDir == null) { + jsDir = jsPath; + } else if (jsDir !== jsPath) { + jsDir = null; + break; + } } - if (element.removeEventListener) { - element.removeEventListener(method, func, false); - } else if (element.detachEvent) { - element.detachEvent("on" + method, func); + return jsDir || undefined; + }; + /** + * Get the presumed location of the "ZeroClipboard.swf" file, based on the location + * of the executing JavaScript file (e.g. "ZeroClipboard.js", etc.). + * + * @returns String + * @private + */ + var _getDefaultSwfPath = function() { + var jsDir = _getDirPathOfUrl(_getCurrentScriptUrl()) || _getUnanimousScriptParentDir() || ""; + return jsDir + "ZeroClipboard.swf"; + }; + /** + * Keep track of if the page is framed (in an `iframe`). This can never change. + * @private + */ + var _pageIsFramed = function() { + return window.opener == null && (!!window.top && window != window.top || !!window.parent && window != window.parent); + }(); + /** + * Keep track of the state of the Flash object. + * @private + */ + var _flashState = { + bridge: null, + version: "0.0.0", + pluginType: "unknown", + disabled: null, + outdated: null, + sandboxed: null, + unavailable: null, + degraded: null, + deactivated: null, + overdue: null, + ready: null + }; + /** + * The minimum Flash Player version required to use ZeroClipboard completely. + * @readonly + * @private + */ + var _minimumFlashVersion = "11.0.0"; + /** + * The ZeroClipboard library version number, as reported by Flash, at the time the SWF was compiled. + */ + var _zcSwfVersion; + /** + * Keep track of all event listener registrations. + * @private + */ + var _handlers = {}; + /** + * Keep track of the currently activated element. + * @private + */ + var _currentElement; + /** + * Keep track of the element that was activated when a `copy` process started. + * @private + */ + var _copyTarget; + /** + * Keep track of data for the pending clipboard transaction. + * @private + */ + var _clipData = {}; + /** + * Keep track of data formats for the pending clipboard transaction. + * @private + */ + var _clipDataFormatMap = null; + /** + * Keep track of the Flash availability check timeout. + * @private + */ + var _flashCheckTimeout = 0; + /** + * Keep track of SWF network errors interval polling. + * @private + */ + var _swfFallbackCheckInterval = 0; + /** + * The `message` store for events + * @private + */ + var _eventMessages = { + ready: "Flash communication is established", + error: { + "flash-disabled": "Flash is disabled or not installed. May also be attempting to run Flash in a sandboxed iframe, which is impossible.", + "flash-outdated": "Flash is too outdated to support ZeroClipboard", + "flash-sandboxed": "Attempting to run Flash in a sandboxed iframe, which is impossible", + "flash-unavailable": "Flash is unable to communicate bidirectionally with JavaScript", + "flash-degraded": "Flash is unable to preserve data fidelity when communicating with JavaScript", + "flash-deactivated": "Flash is too outdated for your browser and/or is configured as click-to-activate.\nThis may also mean that the ZeroClipboard SWF object could not be loaded, so please check your `swfPath` configuration and/or network connectivity.\nMay also be attempting to run Flash in a sandboxed iframe, which is impossible.", + "flash-overdue": "Flash communication was established but NOT within the acceptable time limit", + "version-mismatch": "ZeroClipboard JS version number does not match ZeroClipboard SWF version number", + "clipboard-error": "At least one error was thrown while ZeroClipboard was attempting to inject your data into the clipboard", + "config-mismatch": "ZeroClipboard configuration does not match Flash's reality", + "swf-not-found": "The ZeroClipboard SWF object could not be loaded, so please check your `swfPath` configuration and/or network connectivity" } }; - var _addClass = function(element, value) { - if (!element || element.nodeType !== 1) { - return element; + /** + * The `name`s of `error` events that can only occur is Flash has at least + * been able to load the SWF successfully. + * @private + */ + var _errorsThatOnlyOccurAfterFlashLoads = [ "flash-unavailable", "flash-degraded", "flash-overdue", "version-mismatch", "config-mismatch", "clipboard-error" ]; + /** + * The `name`s of `error` events that should likely result in the `_flashState` + * variable's property values being updated. + * @private + */ + var _flashStateErrorNames = [ "flash-disabled", "flash-outdated", "flash-sandboxed", "flash-unavailable", "flash-degraded", "flash-deactivated", "flash-overdue" ]; + /** + * A RegExp to match the `name` property of `error` events related to Flash. + * @private + */ + var _flashStateErrorNameMatchingRegex = new RegExp("^flash-(" + _flashStateErrorNames.map(function(errorName) { + return errorName.replace(/^flash-/, ""); + }).join("|") + ")$"); + /** + * A RegExp to match the `name` property of `error` events related to Flash, + * which is enabled. + * @private + */ + var _flashStateEnabledErrorNameMatchingRegex = new RegExp("^flash-(" + _flashStateErrorNames.slice(1).map(function(errorName) { + return errorName.replace(/^flash-/, ""); + }).join("|") + ")$"); + /** + * ZeroClipboard configuration defaults for the Core module. + * @private + */ + var _globalConfig = { + swfPath: _getDefaultSwfPath(), + trustedDomains: window.location.host ? [ window.location.host ] : [], + cacheBust: true, + forceEnhancedClipboard: false, + flashLoadTimeout: 3e4, + autoActivate: true, + bubbleEvents: true, + containerId: "global-zeroclipboard-html-bridge", + containerClass: "global-zeroclipboard-container", + swfObjectId: "global-zeroclipboard-flash-bridge", + hoverClass: "zeroclipboard-is-hover", + activeClass: "zeroclipboard-is-active", + forceHandCursor: false, + title: null, + zIndex: 999999999 + }; + /** + * The underlying implementation of `ZeroClipboard.config`. + * @private + */ + var _config = function(options) { + if (typeof options === "object" && options !== null) { + for (var prop in options) { + if (_hasOwn.call(options, prop)) { + if (/^(?:forceHandCursor|title|zIndex|bubbleEvents)$/.test(prop)) { + _globalConfig[prop] = options[prop]; + } else if (_flashState.bridge == null) { + if (prop === "containerId" || prop === "swfObjectId") { + if (_isValidHtml4Id(options[prop])) { + _globalConfig[prop] = options[prop]; + } else { + throw new Error("The specified `" + prop + "` value is not valid as an HTML4 Element ID"); + } + } else { + _globalConfig[prop] = options[prop]; + } + } + } + } } - if (element.classList) { - if (!element.classList.contains(value)) { - element.classList.add(value); + if (typeof options === "string" && options) { + if (_hasOwn.call(_globalConfig, options)) { + return _globalConfig[options]; } - return element; + return; } - if (value && typeof value === "string") { - var classNames = (value || "").split(/\s+/); - if (element.nodeType === 1) { - if (!element.className) { - element.className = value; - } else { - var className = " " + element.className + " ", setClass = element.className; - for (var c = 0, cl = classNames.length; c < cl; c++) { - if (className.indexOf(" " + classNames[c] + " ") < 0) { - setClass += " " + classNames[c]; - } + return _deepCopy(_globalConfig); + }; + /** + * The underlying implementation of `ZeroClipboard.state`. + * @private + */ + var _state = function() { + _detectSandbox(); + return { + browser: _pick(_navigator, [ "userAgent", "platform", "appName" ]), + flash: _omit(_flashState, [ "bridge" ]), + zeroclipboard: { + version: ZeroClipboard.version, + config: ZeroClipboard.config() + } + }; + }; + /** + * The underlying implementation of `ZeroClipboard.isFlashUnusable`. + * @private + */ + var _isFlashUnusable = function() { + return !!(_flashState.disabled || _flashState.outdated || _flashState.sandboxed || _flashState.unavailable || _flashState.degraded || _flashState.deactivated); + }; + /** + * The underlying implementation of `ZeroClipboard.on`. + * @private + */ + var _on = function(eventType, listener) { + var i, len, events, added = {}; + if (typeof eventType === "string" && eventType) { + events = eventType.toLowerCase().split(/\s+/); + } else if (typeof eventType === "object" && eventType && typeof listener === "undefined") { + for (i in eventType) { + if (_hasOwn.call(eventType, i) && typeof i === "string" && i && typeof eventType[i] === "function") { + ZeroClipboard.on(i, eventType[i]); + } + } + } + if (events && events.length) { + for (i = 0, len = events.length; i < len; i++) { + eventType = events[i].replace(/^on/, ""); + added[eventType] = true; + if (!_handlers[eventType]) { + _handlers[eventType] = []; + } + _handlers[eventType].push(listener); + } + if (added.ready && _flashState.ready) { + ZeroClipboard.emit({ + type: "ready" + }); + } + if (added.error) { + for (i = 0, len = _flashStateErrorNames.length; i < len; i++) { + if (_flashState[_flashStateErrorNames[i].replace(/^flash-/, "")] === true) { + ZeroClipboard.emit({ + type: "error", + name: _flashStateErrorNames[i] + }); + break; } - element.className = setClass.replace(/^\s+|\s+$/g, ""); + } + if (_zcSwfVersion !== undefined && ZeroClipboard.version !== _zcSwfVersion) { + ZeroClipboard.emit({ + type: "error", + name: "version-mismatch", + jsVersion: ZeroClipboard.version, + swfVersion: _zcSwfVersion + }); } } } - return element; + return ZeroClipboard; }; - var _removeClass = function(element, value) { - if (!element || element.nodeType !== 1) { - return element; - } - if (element.classList) { - if (element.classList.contains(value)) { - element.classList.remove(value); - } - return element; - } - if (value && typeof value === "string" || value === undefined) { - var classNames = (value || "").split(/\s+/); - if (element.nodeType === 1 && element.className) { - if (value) { - var className = (" " + element.className + " ").replace(/[\n\t]/g, " "); - for (var c = 0, cl = classNames.length; c < cl; c++) { - className = className.replace(" " + classNames[c] + " ", " "); + /** + * The underlying implementation of `ZeroClipboard.off`. + * @private + */ + var _off = function(eventType, listener) { + var i, len, foundIndex, events, perEventHandlers; + if (arguments.length === 0) { + events = _keys(_handlers); + } else if (typeof eventType === "string" && eventType) { + events = eventType.split(/\s+/); + } else if (typeof eventType === "object" && eventType && typeof listener === "undefined") { + for (i in eventType) { + if (_hasOwn.call(eventType, i) && typeof i === "string" && i && typeof eventType[i] === "function") { + ZeroClipboard.off(i, eventType[i]); + } + } + } + if (events && events.length) { + for (i = 0, len = events.length; i < len; i++) { + eventType = events[i].toLowerCase().replace(/^on/, ""); + perEventHandlers = _handlers[eventType]; + if (perEventHandlers && perEventHandlers.length) { + if (listener) { + foundIndex = perEventHandlers.indexOf(listener); + while (foundIndex !== -1) { + perEventHandlers.splice(foundIndex, 1); + foundIndex = perEventHandlers.indexOf(listener, foundIndex); + } + } else { + perEventHandlers.length = 0; } - element.className = className.replace(/^\s+|\s+$/g, ""); - } else { - element.className = ""; } } } - return element; + return ZeroClipboard; }; - var _getZoomFactor = function() { - var rect, physicalWidth, logicalWidth, zoomFactor = 1; - if (typeof document.body.getBoundingClientRect === "function") { - rect = document.body.getBoundingClientRect(); - physicalWidth = rect.right - rect.left; - logicalWidth = document.body.offsetWidth; - zoomFactor = Math.round(physicalWidth / logicalWidth * 100) / 100; + /** + * The underlying implementation of `ZeroClipboard.handlers`. + * @private + */ + var _listeners = function(eventType) { + var copy; + if (typeof eventType === "string" && eventType) { + copy = _deepCopy(_handlers[eventType]) || null; + } else { + copy = _deepCopy(_handlers); } - return zoomFactor; + return copy; }; - var _getDOMObjectPosition = function(obj, defaultZIndex) { - var info = { - left: 0, - top: 0, - width: 0, - height: 0, - zIndex: _getSafeZIndex(defaultZIndex) - 1 - }; - if (obj.getBoundingClientRect) { - var rect = obj.getBoundingClientRect(); - var pageXOffset, pageYOffset, zoomFactor; - if ("pageXOffset" in window && "pageYOffset" in window) { - pageXOffset = window.pageXOffset; - pageYOffset = window.pageYOffset; - } else { - zoomFactor = _getZoomFactor(); - pageXOffset = Math.round(document.documentElement.scrollLeft / zoomFactor); - pageYOffset = Math.round(document.documentElement.scrollTop / zoomFactor); + /** + * The underlying implementation of `ZeroClipboard.emit`. + * @private + */ + var _emit = function(event) { + var eventCopy, returnVal, tmp; + event = _createEvent(event); + if (!event) { + return; + } + if (_preprocessEvent(event)) { + return; + } + if (event.type === "ready" && _flashState.overdue === true) { + return ZeroClipboard.emit({ + type: "error", + name: "flash-overdue" + }); + } + eventCopy = _extend({}, event); + _dispatchCallbacks.call(this, eventCopy); + if (event.type === "copy") { + tmp = _mapClipDataToFlash(_clipData); + returnVal = tmp.data; + _clipDataFormatMap = tmp.formatMap; + } + return returnVal; + }; + /** + * The underlying implementation of `ZeroClipboard.create`. + * @private + */ + var _create = function() { + var previousState = _flashState.sandboxed; + _detectSandbox(); + if (typeof _flashState.ready !== "boolean") { + _flashState.ready = false; + } + if (_flashState.sandboxed !== previousState && _flashState.sandboxed === true) { + _flashState.ready = false; + ZeroClipboard.emit({ + type: "error", + name: "flash-sandboxed" + }); + } else if (!ZeroClipboard.isFlashUnusable() && _flashState.bridge === null) { + var maxWait = _globalConfig.flashLoadTimeout; + if (typeof maxWait === "number" && maxWait >= 0) { + _flashCheckTimeout = _setTimeout(function() { + if (typeof _flashState.deactivated !== "boolean") { + _flashState.deactivated = true; + } + if (_flashState.deactivated === true) { + ZeroClipboard.emit({ + type: "error", + name: "flash-deactivated" + }); + } + }, maxWait); } - var leftBorderWidth = document.documentElement.clientLeft || 0; - var topBorderWidth = document.documentElement.clientTop || 0; - info.left = rect.left + pageXOffset - leftBorderWidth; - info.top = rect.top + pageYOffset - topBorderWidth; - info.width = "width" in rect ? rect.width : rect.right - rect.left; - info.height = "height" in rect ? rect.height : rect.bottom - rect.top; + _flashState.overdue = false; + _embedSwf(); } - return info; }; - var _cacheBust = function(path, options) { - var cacheBust = options == null || options && options.cacheBust === true && options.useNoCache === true; - if (cacheBust) { - return (path.indexOf("?") === -1 ? "?" : "&") + "noCache=" + new Date().getTime(); + /** + * The underlying implementation of `ZeroClipboard.destroy`. + * @private + */ + var _destroy = function() { + ZeroClipboard.clearData(); + ZeroClipboard.blur(); + ZeroClipboard.emit("destroy"); + _unembedSwf(); + ZeroClipboard.off(); + }; + /** + * The underlying implementation of `ZeroClipboard.setData`. + * @private + */ + var _setData = function(format, data) { + var dataObj; + if (typeof format === "object" && format && typeof data === "undefined") { + dataObj = format; + ZeroClipboard.clearData(); + } else if (typeof format === "string" && format) { + dataObj = {}; + dataObj[format] = data; } else { - return ""; + return; } - }; - var _vars = function(options) { - var i, len, domain, str = [], domains = [], trustedOriginsExpanded = []; - if (options.trustedOrigins) { - if (typeof options.trustedOrigins === "string") { - domains.push(options.trustedOrigins); - } else if (typeof options.trustedOrigins === "object" && "length" in options.trustedOrigins) { - domains = domains.concat(options.trustedOrigins); + for (var dataFormat in dataObj) { + if (typeof dataFormat === "string" && dataFormat && _hasOwn.call(dataObj, dataFormat) && typeof dataObj[dataFormat] === "string" && dataObj[dataFormat]) { + _clipData[dataFormat] = dataObj[dataFormat]; } } - if (options.trustedDomains) { - if (typeof options.trustedDomains === "string") { - domains.push(options.trustedDomains); - } else if (typeof options.trustedDomains === "object" && "length" in options.trustedDomains) { - domains = domains.concat(options.trustedDomains); + }; + /** + * The underlying implementation of `ZeroClipboard.clearData`. + * @private + */ + var _clearData = function(format) { + if (typeof format === "undefined") { + _deleteOwnProperties(_clipData); + _clipDataFormatMap = null; + } else if (typeof format === "string" && _hasOwn.call(_clipData, format)) { + delete _clipData[format]; + } + }; + /** + * The underlying implementation of `ZeroClipboard.getData`. + * @private + */ + var _getData = function(format) { + if (typeof format === "undefined") { + return _deepCopy(_clipData); + } else if (typeof format === "string" && _hasOwn.call(_clipData, format)) { + return _clipData[format]; + } + }; + /** + * The underlying implementation of `ZeroClipboard.focus`/`ZeroClipboard.activate`. + * @private + */ + var _focus = function(element) { + if (!(element && element.nodeType === 1)) { + return; + } + if (_currentElement) { + _removeClass(_currentElement, _globalConfig.activeClass); + if (_currentElement !== element) { + _removeClass(_currentElement, _globalConfig.hoverClass); } } - if (domains.length) { - for (i = 0, len = domains.length; i < len; i++) { - if (domains.hasOwnProperty(i) && domains[i] && typeof domains[i] === "string") { - domain = _extractDomain(domains[i]); - if (!domain) { - continue; - } - if (domain === "*") { - trustedOriginsExpanded = [ domain ]; - break; - } - trustedOriginsExpanded.push.apply(trustedOriginsExpanded, [ domain, "//" + domain, window.location.protocol + "//" + domain ]); - } + _currentElement = element; + _addClass(element, _globalConfig.hoverClass); + var newTitle = element.getAttribute("title") || _globalConfig.title; + if (typeof newTitle === "string" && newTitle) { + var htmlBridge = _getHtmlBridge(_flashState.bridge); + if (htmlBridge) { + htmlBridge.setAttribute("title", newTitle); } } - if (trustedOriginsExpanded.length) { - str.push("trustedOrigins=" + encodeURIComponent(trustedOriginsExpanded.join(","))); + var useHandCursor = _globalConfig.forceHandCursor === true || _getStyle(element, "cursor") === "pointer"; + _setHandCursor(useHandCursor); + _reposition(); + }; + /** + * The underlying implementation of `ZeroClipboard.blur`/`ZeroClipboard.deactivate`. + * @private + */ + var _blur = function() { + var htmlBridge = _getHtmlBridge(_flashState.bridge); + if (htmlBridge) { + htmlBridge.removeAttribute("title"); + htmlBridge.style.left = "0px"; + htmlBridge.style.top = "-9999px"; + htmlBridge.style.width = "1px"; + htmlBridge.style.height = "1px"; } - if (typeof options.jsModuleId === "string" && options.jsModuleId) { - str.push("jsModuleId=" + encodeURIComponent(options.jsModuleId)); + if (_currentElement) { + _removeClass(_currentElement, _globalConfig.hoverClass); + _removeClass(_currentElement, _globalConfig.activeClass); + _currentElement = null; } - return str.join("&"); }; - var _inArray = function(elem, array, fromIndex) { - if (typeof array.indexOf === "function") { - return array.indexOf(elem, fromIndex); + /** + * The underlying implementation of `ZeroClipboard.activeElement`. + * @private + */ + var _activeElement = function() { + return _currentElement || null; + }; + /** + * Check if a value is a valid HTML4 `ID` or `Name` token. + * @private + */ + var _isValidHtml4Id = function(id) { + return typeof id === "string" && id && /^[A-Za-z][A-Za-z0-9_:\-\.]*$/.test(id); + }; + /** + * Create or update an `event` object, based on the `eventType`. + * @private + */ + var _createEvent = function(event) { + var eventType; + if (typeof event === "string" && event) { + eventType = event; + event = {}; + } else if (typeof event === "object" && event && typeof event.type === "string" && event.type) { + eventType = event.type; + } + if (!eventType) { + return; } - var i, len = array.length; - if (typeof fromIndex === "undefined") { - fromIndex = 0; - } else if (fromIndex < 0) { - fromIndex = len + fromIndex; + eventType = eventType.toLowerCase(); + if (!event.target && (/^(copy|aftercopy|_click)$/.test(eventType) || eventType === "error" && event.name === "clipboard-error")) { + event.target = _copyTarget; } - for (i = fromIndex; i < len; i++) { - if (array.hasOwnProperty(i) && array[i] === elem) { - return i; + _extend(event, { + type: eventType, + target: event.target || _currentElement || null, + relatedTarget: event.relatedTarget || null, + currentTarget: _flashState && _flashState.bridge || null, + timeStamp: event.timeStamp || _now() || null + }); + var msg = _eventMessages[event.type]; + if (event.type === "error" && event.name && msg) { + msg = msg[event.name]; + } + if (msg) { + event.message = msg; + } + if (event.type === "ready") { + _extend(event, { + target: null, + version: _flashState.version + }); + } + if (event.type === "error") { + if (_flashStateErrorNameMatchingRegex.test(event.name)) { + _extend(event, { + target: null, + minimumVersion: _minimumFlashVersion + }); + } + if (_flashStateEnabledErrorNameMatchingRegex.test(event.name)) { + _extend(event, { + version: _flashState.version + }); } } - return -1; + if (event.type === "copy") { + event.clipboardData = { + setData: ZeroClipboard.setData, + clearData: ZeroClipboard.clearData + }; + } + if (event.type === "aftercopy") { + event = _mapClipResultsFromFlash(event, _clipDataFormatMap); + } + if (event.target && !event.relatedTarget) { + event.relatedTarget = _getRelatedTarget(event.target); + } + return _addMouseData(event); }; - var _prepClip = function(elements) { - if (typeof elements === "string") throw new TypeError("ZeroClipboard doesn't accept query strings."); - if (!elements.length) return [ elements ]; - return elements; + /** + * Get a relatedTarget from the target's `data-clipboard-target` attribute + * @private + */ + var _getRelatedTarget = function(targetEl) { + var relatedTargetId = targetEl && targetEl.getAttribute && targetEl.getAttribute("data-clipboard-target"); + return relatedTargetId ? _document.getElementById(relatedTargetId) : null; + }; + /** + * Add element and position data to `MouseEvent` instances + * @private + */ + var _addMouseData = function(event) { + if (event && /^_(?:click|mouse(?:over|out|down|up|move))$/.test(event.type)) { + var srcElement = event.target; + var fromElement = event.type === "_mouseover" && event.relatedTarget ? event.relatedTarget : undefined; + var toElement = event.type === "_mouseout" && event.relatedTarget ? event.relatedTarget : undefined; + var pos = _getElementPosition(srcElement); + var screenLeft = _window.screenLeft || _window.screenX || 0; + var screenTop = _window.screenTop || _window.screenY || 0; + var scrollLeft = _document.body.scrollLeft + _document.documentElement.scrollLeft; + var scrollTop = _document.body.scrollTop + _document.documentElement.scrollTop; + var pageX = pos.left + (typeof event._stageX === "number" ? event._stageX : 0); + var pageY = pos.top + (typeof event._stageY === "number" ? event._stageY : 0); + var clientX = pageX - scrollLeft; + var clientY = pageY - scrollTop; + var screenX = screenLeft + clientX; + var screenY = screenTop + clientY; + var moveX = typeof event.movementX === "number" ? event.movementX : 0; + var moveY = typeof event.movementY === "number" ? event.movementY : 0; + delete event._stageX; + delete event._stageY; + _extend(event, { + srcElement: srcElement, + fromElement: fromElement, + toElement: toElement, + screenX: screenX, + screenY: screenY, + pageX: pageX, + pageY: pageY, + clientX: clientX, + clientY: clientY, + x: clientX, + y: clientY, + movementX: moveX, + movementY: moveY, + offsetX: 0, + offsetY: 0, + layerX: 0, + layerY: 0 + }); + } + return event; + }; + /** + * Determine if an event's registered handlers should be execute synchronously or asynchronously. + * + * @returns {boolean} + * @private + */ + var _shouldPerformAsync = function(event) { + var eventType = event && typeof event.type === "string" && event.type || ""; + return !/^(?:(?:before)?copy|destroy)$/.test(eventType); }; + /** + * Control if a callback should be executed asynchronously or not. + * + * @returns `undefined` + * @private + */ var _dispatchCallback = function(func, context, args, async) { if (async) { - window.setTimeout(function() { + _setTimeout(function() { func.apply(context, args); }, 0); } else { func.apply(context, args); } }; - var _getSafeZIndex = function(val) { - var zIndex, tmp; - if (val) { - if (typeof val === "number" && val > 0) { - zIndex = val; - } else if (typeof val === "string" && (tmp = parseInt(val, 10)) && !isNaN(tmp) && tmp > 0) { - zIndex = tmp; + /** + * Handle the actual dispatching of events to client instances. + * + * @returns `undefined` + * @private + */ + var _dispatchCallbacks = function(event) { + if (!(typeof event === "object" && event && event.type)) { + return; + } + var async = _shouldPerformAsync(event); + var wildcardTypeHandlers = _handlers["*"] || []; + var specificTypeHandlers = _handlers[event.type] || []; + var handlers = wildcardTypeHandlers.concat(specificTypeHandlers); + if (handlers && handlers.length) { + var i, len, func, context, eventCopy, originalContext = this; + for (i = 0, len = handlers.length; i < len; i++) { + func = handlers[i]; + context = originalContext; + if (typeof func === "string" && typeof _window[func] === "function") { + func = _window[func]; + } + if (typeof func === "object" && func && typeof func.handleEvent === "function") { + context = func; + func = func.handleEvent; + } + if (typeof func === "function") { + eventCopy = _extend({}, event); + _dispatchCallback(func, context, [ eventCopy ], async); + } } } - if (!zIndex) { - if (typeof _globalConfig.zIndex === "number" && _globalConfig.zIndex > 0) { - zIndex = _globalConfig.zIndex; - } else if (typeof _globalConfig.zIndex === "string" && (tmp = parseInt(_globalConfig.zIndex, 10)) && !isNaN(tmp) && tmp > 0) { - zIndex = tmp; + return this; + }; + /** + * Check an `error` event's `name` property to see if Flash has + * already loaded, which rules out possible `iframe` sandboxing. + * @private + */ + var _getSandboxStatusFromErrorEvent = function(event) { + var isSandboxed = null; + if (_pageIsFramed === false || event && event.type === "error" && event.name && _errorsThatOnlyOccurAfterFlashLoads.indexOf(event.name) !== -1) { + isSandboxed = false; + } + return isSandboxed; + }; + /** + * Preprocess any special behaviors, reactions, or state changes after receiving this event. + * Executes only once per event emitted, NOT once per client. + * @private + */ + var _preprocessEvent = function(event) { + var element = event.target || _currentElement || null; + var sourceIsSwf = event._source === "swf"; + delete event._source; + switch (event.type) { + case "error": + var isSandboxed = event.name === "flash-sandboxed" || _getSandboxStatusFromErrorEvent(event); + if (typeof isSandboxed === "boolean") { + _flashState.sandboxed = isSandboxed; + } + if (_flashStateErrorNames.indexOf(event.name) !== -1) { + _extend(_flashState, { + disabled: event.name === "flash-disabled", + outdated: event.name === "flash-outdated", + unavailable: event.name === "flash-unavailable", + degraded: event.name === "flash-degraded", + deactivated: event.name === "flash-deactivated", + overdue: event.name === "flash-overdue", + ready: false + }); + } else if (event.name === "version-mismatch") { + _zcSwfVersion = event.swfVersion; + _extend(_flashState, { + disabled: false, + outdated: false, + unavailable: false, + degraded: false, + deactivated: false, + overdue: false, + ready: false + }); + } + _clearTimeoutsAndPolling(); + break; + + case "ready": + _zcSwfVersion = event.swfVersion; + var wasDeactivated = _flashState.deactivated === true; + _extend(_flashState, { + disabled: false, + outdated: false, + sandboxed: false, + unavailable: false, + degraded: false, + deactivated: false, + overdue: wasDeactivated, + ready: !wasDeactivated + }); + _clearTimeoutsAndPolling(); + break; + + case "beforecopy": + _copyTarget = element; + break; + + case "copy": + var textContent, htmlContent, targetEl = event.relatedTarget; + if (!(_clipData["text/html"] || _clipData["text/plain"]) && targetEl && (htmlContent = targetEl.value || targetEl.outerHTML || targetEl.innerHTML) && (textContent = targetEl.value || targetEl.textContent || targetEl.innerText)) { + event.clipboardData.clearData(); + event.clipboardData.setData("text/plain", textContent); + if (htmlContent !== textContent) { + event.clipboardData.setData("text/html", htmlContent); + } + } else if (!_clipData["text/plain"] && event.target && (textContent = event.target.getAttribute("data-clipboard-text"))) { + event.clipboardData.clearData(); + event.clipboardData.setData("text/plain", textContent); + } + break; + + case "aftercopy": + _queueEmitClipboardErrors(event); + ZeroClipboard.clearData(); + if (element && element !== _safeActiveElement() && element.focus) { + element.focus(); + } + break; + + case "_mouseover": + ZeroClipboard.focus(element); + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + if (element && element !== event.relatedTarget && !_containedBy(event.relatedTarget, element)) { + _fireMouseEvent(_extend({}, event, { + type: "mouseenter", + bubbles: false, + cancelable: false + })); + } + _fireMouseEvent(_extend({}, event, { + type: "mouseover" + })); + } + break; + + case "_mouseout": + ZeroClipboard.blur(); + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + if (element && element !== event.relatedTarget && !_containedBy(event.relatedTarget, element)) { + _fireMouseEvent(_extend({}, event, { + type: "mouseleave", + bubbles: false, + cancelable: false + })); + } + _fireMouseEvent(_extend({}, event, { + type: "mouseout" + })); + } + break; + + case "_mousedown": + _addClass(element, _globalConfig.activeClass); + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + _fireMouseEvent(_extend({}, event, { + type: event.type.slice(1) + })); + } + break; + + case "_mouseup": + _removeClass(element, _globalConfig.activeClass); + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + _fireMouseEvent(_extend({}, event, { + type: event.type.slice(1) + })); + } + break; + + case "_click": + _copyTarget = null; + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + _fireMouseEvent(_extend({}, event, { + type: event.type.slice(1) + })); + } + break; + + case "_mousemove": + if (_globalConfig.bubbleEvents === true && sourceIsSwf) { + _fireMouseEvent(_extend({}, event, { + type: event.type.slice(1) + })); + } + break; + } + if (/^_(?:click|mouse(?:over|out|down|up|move))$/.test(event.type)) { + return true; + } + }; + /** + * Check an "aftercopy" event for clipboard errors and emit a corresponding "error" event. + * @private + */ + var _queueEmitClipboardErrors = function(aftercopyEvent) { + if (aftercopyEvent.errors && aftercopyEvent.errors.length > 0) { + var errorEvent = _deepCopy(aftercopyEvent); + _extend(errorEvent, { + type: "error", + name: "clipboard-error" + }); + delete errorEvent.success; + _setTimeout(function() { + ZeroClipboard.emit(errorEvent); + }, 0); + } + }; + /** + * Dispatch a synthetic MouseEvent. + * + * @returns `undefined` + * @private + */ + var _fireMouseEvent = function(event) { + if (!(event && typeof event.type === "string" && event)) { + return; + } + var e, target = event.target || null, doc = target && target.ownerDocument || _document, defaults = { + view: doc.defaultView || _window, + canBubble: true, + cancelable: true, + detail: event.type === "click" ? 1 : 0, + button: typeof event.which === "number" ? event.which - 1 : typeof event.button === "number" ? event.button : doc.createEvent ? 0 : 1 + }, args = _extend(defaults, event); + if (!target) { + return; + } + if (doc.createEvent && target.dispatchEvent) { + args = [ args.type, args.canBubble, args.cancelable, args.view, args.detail, args.screenX, args.screenY, args.clientX, args.clientY, args.ctrlKey, args.altKey, args.shiftKey, args.metaKey, args.button, args.relatedTarget ]; + e = doc.createEvent("MouseEvents"); + if (e.initMouseEvent) { + e.initMouseEvent.apply(e, args); + e._source = "js"; + target.dispatchEvent(e); } } - return zIndex || 0; }; - var _deprecationWarning = function(deprecatedApiName, debugEnabled) { - if (deprecatedApiName && debugEnabled !== false && typeof console !== "undefined" && console && (console.warn || console.log)) { - var deprecationWarning = "`" + deprecatedApiName + "` is deprecated. See docs for more info:\n" + " https://github.com/zeroclipboard/zeroclipboard/blob/master/docs/instructions.md#deprecations"; - if (console.warn) { - console.warn(deprecationWarning); - } else { - console.log(deprecationWarning); + /** + * Continuously poll the DOM until either: + * (a) the fallback content becomes visible, or + * (b) we receive an event from SWF (handled elsewhere) + * + * IMPORTANT: + * This is NOT a necessary check but it can result in significantly faster + * detection of bad `swfPath` configuration and/or network/server issues [in + * supported browsers] than waiting for the entire `flashLoadTimeout` duration + * to elapse before detecting that the SWF cannot be loaded. The detection + * duration can be anywhere from 10-30 times faster [in supported browsers] by + * using this approach. + * + * @returns `undefined` + * @private + */ + var _watchForSwfFallbackContent = function() { + var maxWait = _globalConfig.flashLoadTimeout; + if (typeof maxWait === "number" && maxWait >= 0) { + var pollWait = Math.min(1e3, maxWait / 10); + var fallbackContentId = _globalConfig.swfObjectId + "_fallbackContent"; + _swfFallbackCheckInterval = _setInterval(function() { + var el = _document.getElementById(fallbackContentId); + if (_isElementVisible(el)) { + _clearTimeoutsAndPolling(); + _flashState.deactivated = null; + ZeroClipboard.emit({ + type: "error", + name: "swf-not-found" + }); + } + }, pollWait); + } + }; + /** + * Create the HTML bridge element to embed the Flash object into. + * @private + */ + var _createHtmlBridge = function() { + var container = _document.createElement("div"); + container.id = _globalConfig.containerId; + container.className = _globalConfig.containerClass; + container.style.position = "absolute"; + container.style.left = "0px"; + container.style.top = "-9999px"; + container.style.width = "1px"; + container.style.height = "1px"; + container.style.zIndex = "" + _getSafeZIndex(_globalConfig.zIndex); + return container; + }; + /** + * Get the HTML element container that wraps the Flash bridge object/element. + * @private + */ + var _getHtmlBridge = function(flashBridge) { + var htmlBridge = flashBridge && flashBridge.parentNode; + while (htmlBridge && htmlBridge.nodeName === "OBJECT" && htmlBridge.parentNode) { + htmlBridge = htmlBridge.parentNode; + } + return htmlBridge || null; + }; + /** + * Create the SWF object. + * + * @returns The SWF object reference. + * @private + */ + var _embedSwf = function() { + var len, flashBridge = _flashState.bridge, container = _getHtmlBridge(flashBridge); + if (!flashBridge) { + var allowScriptAccess = _determineScriptAccess(_window.location.host, _globalConfig); + var allowNetworking = allowScriptAccess === "never" ? "none" : "all"; + var flashvars = _vars(_extend({ + jsVersion: ZeroClipboard.version + }, _globalConfig)); + var swfUrl = _globalConfig.swfPath + _cacheBust(_globalConfig.swfPath, _globalConfig); + container = _createHtmlBridge(); + var divToBeReplaced = _document.createElement("div"); + container.appendChild(divToBeReplaced); + _document.body.appendChild(container); + var tmpDiv = _document.createElement("div"); + var usingActiveX = _flashState.pluginType === "activex"; + tmpDiv.innerHTML = '" + (usingActiveX ? '' : "") + '' + '' + '' + '' + '' + '
 
' + "
"; + flashBridge = tmpDiv.firstChild; + tmpDiv = null; + _unwrap(flashBridge).ZeroClipboard = ZeroClipboard; + container.replaceChild(flashBridge, divToBeReplaced); + _watchForSwfFallbackContent(); + } + if (!flashBridge) { + flashBridge = _document[_globalConfig.swfObjectId]; + if (flashBridge && (len = flashBridge.length)) { + flashBridge = flashBridge[len - 1]; + } + if (!flashBridge && container) { + flashBridge = container.firstChild; } } + _flashState.bridge = flashBridge || null; + return flashBridge; }; - var _extend = function() { - var i, len, arg, prop, src, copy, target = arguments[0] || {}; - for (i = 1, len = arguments.length; i < len; i++) { - if ((arg = arguments[i]) != null) { - for (prop in arg) { - if (arg.hasOwnProperty(prop)) { - src = target[prop]; - copy = arg[prop]; - if (target === copy) { - continue; + /** + * Destroy the SWF object. + * @private + */ + var _unembedSwf = function() { + var flashBridge = _flashState.bridge; + if (flashBridge) { + var htmlBridge = _getHtmlBridge(flashBridge); + if (htmlBridge) { + if (_flashState.pluginType === "activex" && "readyState" in flashBridge) { + flashBridge.style.display = "none"; + (function removeSwfFromIE() { + if (flashBridge.readyState === 4) { + for (var prop in flashBridge) { + if (typeof flashBridge[prop] === "function") { + flashBridge[prop] = null; + } + } + if (flashBridge.parentNode) { + flashBridge.parentNode.removeChild(flashBridge); + } + if (htmlBridge.parentNode) { + htmlBridge.parentNode.removeChild(htmlBridge); + } + } else { + _setTimeout(removeSwfFromIE, 10); } - if (copy !== undefined) { - target[prop] = copy; + })(); + } else { + if (flashBridge.parentNode) { + flashBridge.parentNode.removeChild(flashBridge); + } + if (htmlBridge.parentNode) { + htmlBridge.parentNode.removeChild(htmlBridge); + } + } + } + _clearTimeoutsAndPolling(); + _flashState.ready = null; + _flashState.bridge = null; + _flashState.deactivated = null; + _zcSwfVersion = undefined; + } + }; + /** + * Map the data format names of the "clipData" to Flash-friendly names. + * + * @returns A new transformed object. + * @private + */ + var _mapClipDataToFlash = function(clipData) { + var newClipData = {}, formatMap = {}; + if (!(typeof clipData === "object" && clipData)) { + return; + } + for (var dataFormat in clipData) { + if (dataFormat && _hasOwn.call(clipData, dataFormat) && typeof clipData[dataFormat] === "string" && clipData[dataFormat]) { + switch (dataFormat.toLowerCase()) { + case "text/plain": + case "text": + case "air:text": + case "flash:text": + newClipData.text = clipData[dataFormat]; + formatMap.text = dataFormat; + break; + + case "text/html": + case "html": + case "air:html": + case "flash:html": + newClipData.html = clipData[dataFormat]; + formatMap.html = dataFormat; + break; + + case "application/rtf": + case "text/rtf": + case "rtf": + case "richtext": + case "air:rtf": + case "flash:rtf": + newClipData.rtf = clipData[dataFormat]; + formatMap.rtf = dataFormat; + break; + + default: + break; + } + } + } + return { + data: newClipData, + formatMap: formatMap + }; + }; + /** + * Map the data format names from Flash-friendly names back to their original "clipData" names (via a format mapping). + * + * @returns A new transformed object. + * @private + */ + var _mapClipResultsFromFlash = function(clipResults, formatMap) { + if (!(typeof clipResults === "object" && clipResults && typeof formatMap === "object" && formatMap)) { + return clipResults; + } + var newResults = {}; + for (var prop in clipResults) { + if (_hasOwn.call(clipResults, prop)) { + if (prop === "errors") { + newResults[prop] = clipResults[prop] ? clipResults[prop].slice() : []; + for (var i = 0, len = newResults[prop].length; i < len; i++) { + newResults[prop][i].format = formatMap[newResults[prop][i].format]; + } + } else if (prop !== "success" && prop !== "data") { + newResults[prop] = clipResults[prop]; + } else { + newResults[prop] = {}; + var tmpHash = clipResults[prop]; + for (var dataFormat in tmpHash) { + if (dataFormat && _hasOwn.call(tmpHash, dataFormat) && _hasOwn.call(formatMap, dataFormat)) { + newResults[prop][formatMap[dataFormat]] = tmpHash[dataFormat]; } } } } } - return target; + return newResults; + }; + /** + * Will look at a path, and will create a "?noCache={time}" or "&noCache={time}" + * query param string to return. Does NOT append that string to the original path. + * This is useful because ExternalInterface often breaks when a Flash SWF is cached. + * + * @returns The `noCache` query param with necessary "?"/"&" prefix. + * @private + */ + var _cacheBust = function(path, options) { + var cacheBust = options == null || options && options.cacheBust === true; + if (cacheBust) { + return (path.indexOf("?") === -1 ? "?" : "&") + "noCache=" + _now(); + } else { + return ""; + } }; + /** + * Creates a query string for the FlashVars param. + * Does NOT include the cache-busting query param. + * + * @returns FlashVars query string + * @private + */ + var _vars = function(options) { + var i, len, domain, domains, str = "", trustedOriginsExpanded = []; + if (options.trustedDomains) { + if (typeof options.trustedDomains === "string") { + domains = [ options.trustedDomains ]; + } else if (typeof options.trustedDomains === "object" && "length" in options.trustedDomains) { + domains = options.trustedDomains; + } + } + if (domains && domains.length) { + for (i = 0, len = domains.length; i < len; i++) { + if (_hasOwn.call(domains, i) && domains[i] && typeof domains[i] === "string") { + domain = _extractDomain(domains[i]); + if (!domain) { + continue; + } + if (domain === "*") { + trustedOriginsExpanded.length = 0; + trustedOriginsExpanded.push(domain); + break; + } + trustedOriginsExpanded.push.apply(trustedOriginsExpanded, [ domain, "//" + domain, _window.location.protocol + "//" + domain ]); + } + } + } + if (trustedOriginsExpanded.length) { + str += "trustedOrigins=" + _encodeURIComponent(trustedOriginsExpanded.join(",")); + } + if (options.forceEnhancedClipboard === true) { + str += (str ? "&" : "") + "forceEnhancedClipboard=true"; + } + if (typeof options.swfObjectId === "string" && options.swfObjectId) { + str += (str ? "&" : "") + "swfObjectId=" + _encodeURIComponent(options.swfObjectId); + } + if (typeof options.jsVersion === "string" && options.jsVersion) { + str += (str ? "&" : "") + "jsVersion=" + _encodeURIComponent(options.jsVersion); + } + return str; + }; + /** + * Extract the domain (e.g. "github.com") from an origin (e.g. "https://github.com") or + * URL (e.g. "https://github.com/zeroclipboard/zeroclipboard/"). + * + * @returns the domain + * @private + */ var _extractDomain = function(originOrUrl) { if (originOrUrl == null || originOrUrl === "") { return null; @@ -359,57 +1443,49 @@ if (originOrUrl && originOrUrl.slice(-4).toLowerCase() === ".swf") { return null; } - return originOrUrl || null; - }; - var _determineScriptAccess = function() { - var _extractAllDomains = function(origins, resultsArray) { - var i, len, tmp; - if (origins != null && resultsArray[0] !== "*") { - if (typeof origins === "string") { - origins = [ origins ]; - } - if (typeof origins === "object" && "length" in origins) { - for (i = 0, len = origins.length; i < len; i++) { - if (origins.hasOwnProperty(i)) { - tmp = _extractDomain(origins[i]); - if (tmp) { - if (tmp === "*") { - resultsArray.length = 0; - resultsArray.push("*"); - break; - } - if (_inArray(tmp, resultsArray) === -1) { - resultsArray.push(tmp); - } - } - } + return originOrUrl || null; + }; + /** + * Set `allowScriptAccess` based on `trustedDomains` and `window.location.host` vs. `swfPath`. + * + * @returns The appropriate script access level. + * @private + */ + var _determineScriptAccess = function() { + var _extractAllDomains = function(origins) { + var i, len, tmp, resultsArray = []; + if (typeof origins === "string") { + origins = [ origins ]; + } + if (!(typeof origins === "object" && origins && typeof origins.length === "number")) { + return resultsArray; + } + for (i = 0, len = origins.length; i < len; i++) { + if (_hasOwn.call(origins, i) && (tmp = _extractDomain(origins[i]))) { + if (tmp === "*") { + resultsArray.length = 0; + resultsArray.push("*"); + break; + } + if (resultsArray.indexOf(tmp) === -1) { + resultsArray.push(tmp); } } } - }; - var _accessLevelLookup = { - always: "always", - samedomain: "sameDomain", - never: "never" + return resultsArray; }; return function(currentDomain, configOptions) { - var asaLower, allowScriptAccess = configOptions.allowScriptAccess; - if (typeof allowScriptAccess === "string" && (asaLower = allowScriptAccess.toLowerCase()) && /^always|samedomain|never$/.test(asaLower)) { - return _accessLevelLookup[asaLower]; - } - var swfDomain = _extractDomain(configOptions.moviePath); + var swfDomain = _extractDomain(configOptions.swfPath); if (swfDomain === null) { swfDomain = currentDomain; } - var trustedDomains = []; - _extractAllDomains(configOptions.trustedOrigins, trustedDomains); - _extractAllDomains(configOptions.trustedDomains, trustedDomains); + var trustedDomains = _extractAllDomains(configOptions.trustedDomains); var len = trustedDomains.length; if (len > 0) { if (len === 1 && trustedDomains[0] === "*") { return "always"; } - if (_inArray(currentDomain, trustedDomains) !== -1) { + if (trustedDomains.indexOf(currentDomain) !== -1) { if (len === 1 && currentDomain === swfDomain) { return "sameDomain"; } @@ -419,592 +1495,1087 @@ return "never"; }; }(); - var _objectKeys = function(obj) { - if (obj == null) { - return []; - } - if (Object.keys) { - return Object.keys(obj); + /** + * Get the currently active/focused DOM element. + * + * @returns the currently active/focused element, or `null` + * @private + */ + var _safeActiveElement = function() { + try { + return _document.activeElement; + } catch (err) { + return null; } - var keys = []; - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - keys.push(prop); + }; + /** + * Add a class to an element, if it doesn't already have it. + * + * @returns The element, with its new class added. + * @private + */ + var _addClass = function(element, value) { + var c, cl, className, classNames = []; + if (typeof value === "string" && value) { + classNames = value.split(/\s+/); + } + if (element && element.nodeType === 1 && classNames.length > 0) { + if (element.classList) { + for (c = 0, cl = classNames.length; c < cl; c++) { + element.classList.add(classNames[c]); + } + } else if (element.hasOwnProperty("className")) { + className = " " + element.className + " "; + for (c = 0, cl = classNames.length; c < cl; c++) { + if (className.indexOf(" " + classNames[c] + " ") === -1) { + className += classNames[c] + " "; + } + } + element.className = className.replace(/^\s+|\s+$/g, ""); } } - return keys; + return element; }; - var _deleteOwnProperties = function(obj) { - if (obj) { - for (var prop in obj) { - if (obj.hasOwnProperty(prop)) { - delete obj[prop]; + /** + * Remove a class from an element, if it has it. + * + * @returns The element, with its class removed. + * @private + */ + var _removeClass = function(element, value) { + var c, cl, className, classNames = []; + if (typeof value === "string" && value) { + classNames = value.split(/\s+/); + } + if (element && element.nodeType === 1 && classNames.length > 0) { + if (element.classList && element.classList.length > 0) { + for (c = 0, cl = classNames.length; c < cl; c++) { + element.classList.remove(classNames[c]); + } + } else if (element.className) { + className = (" " + element.className + " ").replace(/[\r\n\t]/g, " "); + for (c = 0, cl = classNames.length; c < cl; c++) { + className = className.replace(" " + classNames[c] + " ", " "); } + element.className = className.replace(/^\s+|\s+$/g, ""); } } - return obj; + return element; }; - var _detectFlashSupport = function() { - var hasFlash = false; - if (typeof flashState.disabled === "boolean") { - hasFlash = flashState.disabled === false; - } else { - if (typeof ActiveXObject === "function") { - try { - if (new ActiveXObject("ShockwaveFlash.ShockwaveFlash")) { - hasFlash = true; - } - } catch (error) {} - } - if (!hasFlash && navigator.mimeTypes["application/x-shockwave-flash"]) { - hasFlash = true; + /** + * Attempt to interpret the element's CSS styling. If `prop` is `"cursor"`, + * then we assume that it should be a hand ("pointer") cursor if the element + * is an anchor element ("a" tag). + * + * @returns The computed style property. + * @private + */ + var _getStyle = function(el, prop) { + var value = _getComputedStyle(el, null).getPropertyValue(prop); + if (prop === "cursor") { + if (!value || value === "auto") { + if (el.nodeName === "A") { + return "pointer"; + } } } - return hasFlash; + return value; }; - function _parseFlashVersion(flashVersion) { - return flashVersion.replace(/,/g, ".").replace(/[^0-9\.]/g, ""); - } - function _isFlashVersionSupported(flashVersion) { - return parseFloat(_parseFlashVersion(flashVersion)) >= 10; - } - var ZeroClipboard = function(elements, options) { - if (!(this instanceof ZeroClipboard)) { - return new ZeroClipboard(elements, options); - } - this.id = "" + clientIdCounter++; - _clientMeta[this.id] = { - instance: this, - elements: [], - handlers: {} + /** + * Get the absolutely positioned coordinates of a DOM element. + * + * @returns Object containing the element's position, width, and height. + * @private + */ + var _getElementPosition = function(el) { + var pos = { + left: 0, + top: 0, + width: 0, + height: 0 }; - if (elements) { - this.clip(elements); - } - if (typeof options !== "undefined") { - _deprecationWarning("new ZeroClipboard(elements, options)", _globalConfig.debug); - ZeroClipboard.config(options); - } - this.options = ZeroClipboard.config(); - if (typeof flashState.disabled !== "boolean") { - flashState.disabled = !_detectFlashSupport(); - } - if (flashState.disabled === false && flashState.outdated !== true) { - if (flashState.bridge === null) { - flashState.outdated = false; - flashState.ready = false; - _bridge(); + if (el.getBoundingClientRect) { + var elRect = el.getBoundingClientRect(); + var pageXOffset = _window.pageXOffset; + var pageYOffset = _window.pageYOffset; + var leftBorderWidth = _document.documentElement.clientLeft || 0; + var topBorderWidth = _document.documentElement.clientTop || 0; + var leftBodyOffset = 0; + var topBodyOffset = 0; + if (_getStyle(_document.body, "position") === "relative") { + var bodyRect = _document.body.getBoundingClientRect(); + var htmlRect = _document.documentElement.getBoundingClientRect(); + leftBodyOffset = bodyRect.left - htmlRect.left || 0; + topBodyOffset = bodyRect.top - htmlRect.top || 0; } + pos.left = elRect.left + pageXOffset - leftBorderWidth - leftBodyOffset; + pos.top = elRect.top + pageYOffset - topBorderWidth - topBodyOffset; + pos.width = "width" in elRect ? elRect.width : elRect.right - elRect.left; + pos.height = "height" in elRect ? elRect.height : elRect.bottom - elRect.top; } + return pos; }; - ZeroClipboard.prototype.setText = function(newText) { - if (newText && newText !== "") { - _clipData["text/plain"] = newText; - if (flashState.ready === true && flashState.bridge) { - flashState.bridge.setText(newText); - } else {} - } - return this; - }; - ZeroClipboard.prototype.setSize = function(width, height) { - if (flashState.ready === true && flashState.bridge) { - flashState.bridge.setSize(width, height); - } else {} - return this; + /** + * Determine is an element is visible somewhere within the document (page). + * + * @returns Boolean + * @private + */ + var _isElementVisible = function(el) { + if (!el) { + return false; + } + var styles = _getComputedStyle(el, null); + var hasCssHeight = _parseFloat(styles.height) > 0; + var hasCssWidth = _parseFloat(styles.width) > 0; + var hasCssTop = _parseFloat(styles.top) >= 0; + var hasCssLeft = _parseFloat(styles.left) >= 0; + var cssKnows = hasCssHeight && hasCssWidth && hasCssTop && hasCssLeft; + var rect = cssKnows ? null : _getElementPosition(el); + var isVisible = styles.display !== "none" && styles.visibility !== "collapse" && (cssKnows || !!rect && (hasCssHeight || rect.height > 0) && (hasCssWidth || rect.width > 0) && (hasCssTop || rect.top >= 0) && (hasCssLeft || rect.left >= 0)); + return isVisible; }; - var _setHandCursor = function(enabled) { - if (flashState.ready === true && flashState.bridge) { - flashState.bridge.setHandCursor(enabled); - } else {} + /** + * Clear all existing timeouts and interval polling delegates. + * + * @returns `undefined` + * @private + */ + var _clearTimeoutsAndPolling = function() { + _clearTimeout(_flashCheckTimeout); + _flashCheckTimeout = 0; + _clearInterval(_swfFallbackCheckInterval); + _swfFallbackCheckInterval = 0; }; - ZeroClipboard.prototype.destroy = function() { - this.unclip(); - this.off(); - delete _clientMeta[this.id]; + /** + * Reposition the Flash object to cover the currently activated element. + * + * @returns `undefined` + * @private + */ + var _reposition = function() { + var htmlBridge; + if (_currentElement && (htmlBridge = _getHtmlBridge(_flashState.bridge))) { + var pos = _getElementPosition(_currentElement); + _extend(htmlBridge.style, { + width: pos.width + "px", + height: pos.height + "px", + top: pos.top + "px", + left: pos.left + "px", + zIndex: "" + _getSafeZIndex(_globalConfig.zIndex) + }); + } }; - var _getAllClients = function() { - var i, len, client, clients = [], clientIds = _objectKeys(_clientMeta); - for (i = 0, len = clientIds.length; i < len; i++) { - client = _clientMeta[clientIds[i]].instance; - if (client && client instanceof ZeroClipboard) { - clients.push(client); + /** + * Sends a signal to the Flash object to display the hand cursor if `true`. + * + * @returns `undefined` + * @private + */ + var _setHandCursor = function(enabled) { + if (_flashState.ready === true) { + if (_flashState.bridge && typeof _flashState.bridge.setHandCursor === "function") { + _flashState.bridge.setHandCursor(enabled); + } else { + _flashState.ready = false; } } - return clients; }; - ZeroClipboard.version = "1.3.2"; - var _globalConfig = { - swfPath: _swfPath, - trustedDomains: window.location.host ? [ window.location.host ] : [], - cacheBust: true, - forceHandCursor: false, - zIndex: 999999999, - debug: true, - title: null, - autoActivate: true - }; - ZeroClipboard.config = function(options) { - if (typeof options === "object" && options !== null) { - _extend(_globalConfig, options); + /** + * Get a safe value for `zIndex` + * + * @returns an integer, or "auto" + * @private + */ + var _getSafeZIndex = function(val) { + if (/^(?:auto|inherit)$/.test(val)) { + return val; } - if (typeof options === "string" && options) { - if (_globalConfig.hasOwnProperty(options)) { - return _globalConfig[options]; - } - return; + var zIndex; + if (typeof val === "number" && !_isNaN(val)) { + zIndex = val; + } else if (typeof val === "string") { + zIndex = _getSafeZIndex(_parseInt(val, 10)); } - var copy = {}; - for (var prop in _globalConfig) { - if (_globalConfig.hasOwnProperty(prop)) { - if (typeof _globalConfig[prop] === "object" && _globalConfig[prop] !== null) { - if ("length" in _globalConfig[prop]) { - copy[prop] = _globalConfig[prop].slice(0); - } else { - copy[prop] = _extend({}, _globalConfig[prop]); - } - } else { - copy[prop] = _globalConfig[prop]; + return typeof zIndex === "number" ? zIndex : "auto"; + }; + /** + * Attempt to detect if ZeroClipboard is executing inside of a sandboxed iframe. + * If it is, Flash Player cannot be used, so ZeroClipboard is dead in the water. + * + * @see {@link http://lists.w3.org/Archives/Public/public-whatwg-archive/2014Dec/0002.html} + * @see {@link https://github.com/zeroclipboard/zeroclipboard/issues/511} + * @see {@link http://zeroclipboard.org/test-iframes.html} + * + * @returns `true` (is sandboxed), `false` (is not sandboxed), or `null` (uncertain) + * @private + */ + var _detectSandbox = function(doNotReassessFlashSupport) { + var effectiveScriptOrigin, frame, frameError, previousState = _flashState.sandboxed, isSandboxed = null; + doNotReassessFlashSupport = doNotReassessFlashSupport === true; + if (_pageIsFramed === false) { + isSandboxed = false; + } else { + try { + frame = window.frameElement || null; + } catch (e) { + frameError = { + name: e.name, + message: e.message + }; + } + if (frame && frame.nodeType === 1 && frame.nodeName === "IFRAME") { + try { + isSandboxed = frame.hasAttribute("sandbox"); + } catch (e) { + isSandboxed = null; + } + } else { + try { + effectiveScriptOrigin = document.domain || null; + } catch (e) { + effectiveScriptOrigin = null; + } + if (effectiveScriptOrigin === null || frameError && frameError.name === "SecurityError" && /(^|[\s\(\[@])sandbox(es|ed|ing|[\s\.,!\)\]@]|$)/.test(frameError.message.toLowerCase())) { + isSandboxed = true; } } } - return copy; + _flashState.sandboxed = isSandboxed; + if (previousState !== isSandboxed && !doNotReassessFlashSupport) { + _detectFlashSupport(_ActiveXObject); + } + return isSandboxed; }; - ZeroClipboard.destroy = function() { - ZeroClipboard.deactivate(); - for (var clientId in _clientMeta) { - if (_clientMeta.hasOwnProperty(clientId) && _clientMeta[clientId]) { - var client = _clientMeta[clientId].instance; - if (client && typeof client.destroy === "function") { - client.destroy(); + /** + * Detect the Flash Player status, version, and plugin type. + * + * @see {@link https://code.google.com/p/doctype-mirror/wiki/ArticleDetectFlash#The_code} + * @see {@link http://stackoverflow.com/questions/12866060/detecting-pepper-ppapi-flash-with-javascript} + * + * @returns `undefined` + * @private + */ + var _detectFlashSupport = function(ActiveXObject) { + var plugin, ax, mimeType, hasFlash = false, isActiveX = false, isPPAPI = false, flashVersion = ""; + /** + * Derived from Apple's suggested sniffer. + * @param {String} desc e.g. "Shockwave Flash 7.0 r61" + * @returns {String} "7.0.61" + * @private + */ + function parseFlashVersion(desc) { + var matches = desc.match(/[\d]+/g); + matches.length = 3; + return matches.join("."); + } + function isPepperFlash(flashPlayerFileName) { + return !!flashPlayerFileName && (flashPlayerFileName = flashPlayerFileName.toLowerCase()) && (/^(pepflashplayer\.dll|libpepflashplayer\.so|pepperflashplayer\.plugin)$/.test(flashPlayerFileName) || flashPlayerFileName.slice(-13) === "chrome.plugin"); + } + function inspectPlugin(plugin) { + if (plugin) { + hasFlash = true; + if (plugin.version) { + flashVersion = parseFlashVersion(plugin.version); + } + if (!flashVersion && plugin.description) { + flashVersion = parseFlashVersion(plugin.description); + } + if (plugin.filename) { + isPPAPI = isPepperFlash(plugin.filename); } } } - var htmlBridge = _getHtmlBridge(flashState.bridge); - if (htmlBridge && htmlBridge.parentNode) { - htmlBridge.parentNode.removeChild(htmlBridge); - flashState.ready = null; - flashState.bridge = null; + if (_navigator.plugins && _navigator.plugins.length) { + plugin = _navigator.plugins["Shockwave Flash"]; + inspectPlugin(plugin); + if (_navigator.plugins["Shockwave Flash 2.0"]) { + hasFlash = true; + flashVersion = "2.0.0.11"; + } + } else if (_navigator.mimeTypes && _navigator.mimeTypes.length) { + mimeType = _navigator.mimeTypes["application/x-shockwave-flash"]; + plugin = mimeType && mimeType.enabledPlugin; + inspectPlugin(plugin); + } else if (typeof ActiveXObject !== "undefined") { + isActiveX = true; + try { + ax = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7"); + hasFlash = true; + flashVersion = parseFlashVersion(ax.GetVariable("$version")); + } catch (e1) { + try { + ax = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6"); + hasFlash = true; + flashVersion = "6.0.21"; + } catch (e2) { + try { + ax = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); + hasFlash = true; + flashVersion = parseFlashVersion(ax.GetVariable("$version")); + } catch (e3) { + isActiveX = false; + } + } + } } + _flashState.disabled = hasFlash !== true; + _flashState.outdated = flashVersion && _parseFloat(flashVersion) < _parseFloat(_minimumFlashVersion); + _flashState.version = flashVersion || "0.0.0"; + _flashState.pluginType = isPPAPI ? "pepper" : isActiveX ? "activex" : hasFlash ? "netscape" : "unknown"; }; - ZeroClipboard.activate = function(element) { - if (currentElement) { - _removeClass(currentElement, _globalConfig.hoverClass); - _removeClass(currentElement, _globalConfig.activeClass); + /** + * Invoke the Flash detection algorithms immediately upon inclusion so we're not waiting later. + */ + _detectFlashSupport(_ActiveXObject); + /** + * Always assess the `sandboxed` state of the page at important Flash-related moments. + */ + _detectSandbox(true); + /** + * A shell constructor for `ZeroClipboard` client instances. + * + * @constructor + */ + var ZeroClipboard = function() { + if (!(this instanceof ZeroClipboard)) { + return new ZeroClipboard(); } - currentElement = element; - _addClass(element, _globalConfig.hoverClass); - _reposition(); - var newTitle = _globalConfig.title || element.getAttribute("title"); - if (newTitle) { - var htmlBridge = _getHtmlBridge(flashState.bridge); - if (htmlBridge) { - htmlBridge.setAttribute("title", newTitle); - } + if (typeof ZeroClipboard._createClient === "function") { + ZeroClipboard._createClient.apply(this, _args(arguments)); } - var useHandCursor = _globalConfig.forceHandCursor === true || _getStyle(element, "cursor") === "pointer"; - _setHandCursor(useHandCursor); }; - ZeroClipboard.deactivate = function() { - var htmlBridge = _getHtmlBridge(flashState.bridge); - if (htmlBridge) { - htmlBridge.style.left = "0px"; - htmlBridge.style.top = "-9999px"; - htmlBridge.removeAttribute("title"); - } - if (currentElement) { - _removeClass(currentElement, _globalConfig.hoverClass); - _removeClass(currentElement, _globalConfig.activeClass); - currentElement = null; - } - }; - var _bridge = function() { - var flashBridge, len; - var container = document.getElementById("global-zeroclipboard-html-bridge"); - if (!container) { - var opts = ZeroClipboard.config(); - opts.jsModuleId = typeof _amdModuleId === "string" && _amdModuleId || typeof _cjsModuleId === "string" && _cjsModuleId || null; - var allowScriptAccess = _determineScriptAccess(window.location.host, _globalConfig); - var flashvars = _vars(opts); - var swfUrl = _globalConfig.moviePath + _cacheBust(_globalConfig.moviePath, _globalConfig); - var html = ' '; - container = document.createElement("div"); - container.id = "global-zeroclipboard-html-bridge"; - container.setAttribute("class", "global-zeroclipboard-container"); - container.style.position = "absolute"; - container.style.left = "0px"; - container.style.top = "-9999px"; - container.style.width = "15px"; - container.style.height = "15px"; - container.style.zIndex = "" + _getSafeZIndex(_globalConfig.zIndex); - document.body.appendChild(container); - container.innerHTML = html; - } - flashBridge = document["global-zeroclipboard-flash-bridge"]; - if (flashBridge && (len = flashBridge.length)) { - flashBridge = flashBridge[len - 1]; - } - flashState.bridge = flashBridge || container.children[0].lastElementChild; + /** + * The ZeroClipboard library's version number. + * + * @static + * @readonly + * @property {string} + */ + _defineProperty(ZeroClipboard, "version", { + value: "2.2.0", + writable: false, + configurable: true, + enumerable: true + }); + /** + * Update or get a copy of the ZeroClipboard global configuration. + * Returns a copy of the current/updated configuration. + * + * @returns Object + * @static + */ + ZeroClipboard.config = function() { + return _config.apply(this, _args(arguments)); }; - var _getHtmlBridge = function(flashBridge) { - var isFlashElement = /^OBJECT|EMBED$/; - var htmlBridge = flashBridge && flashBridge.parentNode; - while (htmlBridge && isFlashElement.test(htmlBridge.nodeName) && htmlBridge.parentNode) { - htmlBridge = htmlBridge.parentNode; - } - return htmlBridge || null; + /** + * Diagnostic method that describes the state of the browser, Flash Player, and ZeroClipboard. + * + * @returns Object + * @static + */ + ZeroClipboard.state = function() { + return _state.apply(this, _args(arguments)); }; - var _reposition = function() { - if (currentElement) { - var pos = _getDOMObjectPosition(currentElement, _globalConfig.zIndex); - var htmlBridge = _getHtmlBridge(flashState.bridge); - if (htmlBridge) { - htmlBridge.style.top = pos.top + "px"; - htmlBridge.style.left = pos.left + "px"; - htmlBridge.style.width = pos.width + "px"; - htmlBridge.style.height = pos.height + "px"; - htmlBridge.style.zIndex = pos.zIndex + 1; - } - if (flashState.ready === true && flashState.bridge) { - flashState.bridge.setSize(pos.width, pos.height); - } + /** + * Check if Flash is unusable for any reason: disabled, outdated, deactivated, etc. + * + * @returns Boolean + * @static + */ + ZeroClipboard.isFlashUnusable = function() { + return _isFlashUnusable.apply(this, _args(arguments)); + }; + /** + * Register an event listener. + * + * @returns `ZeroClipboard` + * @static + */ + ZeroClipboard.on = function() { + return _on.apply(this, _args(arguments)); + }; + /** + * Unregister an event listener. + * If no `listener` function/object is provided, it will unregister all listeners for the provided `eventType`. + * If no `eventType` is provided, it will unregister all listeners for every event type. + * + * @returns `ZeroClipboard` + * @static + */ + ZeroClipboard.off = function() { + return _off.apply(this, _args(arguments)); + }; + /** + * Retrieve event listeners for an `eventType`. + * If no `eventType` is provided, it will retrieve all listeners for every event type. + * + * @returns array of listeners for the `eventType`; if no `eventType`, then a map/hash object of listeners for all event types; or `null` + */ + ZeroClipboard.handlers = function() { + return _listeners.apply(this, _args(arguments)); + }; + /** + * Event emission receiver from the Flash object, forwarding to any registered JavaScript event listeners. + * + * @returns For the "copy" event, returns the Flash-friendly "clipData" object; otherwise `undefined`. + * @static + */ + ZeroClipboard.emit = function() { + return _emit.apply(this, _args(arguments)); + }; + /** + * Create and embed the Flash object. + * + * @returns The Flash object + * @static + */ + ZeroClipboard.create = function() { + return _create.apply(this, _args(arguments)); + }; + /** + * Self-destruct and clean up everything, including the embedded Flash object. + * + * @returns `undefined` + * @static + */ + ZeroClipboard.destroy = function() { + return _destroy.apply(this, _args(arguments)); + }; + /** + * Set the pending data for clipboard injection. + * + * @returns `undefined` + * @static + */ + ZeroClipboard.setData = function() { + return _setData.apply(this, _args(arguments)); + }; + /** + * Clear the pending data for clipboard injection. + * If no `format` is provided, all pending data formats will be cleared. + * + * @returns `undefined` + * @static + */ + ZeroClipboard.clearData = function() { + return _clearData.apply(this, _args(arguments)); + }; + /** + * Get a copy of the pending data for clipboard injection. + * If no `format` is provided, a copy of ALL pending data formats will be returned. + * + * @returns `String` or `Object` + * @static + */ + ZeroClipboard.getData = function() { + return _getData.apply(this, _args(arguments)); + }; + /** + * Sets the current HTML object that the Flash object should overlay. This will put the global + * Flash object on top of the current element; depending on the setup, this may also set the + * pending clipboard text data as well as the Flash object's wrapping element's title attribute + * based on the underlying HTML element and ZeroClipboard configuration. + * + * @returns `undefined` + * @static + */ + ZeroClipboard.focus = ZeroClipboard.activate = function() { + return _focus.apply(this, _args(arguments)); + }; + /** + * Un-overlays the Flash object. This will put the global Flash object off-screen; depending on + * the setup, this may also unset the Flash object's wrapping element's title attribute based on + * the underlying HTML element and ZeroClipboard configuration. + * + * @returns `undefined` + * @static + */ + ZeroClipboard.blur = ZeroClipboard.deactivate = function() { + return _blur.apply(this, _args(arguments)); + }; + /** + * Returns the currently focused/"activated" HTML element that the Flash object is wrapping. + * + * @returns `HTMLElement` or `null` + * @static + */ + ZeroClipboard.activeElement = function() { + return _activeElement.apply(this, _args(arguments)); + }; + /** + * Keep track of the ZeroClipboard client instance counter. + */ + var _clientIdCounter = 0; + /** + * Keep track of the state of the client instances. + * + * Entry structure: + * _clientMeta[client.id] = { + * instance: client, + * elements: [], + * handlers: {} + * }; + */ + var _clientMeta = {}; + /** + * Keep track of the ZeroClipboard clipped elements counter. + */ + var _elementIdCounter = 0; + /** + * Keep track of the state of the clipped element relationships to clients. + * + * Entry structure: + * _elementMeta[element.zcClippingId] = [client1.id, client2.id]; + */ + var _elementMeta = {}; + /** + * Keep track of the state of the mouse event handlers for clipped elements. + * + * Entry structure: + * _mouseHandlers[element.zcClippingId] = { + * mouseover: function(event) {}, + * mouseout: function(event) {}, + * mouseenter: function(event) {}, + * mouseleave: function(event) {}, + * mousemove: function(event) {} + * }; + */ + var _mouseHandlers = {}; + /** + * Extending the ZeroClipboard configuration defaults for the Client module. + */ + _extend(_globalConfig, { + autoActivate: true + }); + /** + * The real constructor for `ZeroClipboard` client instances. + * @private + */ + var _clientConstructor = function(elements) { + var client = this; + client.id = "" + _clientIdCounter++; + _clientMeta[client.id] = { + instance: client, + elements: [], + handlers: {} + }; + if (elements) { + client.clip(elements); } - return this; + ZeroClipboard.on("*", function(event) { + return client.emit(event); + }); + ZeroClipboard.on("destroy", function() { + client.destroy(); + }); + ZeroClipboard.create(); }; - ZeroClipboard.prototype.on = function(eventName, func) { - var i, len, events, added = {}, handlers = _clientMeta[this.id] && _clientMeta[this.id].handlers; - if (typeof eventName === "string" && eventName) { - events = eventName.toLowerCase().split(/\s+/); - } else if (typeof eventName === "object" && eventName && typeof func === "undefined") { - for (i in eventName) { - if (eventName.hasOwnProperty(i) && typeof i === "string" && i && typeof eventName[i] === "function") { - this.on(i, eventName[i]); + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.on`. + * @private + */ + var _clientOn = function(eventType, listener) { + var i, len, events, added = {}, meta = _clientMeta[this.id], handlers = meta && meta.handlers; + if (!meta) { + throw new Error("Attempted to add new listener(s) to a destroyed ZeroClipboard client instance"); + } + if (typeof eventType === "string" && eventType) { + events = eventType.toLowerCase().split(/\s+/); + } else if (typeof eventType === "object" && eventType && typeof listener === "undefined") { + for (i in eventType) { + if (_hasOwn.call(eventType, i) && typeof i === "string" && i && typeof eventType[i] === "function") { + this.on(i, eventType[i]); } } } if (events && events.length) { for (i = 0, len = events.length; i < len; i++) { - eventName = events[i].replace(/^on/, ""); - added[eventName] = true; - if (!handlers[eventName]) { - handlers[eventName] = []; + eventType = events[i].replace(/^on/, ""); + added[eventType] = true; + if (!handlers[eventType]) { + handlers[eventType] = []; } - handlers[eventName].push(func); + handlers[eventType].push(listener); } - if (added.noflash && flashState.disabled) { - _receiveEvent.call(this, "noflash", {}); - } - if (added.wrongflash && flashState.outdated) { - _receiveEvent.call(this, "wrongflash", { - flashVersion: flashState.version + if (added.ready && _flashState.ready) { + this.emit({ + type: "ready", + client: this }); } - if (added.load && flashState.ready) { - _receiveEvent.call(this, "load", { - flashVersion: flashState.version - }); + if (added.error) { + for (i = 0, len = _flashStateErrorNames.length; i < len; i++) { + if (_flashState[_flashStateErrorNames[i].replace(/^flash-/, "")]) { + this.emit({ + type: "error", + name: _flashStateErrorNames[i], + client: this + }); + break; + } + } + if (_zcSwfVersion !== undefined && ZeroClipboard.version !== _zcSwfVersion) { + this.emit({ + type: "error", + name: "version-mismatch", + jsVersion: ZeroClipboard.version, + swfVersion: _zcSwfVersion + }); + } } } return this; }; - ZeroClipboard.prototype.off = function(eventName, func) { - var i, len, foundIndex, events, perEventHandlers, handlers = _clientMeta[this.id] && _clientMeta[this.id].handlers; + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.off`. + * @private + */ + var _clientOff = function(eventType, listener) { + var i, len, foundIndex, events, perEventHandlers, meta = _clientMeta[this.id], handlers = meta && meta.handlers; + if (!handlers) { + return this; + } if (arguments.length === 0) { - events = _objectKeys(handlers); - } else if (typeof eventName === "string" && eventName) { - events = eventName.split(/\s+/); - } else if (typeof eventName === "object" && eventName && typeof func === "undefined") { - for (i in eventName) { - if (eventName.hasOwnProperty(i) && typeof i === "string" && i && typeof eventName[i] === "function") { - this.off(i, eventName[i]); + events = _keys(handlers); + } else if (typeof eventType === "string" && eventType) { + events = eventType.split(/\s+/); + } else if (typeof eventType === "object" && eventType && typeof listener === "undefined") { + for (i in eventType) { + if (_hasOwn.call(eventType, i) && typeof i === "string" && i && typeof eventType[i] === "function") { + this.off(i, eventType[i]); } } } if (events && events.length) { for (i = 0, len = events.length; i < len; i++) { - eventName = events[i].toLowerCase().replace(/^on/, ""); - perEventHandlers = handlers[eventName]; + eventType = events[i].toLowerCase().replace(/^on/, ""); + perEventHandlers = handlers[eventType]; if (perEventHandlers && perEventHandlers.length) { - if (func) { - foundIndex = _inArray(func, perEventHandlers); + if (listener) { + foundIndex = perEventHandlers.indexOf(listener); while (foundIndex !== -1) { perEventHandlers.splice(foundIndex, 1); - foundIndex = _inArray(func, perEventHandlers, foundIndex); + foundIndex = perEventHandlers.indexOf(listener, foundIndex); } } else { - handlers[eventName].length = 0; + perEventHandlers.length = 0; } } } } return this; }; - ZeroClipboard.prototype.handlers = function(eventName) { - var prop, copy = null, handlers = _clientMeta[this.id] && _clientMeta[this.id].handlers; + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.handlers`. + * @private + */ + var _clientListeners = function(eventType) { + var copy = null, handlers = _clientMeta[this.id] && _clientMeta[this.id].handlers; if (handlers) { - if (typeof eventName === "string" && eventName) { - return handlers[eventName] ? handlers[eventName].slice(0) : null; - } - copy = {}; - for (prop in handlers) { - if (handlers.hasOwnProperty(prop) && handlers[prop]) { - copy[prop] = handlers[prop].slice(0); - } + if (typeof eventType === "string" && eventType) { + copy = handlers[eventType] ? handlers[eventType].slice(0) : []; + } else { + copy = _deepCopy(handlers); } } return copy; }; - var _dispatchClientCallbacks = function(eventName, context, args, async) { - var handlers = _clientMeta[this.id] && _clientMeta[this.id].handlers[eventName]; - if (handlers && handlers.length) { - var i, len, func, originalContext = context || this; - for (i = 0, len = handlers.length; i < len; i++) { - func = handlers[i]; - context = originalContext; - if (typeof func === "string" && typeof window[func] === "function") { - func = window[func]; - } - if (typeof func === "object" && func && typeof func.handleEvent === "function") { - context = func; - func = func.handleEvent; - } - if (typeof func === "function") { - _dispatchCallback(func, context, args, async); - } + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.emit`. + * @private + */ + var _clientEmit = function(event) { + if (_clientShouldEmit.call(this, event)) { + if (typeof event === "object" && event && typeof event.type === "string" && event.type) { + event = _extend({}, event); } + var eventCopy = _extend({}, _createEvent(event), { + client: this + }); + _clientDispatchCallbacks.call(this, eventCopy); } return this; }; - ZeroClipboard.prototype.clip = function(elements) { + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.clip`. + * @private + */ + var _clientClip = function(elements) { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to clip element(s) to a destroyed ZeroClipboard client instance"); + } elements = _prepClip(elements); for (var i = 0; i < elements.length; i++) { - if (elements.hasOwnProperty(i) && elements[i] && elements[i].nodeType === 1) { + if (_hasOwn.call(elements, i) && elements[i] && elements[i].nodeType === 1) { if (!elements[i].zcClippingId) { - elements[i].zcClippingId = "zcClippingId_" + elementIdCounter++; + elements[i].zcClippingId = "zcClippingId_" + _elementIdCounter++; _elementMeta[elements[i].zcClippingId] = [ this.id ]; if (_globalConfig.autoActivate === true) { - _addEventHandler(elements[i], "mouseover", _elementMouseOver); + _addMouseHandlers(elements[i]); } - } else if (_inArray(this.id, _elementMeta[elements[i].zcClippingId]) === -1) { + } else if (_elementMeta[elements[i].zcClippingId].indexOf(this.id) === -1) { _elementMeta[elements[i].zcClippingId].push(this.id); } - var clippedElements = _clientMeta[this.id].elements; - if (_inArray(elements[i], clippedElements) === -1) { + var clippedElements = _clientMeta[this.id] && _clientMeta[this.id].elements; + if (clippedElements.indexOf(elements[i]) === -1) { clippedElements.push(elements[i]); } } } return this; }; - ZeroClipboard.prototype.unclip = function(elements) { + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.unclip`. + * @private + */ + var _clientUnclip = function(elements) { var meta = _clientMeta[this.id]; - if (meta) { - var clippedElements = meta.elements; - var arrayIndex; - if (typeof elements === "undefined") { - elements = clippedElements.slice(0); - } else { - elements = _prepClip(elements); - } - for (var i = elements.length; i--; ) { - if (elements.hasOwnProperty(i) && elements[i] && elements[i].nodeType === 1) { + if (!meta) { + return this; + } + var clippedElements = meta.elements; + var arrayIndex; + if (typeof elements === "undefined") { + elements = clippedElements.slice(0); + } else { + elements = _prepClip(elements); + } + for (var i = elements.length; i--; ) { + if (_hasOwn.call(elements, i) && elements[i] && elements[i].nodeType === 1) { + arrayIndex = 0; + while ((arrayIndex = clippedElements.indexOf(elements[i], arrayIndex)) !== -1) { + clippedElements.splice(arrayIndex, 1); + } + var clientIds = _elementMeta[elements[i].zcClippingId]; + if (clientIds) { arrayIndex = 0; - while ((arrayIndex = _inArray(elements[i], clippedElements, arrayIndex)) !== -1) { - clippedElements.splice(arrayIndex, 1); + while ((arrayIndex = clientIds.indexOf(this.id, arrayIndex)) !== -1) { + clientIds.splice(arrayIndex, 1); } - var clientIds = _elementMeta[elements[i].zcClippingId]; - if (clientIds) { - arrayIndex = 0; - while ((arrayIndex = _inArray(this.id, clientIds, arrayIndex)) !== -1) { - clientIds.splice(arrayIndex, 1); - } - if (clientIds.length === 0) { - if (_globalConfig.autoActivate === true) { - _removeEventHandler(elements[i], "mouseover", _elementMouseOver); - } - delete elements[i].zcClippingId; + if (clientIds.length === 0) { + if (_globalConfig.autoActivate === true) { + _removeMouseHandlers(elements[i]); } + delete elements[i].zcClippingId; } } } } return this; }; - ZeroClipboard.prototype.elements = function() { + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.elements`. + * @private + */ + var _clientElements = function() { var meta = _clientMeta[this.id]; return meta && meta.elements ? meta.elements.slice(0) : []; }; - var _getAllClientsClippedToElement = function(element) { - var elementMetaId, clientIds, i, len, client, clients = []; - if (element && element.nodeType === 1 && (elementMetaId = element.zcClippingId) && _elementMeta.hasOwnProperty(elementMetaId)) { - clientIds = _elementMeta[elementMetaId]; - if (clientIds && clientIds.length) { - for (i = 0, len = clientIds.length; i < len; i++) { - client = _clientMeta[clientIds[i]].instance; - if (client && client instanceof ZeroClipboard) { - clients.push(client); - } - } - } + /** + * The underlying implementation of `ZeroClipboard.Client.prototype.destroy`. + * @private + */ + var _clientDestroy = function() { + if (!_clientMeta[this.id]) { + return; } - return clients; + this.unclip(); + this.off(); + delete _clientMeta[this.id]; }; - _globalConfig.hoverClass = "zeroclipboard-is-hover"; - _globalConfig.activeClass = "zeroclipboard-is-active"; - _globalConfig.trustedOrigins = null; - _globalConfig.allowScriptAccess = null; - _globalConfig.useNoCache = true; - _globalConfig.moviePath = "ZeroClipboard.swf"; - ZeroClipboard.detectFlashSupport = function() { - _deprecationWarning("ZeroClipboard.detectFlashSupport", _globalConfig.debug); - return _detectFlashSupport(); + /** + * Inspect an Event to see if the Client (`this`) should honor it for emission. + * @private + */ + var _clientShouldEmit = function(event) { + if (!(event && event.type)) { + return false; + } + if (event.client && event.client !== this) { + return false; + } + var meta = _clientMeta[this.id]; + var clippedEls = meta && meta.elements; + var hasClippedEls = !!clippedEls && clippedEls.length > 0; + var goodTarget = !event.target || hasClippedEls && clippedEls.indexOf(event.target) !== -1; + var goodRelTarget = event.relatedTarget && hasClippedEls && clippedEls.indexOf(event.relatedTarget) !== -1; + var goodClient = event.client && event.client === this; + if (!meta || !(goodTarget || goodRelTarget || goodClient)) { + return false; + } + return true; }; - ZeroClipboard.dispatch = function(eventName, args) { - if (typeof eventName === "string" && eventName) { - var cleanEventName = eventName.toLowerCase().replace(/^on/, ""); - if (cleanEventName) { - var clients = currentElement ? _getAllClientsClippedToElement(currentElement) : _getAllClients(); - for (var i = 0, len = clients.length; i < len; i++) { - _receiveEvent.call(clients[i], cleanEventName, args); + /** + * Handle the actual dispatching of events to a client instance. + * + * @returns `undefined` + * @private + */ + var _clientDispatchCallbacks = function(event) { + var meta = _clientMeta[this.id]; + if (!(typeof event === "object" && event && event.type && meta)) { + return; + } + var async = _shouldPerformAsync(event); + var wildcardTypeHandlers = meta && meta.handlers["*"] || []; + var specificTypeHandlers = meta && meta.handlers[event.type] || []; + var handlers = wildcardTypeHandlers.concat(specificTypeHandlers); + if (handlers && handlers.length) { + var i, len, func, context, eventCopy, originalContext = this; + for (i = 0, len = handlers.length; i < len; i++) { + func = handlers[i]; + context = originalContext; + if (typeof func === "string" && typeof _window[func] === "function") { + func = _window[func]; + } + if (typeof func === "object" && func && typeof func.handleEvent === "function") { + context = func; + func = func.handleEvent; + } + if (typeof func === "function") { + eventCopy = _extend({}, event); + _dispatchCallback(func, context, [ eventCopy ], async); } } } }; - ZeroClipboard.prototype.setHandCursor = function(enabled) { - _deprecationWarning("ZeroClipboard.prototype.setHandCursor", _globalConfig.debug); - enabled = typeof enabled === "boolean" ? enabled : !!enabled; - _setHandCursor(enabled); - _globalConfig.forceHandCursor = enabled; - return this; + /** + * Prepares the elements for clipping/unclipping. + * + * @returns An Array of elements. + * @private + */ + var _prepClip = function(elements) { + if (typeof elements === "string") { + elements = []; + } + return typeof elements.length !== "number" ? [ elements ] : elements; }; - ZeroClipboard.prototype.reposition = function() { - _deprecationWarning("ZeroClipboard.prototype.reposition", _globalConfig.debug); - return _reposition(); + /** + * Add a `mouseover` handler function for a clipped element. + * + * @returns `undefined` + * @private + */ + var _addMouseHandlers = function(element) { + if (!(element && element.nodeType === 1)) { + return; + } + var _suppressMouseEvents = function(event) { + if (!(event || (event = _window.event))) { + return; + } + if (event._source !== "js") { + event.stopImmediatePropagation(); + event.preventDefault(); + } + delete event._source; + }; + var _elementMouseOver = function(event) { + if (!(event || (event = _window.event))) { + return; + } + _suppressMouseEvents(event); + ZeroClipboard.focus(element); + }; + element.addEventListener("mouseover", _elementMouseOver, false); + element.addEventListener("mouseout", _suppressMouseEvents, false); + element.addEventListener("mouseenter", _suppressMouseEvents, false); + element.addEventListener("mouseleave", _suppressMouseEvents, false); + element.addEventListener("mousemove", _suppressMouseEvents, false); + _mouseHandlers[element.zcClippingId] = { + mouseover: _elementMouseOver, + mouseout: _suppressMouseEvents, + mouseenter: _suppressMouseEvents, + mouseleave: _suppressMouseEvents, + mousemove: _suppressMouseEvents + }; }; - ZeroClipboard.prototype.receiveEvent = function(eventName, args) { - _deprecationWarning("ZeroClipboard.prototype.receiveEvent", _globalConfig.debug); - if (typeof eventName === "string" && eventName) { - var cleanEventName = eventName.toLowerCase().replace(/^on/, ""); - if (cleanEventName) { - _receiveEvent.call(this, cleanEventName, args); + /** + * Remove a `mouseover` handler function for a clipped element. + * + * @returns `undefined` + * @private + */ + var _removeMouseHandlers = function(element) { + if (!(element && element.nodeType === 1)) { + return; + } + var mouseHandlers = _mouseHandlers[element.zcClippingId]; + if (!(typeof mouseHandlers === "object" && mouseHandlers)) { + return; + } + var key, val, mouseEvents = [ "move", "leave", "enter", "out", "over" ]; + for (var i = 0, len = mouseEvents.length; i < len; i++) { + key = "mouse" + mouseEvents[i]; + val = mouseHandlers[key]; + if (typeof val === "function") { + element.removeEventListener(key, val, false); } } + delete _mouseHandlers[element.zcClippingId]; + }; + /** + * Creates a new ZeroClipboard client instance. + * Optionally, auto-`clip` an element or collection of elements. + * + * @constructor + */ + ZeroClipboard._createClient = function() { + _clientConstructor.apply(this, _args(arguments)); + }; + /** + * Register an event listener to the client. + * + * @returns `this` + */ + ZeroClipboard.prototype.on = function() { + return _clientOn.apply(this, _args(arguments)); + }; + /** + * Unregister an event handler from the client. + * If no `listener` function/object is provided, it will unregister all handlers for the provided `eventType`. + * If no `eventType` is provided, it will unregister all handlers for every event type. + * + * @returns `this` + */ + ZeroClipboard.prototype.off = function() { + return _clientOff.apply(this, _args(arguments)); + }; + /** + * Retrieve event listeners for an `eventType` from the client. + * If no `eventType` is provided, it will retrieve all listeners for every event type. + * + * @returns array of listeners for the `eventType`; if no `eventType`, then a map/hash object of listeners for all event types; or `null` + */ + ZeroClipboard.prototype.handlers = function() { + return _clientListeners.apply(this, _args(arguments)); + }; + /** + * Event emission receiver from the Flash object for this client's registered JavaScript event listeners. + * + * @returns For the "copy" event, returns the Flash-friendly "clipData" object; otherwise `undefined`. + */ + ZeroClipboard.prototype.emit = function() { + return _clientEmit.apply(this, _args(arguments)); }; - ZeroClipboard.prototype.setCurrent = function(element) { - _deprecationWarning("ZeroClipboard.prototype.setCurrent", _globalConfig.debug); - ZeroClipboard.activate(element); + /** + * Register clipboard actions for new element(s) to the client. + * + * @returns `this` + */ + ZeroClipboard.prototype.clip = function() { + return _clientClip.apply(this, _args(arguments)); + }; + /** + * Unregister the clipboard actions of previously registered element(s) on the page. + * If no elements are provided, ALL registered elements will be unregistered. + * + * @returns `this` + */ + ZeroClipboard.prototype.unclip = function() { + return _clientUnclip.apply(this, _args(arguments)); + }; + /** + * Get all of the elements to which this client is clipped. + * + * @returns array of clipped elements + */ + ZeroClipboard.prototype.elements = function() { + return _clientElements.apply(this, _args(arguments)); + }; + /** + * Self-destruct and clean up everything for a single client. + * This will NOT destroy the embedded Flash object. + * + * @returns `undefined` + */ + ZeroClipboard.prototype.destroy = function() { + return _clientDestroy.apply(this, _args(arguments)); + }; + /** + * Stores the pending plain text to inject into the clipboard. + * + * @returns `this` + */ + ZeroClipboard.prototype.setText = function(text) { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to set pending clipboard data from a destroyed ZeroClipboard client instance"); + } + ZeroClipboard.setData("text/plain", text); return this; }; - ZeroClipboard.prototype.resetBridge = function() { - _deprecationWarning("ZeroClipboard.prototype.resetBridge", _globalConfig.debug); - ZeroClipboard.deactivate(); + /** + * Stores the pending HTML text to inject into the clipboard. + * + * @returns `this` + */ + ZeroClipboard.prototype.setHtml = function(html) { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to set pending clipboard data from a destroyed ZeroClipboard client instance"); + } + ZeroClipboard.setData("text/html", html); return this; }; - ZeroClipboard.prototype.setTitle = function(newTitle) { - _deprecationWarning("ZeroClipboard.prototype.setTitle", _globalConfig.debug); - newTitle = newTitle || _globalConfig.title || currentElement && currentElement.getAttribute("title"); - if (newTitle) { - var htmlBridge = _getHtmlBridge(flashState.bridge); - if (htmlBridge) { - htmlBridge.setAttribute("title", newTitle); - } - } + /** + * Stores the pending rich text (RTF) to inject into the clipboard. + * + * @returns `this` + */ + ZeroClipboard.prototype.setRichText = function(richText) { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to set pending clipboard data from a destroyed ZeroClipboard client instance"); + } + ZeroClipboard.setData("application/rtf", richText); return this; }; - ZeroClipboard.setDefaults = function(options) { - _deprecationWarning("ZeroClipboard.setDefaults", _globalConfig.debug); - ZeroClipboard.config(options); - }; - ZeroClipboard.prototype.addEventListener = function(eventName, func) { - _deprecationWarning("ZeroClipboard.prototype.addEventListener", _globalConfig.debug); - return this.on(eventName, func); - }; - ZeroClipboard.prototype.removeEventListener = function(eventName, func) { - _deprecationWarning("ZeroClipboard.prototype.removeEventListener", _globalConfig.debug); - return this.off(eventName, func); - }; - ZeroClipboard.prototype.ready = function() { - _deprecationWarning("ZeroClipboard.prototype.ready", _globalConfig.debug); - return flashState.ready === true; - }; - var _receiveEvent = function(eventName, args) { - eventName = eventName.toLowerCase().replace(/^on/, ""); - var cleanVersion = args && args.flashVersion && _parseFlashVersion(args.flashVersion) || null; - var element = currentElement; - var performCallbackAsync = true; - switch (eventName) { - case "load": - if (cleanVersion) { - if (!_isFlashVersionSupported(cleanVersion)) { - _receiveEvent.call(this, "onWrongFlash", { - flashVersion: cleanVersion - }); - return; - } - flashState.outdated = false; - flashState.ready = true; - flashState.version = cleanVersion; - } - break; - - case "wrongflash": - if (cleanVersion && !_isFlashVersionSupported(cleanVersion)) { - flashState.outdated = true; - flashState.ready = false; - flashState.version = cleanVersion; - } - break; - - case "mouseover": - _addClass(element, _globalConfig.hoverClass); - break; - - case "mouseout": - if (_globalConfig.autoActivate === true) { - ZeroClipboard.deactivate(); - } - break; - - case "mousedown": - _addClass(element, _globalConfig.activeClass); - break; - - case "mouseup": - _removeClass(element, _globalConfig.activeClass); - break; - - case "datarequested": - var targetId = element.getAttribute("data-clipboard-target"), targetEl = !targetId ? null : document.getElementById(targetId); - if (targetEl) { - var textContent = targetEl.value || targetEl.textContent || targetEl.innerText; - if (textContent) { - this.setText(textContent); - } - } else { - var defaultText = element.getAttribute("data-clipboard-text"); - if (defaultText) { - this.setText(defaultText); - } - } - performCallbackAsync = false; - break; - - case "complete": - _deleteOwnProperties(_clipData); - break; - } - var context = element; - var eventArgs = [ this, args ]; - return _dispatchClientCallbacks.call(this, eventName, context, eventArgs, performCallbackAsync); + /** + * Stores the pending data to inject into the clipboard. + * + * @returns `this` + */ + ZeroClipboard.prototype.setData = function() { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to set pending clipboard data from a destroyed ZeroClipboard client instance"); + } + ZeroClipboard.setData.apply(this, _args(arguments)); + return this; + }; + /** + * Clears the pending data to inject into the clipboard. + * If no `format` is provided, all pending data formats will be cleared. + * + * @returns `this` + */ + ZeroClipboard.prototype.clearData = function() { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to clear pending clipboard data from a destroyed ZeroClipboard client instance"); + } + ZeroClipboard.clearData.apply(this, _args(arguments)); + return this; + }; + /** + * Gets a copy of the pending data to inject into the clipboard. + * If no `format` is provided, a copy of ALL pending data formats will be returned. + * + * @returns `String` or `Object` + */ + ZeroClipboard.prototype.getData = function() { + if (!_clientMeta[this.id]) { + throw new Error("Attempted to get pending clipboard data from a destroyed ZeroClipboard client instance"); + } + return ZeroClipboard.getData.apply(this, _args(arguments)); }; if (typeof define === "function" && define.amd) { - define([ "require", "exports", "module" ], function(require, exports, module) { - _amdModuleId = module && module.id || null; + define(function() { return ZeroClipboard; }); } else if (typeof module === "object" && module && typeof module.exports === "object" && module.exports) { - _cjsModuleId = module.id || null; module.exports = ZeroClipboard; } else { window.ZeroClipboard = ZeroClipboard; } -})(); \ No newline at end of file +})(function() { + return this || window; +}()); \ No newline at end of file diff --git a/src/UI/Mixins/CopyToClipboard.js b/src/UI/Mixins/CopyToClipboard.js index 23c29b4b8..d3b01e628 100644 --- a/src/UI/Mixins/CopyToClipboard.js +++ b/src/UI/Mixins/CopyToClipboard.js @@ -3,17 +3,20 @@ var StatusModel = require('../System/StatusModel'); var ZeroClipboard = require('zero.clipboard'); var Messenger = require('../Shared/Messenger'); -module.exports = (function(){ - $.fn.copyToClipboard = function(input){ - var moviePath = StatusModel.get('urlBase') + '/Content/zero.clipboard.swf'; - var client = new ZeroClipboard(this, {moviePath : moviePath}); - client.on('load', function(client){ - client.on('dataRequested', function(client){ - client.setText(input.val()); - }); - client.on('complete', function(){ - Messenger.show({message : 'Copied text to clipboard'}); - }); +$.fn.copyToClipboard = function(input) { + + ZeroClipboard.config({ + swfPath: StatusModel.get('urlBase') + '/Content/zero.clipboard.swf' + }); + + var client = new ZeroClipboard(this); + + client.on('ready', function(e) { + client.on('copy', function(e) { + e.clipboardData.setData("text/plain", input.val()); + }); + client.on('aftercopy', function() { + Messenger.show({message : 'Copied text to clipboard'}); }); - }; -}).call(this); \ No newline at end of file + }); +}; \ No newline at end of file diff --git a/src/UI/Settings/General/GeneralView.js b/src/UI/Settings/General/GeneralView.js index 157949ad1..9b061afa7 100644 --- a/src/UI/Settings/General/GeneralView.js +++ b/src/UI/Settings/General/GeneralView.js @@ -1,123 +1,121 @@ -'use strict'; -define( - [ - 'vent', - 'marionette', - 'Commands/CommandController', - 'Mixins/AsModelBoundView', - 'Mixins/AsValidatedView', - 'Mixins/CopyToClipboard' - ], function (vent, Marionette, CommandController, AsModelBoundView, AsValidatedView) { - var view = Marionette.ItemView.extend({ - template: 'Settings/General/GeneralViewTemplate', - - events: { - 'change .x-auth' : '_setAuthOptionsVisibility', - 'change .x-ssl' : '_setSslOptionsVisibility', - 'click .x-reset-api-key' : '_resetApiKey', - 'change .x-update-mechanism' : '_setScriptGroupVisibility' - }, - - ui: { - authToggle : '.x-auth', - authOptions : '.x-auth-options', - sslToggle : '.x-ssl', - sslOptions : '.x-ssl-options', - resetApiKey : '.x-reset-api-key', - copyApiKey : '.x-copy-api-key', - apiKeyInput : '.x-api-key', - updateMechanism : '.x-update-mechanism', - scriptGroup : '.x-script-group' - }, - - initialize: function () { - this.listenTo(vent, vent.Events.CommandComplete, this._commandComplete); - }, - - onRender: function(){ - if(this.ui.authToggle.val() === 'none'){ - this.ui.authOptions.hide(); - } - - if(!this.ui.sslToggle.prop('checked')){ - this.ui.sslOptions.hide(); - } - - if (!this._showScriptGroup()) { - this.ui.scriptGroup.hide(); - } - - CommandController.bindToCommand({ - element: this.ui.resetApiKey, - command: { - name: 'resetApiKey' - } - }); - }, - - onShow: function () { - this.ui.copyApiKey.copyToClipboard(this.ui.apiKeyInput); - }, - - _setAuthOptionsVisibility: function () { - - var showAuthOptions = this.ui.authToggle.val() !== 'none'; - - if (showAuthOptions) { - this.ui.authOptions.slideDown(); - } - - else { - this.ui.authOptions.slideUp(); - } - }, - - _setSslOptionsVisibility: function () { - - var showSslOptions = this.ui.sslToggle.prop('checked'); - - if (showSslOptions) { - this.ui.sslOptions.slideDown(); - } - - else { - this.ui.sslOptions.slideUp(); - } - }, - - _resetApiKey: function () { - if (window.confirm('Reset API Key?')) { - CommandController.Execute('resetApiKey', { - name : 'resetApiKey' - }); - } - }, - - _commandComplete: function (options) { - if (options.command.get('name') === 'resetapikey') { - this.model.fetch(); - } - }, - - _setScriptGroupVisibility: function () { - - if (this._showScriptGroup()) { - this.ui.scriptGroup.slideDown(); - } - - else { - this.ui.scriptGroup.slideUp(); - } - }, - - _showScriptGroup: function () { - return this.ui.updateMechanism.val() === 'script'; + +var vent = require('../../vent'); +var Marionette = require('marionette'); +var CommandController = require('../../Commands/CommandController'); +var AsModelBoundView = require('../../Mixins/AsModelBoundView'); +var AsValidatedView = require('../../Mixins/AsValidatedView'); + +require('../../Mixins/CopyToClipboard'); + +var view = Marionette.ItemView.extend({ + template: 'Settings/General/GeneralViewTemplate', + + events: { + 'change .x-auth' : '_setAuthOptionsVisibility', + 'change .x-ssl' : '_setSslOptionsVisibility', + 'click .x-reset-api-key' : '_resetApiKey', + 'change .x-update-mechanism' : '_setScriptGroupVisibility' + }, + + ui: { + authToggle : '.x-auth', + authOptions : '.x-auth-options', + sslToggle : '.x-ssl', + sslOptions : '.x-ssl-options', + resetApiKey : '.x-reset-api-key', + copyApiKey : '.x-copy-api-key', + apiKeyInput : '.x-api-key', + updateMechanism : '.x-update-mechanism', + scriptGroup : '.x-script-group' + }, + + initialize: function () { + this.listenTo(vent, vent.Events.CommandComplete, this._commandComplete); + }, + + onRender: function(){ + if(this.ui.authToggle.val() === 'none'){ + this.ui.authOptions.hide(); + } + + if(!this.ui.sslToggle.prop('checked')){ + this.ui.sslOptions.hide(); + } + + if (!this._showScriptGroup()) { + this.ui.scriptGroup.hide(); + } + + CommandController.bindToCommand({ + element: this.ui.resetApiKey, + command: { + name: 'resetApiKey' } }); + }, - AsModelBoundView.call(view); - AsValidatedView.call(view); + onShow: function () { + this.ui.copyApiKey.copyToClipboard(this.ui.apiKeyInput); + }, - return view; - }); + _setAuthOptionsVisibility: function () { + + var showAuthOptions = this.ui.authToggle.val() !== 'none'; + + if (showAuthOptions) { + this.ui.authOptions.slideDown(); + } + + else { + this.ui.authOptions.slideUp(); + } + }, + + _setSslOptionsVisibility: function () { + + var showSslOptions = this.ui.sslToggle.prop('checked'); + + if (showSslOptions) { + this.ui.sslOptions.slideDown(); + } + + else { + this.ui.sslOptions.slideUp(); + } + }, + + _resetApiKey: function () { + if (window.confirm('Reset API Key?')) { + CommandController.Execute('resetApiKey', { + name : 'resetApiKey' + }); + } + }, + + _commandComplete: function (options) { + if (options.command.get('name') === 'resetapikey') { + this.model.fetch(); + } + }, + + _setScriptGroupVisibility: function () { + + if (this._showScriptGroup()) { + this.ui.scriptGroup.slideDown(); + } + + else { + this.ui.scriptGroup.slideUp(); + } + }, + + _showScriptGroup: function () { + return this.ui.updateMechanism.val() === 'script'; + } +}); + +AsModelBoundView.call(view); +AsValidatedView.call(view); + +module.exports = view;