viewer.js 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479
  1. /*!
  2. * Viewer v1.0.0
  3. * https://github.com/fengyuanchen/viewer
  4. *
  5. * Copyright (c) 2015-2018 Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2018-04-01T06:11:06.751Z
  9. */
  10. (function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('jquery')) :
  12. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  13. (factory(global.jQuery));
  14. }(this, (function ($) { 'use strict';
  15. $ = $ && $.hasOwnProperty('default') ? $['default'] : $;
  16. var DEFAULTS = {
  17. // Enable inline mode
  18. inline: false,
  19. // Show the button on the top-right of the viewer
  20. button: true,
  21. // Show the navbar
  22. navbar: true,
  23. // Show the title
  24. title: true,
  25. // Show the toolbar
  26. toolbar: true,
  27. // Show the tooltip with image ratio (percentage) when zoom in or zoom out
  28. tooltip: true,
  29. // Enable to move the image
  30. movable: true,
  31. // Enable to zoom the image
  32. zoomable: true,
  33. // Enable to rotate the image
  34. rotatable: true,
  35. // Enable to scale the image
  36. scalable: true,
  37. // Enable CSS3 Transition for some special elements
  38. transition: true,
  39. // Enable to request fullscreen when play
  40. fullscreen: true,
  41. // The amount of time to delay between automatically cycling an image when playing.
  42. interval: 2000,
  43. // Enable keyboard support
  44. keyboard: true,
  45. // Enable a modal backdrop, specify `static` for a backdrop which doesn't close the modal on click
  46. backdrop: true,
  47. // Indicate if show a loading spinner when load image or not.
  48. loading: true,
  49. // Indicate if enable loop viewing or not.
  50. loop: true,
  51. // Min width of the viewer in inline mode
  52. minWidth: 200,
  53. // Min height of the viewer in inline mode
  54. minHeight: 100,
  55. // Define the ratio when zoom the image by wheeling mouse
  56. zoomRatio: 0.1,
  57. // Define the min ratio of the image when zoom out
  58. minZoomRatio: 0.01,
  59. // Define the max ratio of the image when zoom in
  60. maxZoomRatio: 100,
  61. // Define the CSS `z-index` value of viewer in modal mode.
  62. zIndex: 2015,
  63. // Define the CSS `z-index` value of viewer in inline mode.
  64. zIndexInline: 0,
  65. // Define where to get the original image URL for viewing
  66. // Type: String (an image attribute) or Function (should return an image URL)
  67. url: 'src',
  68. url_original:'src',
  69. // Define where to put the viewer in modal mode.
  70. // Type: String | Element
  71. container: 'body',
  72. // Filter the images for viewing.
  73. // Type: Function (return true if the image is viewable)
  74. filter: null,
  75. // Event shortcuts
  76. ready: null,
  77. show: null,
  78. shown: null,
  79. hide: null,
  80. hidden: null,
  81. view: null,
  82. viewed: null,
  83. next:null
  84. };
  85. var TEMPLATE = '<div class="viewer-container" touch-action="none">' +
  86. '<div class="viewer-canvas"></div>' +
  87. '<div class="viewer-footer">' +
  88. '<div class="viewer-toolbar">'+
  89. '<div class="viewer-title"></div>'+'</div>' +
  90. '<div class="viewer-navbar" style="display: none">' + '<ul class="viewer-list"></ul>' + '</div>' + '</div>' +
  91. '<div class="viewer-tooltip"></div>' +
  92. '<div role="button" class="viewer-button" data-action="mix"></div>' + '<div class="viewer-player"></div>' + '</div>';
  93. var IN_BROWSER = typeof window !== 'undefined';
  94. var WINDOW = IN_BROWSER ? window : {};
  95. var NAMESPACE = 'viewer';
  96. // Actions
  97. var ACTION_MOVE = 'move';
  98. var ACTION_SWITCH = 'switch';
  99. var ACTION_ZOOM = 'zoom';
  100. // Classes
  101. var CLASS_ACTIVE = NAMESPACE + '-active';
  102. var CLASS_CLOSE = NAMESPACE + '-close';
  103. var CLASS_FADE = NAMESPACE + '-fade';
  104. var CLASS_FIXED = NAMESPACE + '-fixed';
  105. var CLASS_FULLSCREEN = NAMESPACE + '-fullscreen';
  106. var CLASS_FULLSCREEN_EXIT = NAMESPACE + '-fullscreen-exit';
  107. var CLASS_HIDE = NAMESPACE + '-hide';
  108. var CLASS_HIDE_MD_DOWN = NAMESPACE + '-hide-md-down';
  109. var CLASS_HIDE_SM_DOWN = NAMESPACE + '-hide-sm-down';
  110. var CLASS_HIDE_XS_DOWN = NAMESPACE + '-hide-xs-down';
  111. var CLASS_IN = NAMESPACE + '-in';
  112. var CLASS_INVISIBLE = NAMESPACE + '-invisible';
  113. var CLASS_LOADING = NAMESPACE + '-loading';
  114. var CLASS_MOVE = NAMESPACE + '-move';
  115. var CLASS_OPEN = NAMESPACE + '-open';
  116. var CLASS_SHOW = NAMESPACE + '-show';
  117. var CLASS_TRANSITION = NAMESPACE + '-transition';
  118. // Events
  119. var EVENT_READY = 'ready';
  120. var EVENT_SHOW = 'show';
  121. var EVENT_SHOWN = 'shown';
  122. var EVENT_NEXT='next';
  123. var EVENT_HIDE = 'hide';
  124. var EVENT_HIDDEN = 'hidden';
  125. var EVENT_VIEW = 'view';
  126. var EVENT_VIEWED = 'viewed';
  127. var EVENT_CLICK = 'click';
  128. var EVENT_DRAG_START = 'dragstart';
  129. var EVENT_KEY_DOWN = 'keydown';
  130. var EVENT_LOAD = 'load';
  131. var EVENT_POINTER_DOWN = WINDOW.PointerEvent ? 'pointerdown' : 'touchstart mousedown';
  132. var EVENT_POINTER_MOVE = WINDOW.PointerEvent ? 'pointermove' : 'touchmove mousemove';
  133. var EVENT_POINTER_UP = WINDOW.PointerEvent ? 'pointerup pointercancel' : 'touchend touchcancel mouseup';
  134. var EVENT_RESIZE = 'resize';
  135. var EVENT_TRANSITION_END = 'transitionend';
  136. var EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll';
  137. //var BUTTONS = ['zoom-in', 'zoom-out', 'one-to-one', 'reset', 'prev', 'play', 'next', 'rotate-left', 'rotate-right', 'flip-horizontal', 'flip-vertical'];
  138. //plays
  139. var BUTTONS = [ 'one-to-ones','one-to-ones2','enlarge','zoom-ins', 'zoom-outs', 'prevs', 'nexts',];
  140. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  141. return typeof obj;
  142. } : function (obj) {
  143. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  144. };
  145. var classCallCheck = function (instance, Constructor) {
  146. if (!(instance instanceof Constructor)) {
  147. throw new TypeError("Cannot call a class as a function");
  148. }
  149. };
  150. var createClass = function () {
  151. function defineProperties(target, props) {
  152. for (var i = 0; i < props.length; i++) {
  153. var descriptor = props[i];
  154. descriptor.enumerable = descriptor.enumerable || false;
  155. descriptor.configurable = true;
  156. if ("value" in descriptor) descriptor.writable = true;
  157. Object.defineProperty(target, descriptor.key, descriptor);
  158. }
  159. }
  160. return function (Constructor, protoProps, staticProps) {
  161. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  162. if (staticProps) defineProperties(Constructor, staticProps);
  163. return Constructor;
  164. };
  165. }();
  166. /**
  167. * Check if the given value is a string.
  168. * @param {*} value - The value to check.
  169. * @returns {boolean} Returns `true` if the given value is a string, else `false`.
  170. */
  171. function isString(value) {
  172. return typeof value === 'string';
  173. }
  174. /**
  175. * Check if the given value is not a number.
  176. */
  177. var isNaN = Number.isNaN || WINDOW.isNaN;
  178. /**
  179. * Check if the given value is a number.
  180. * @param {*} value - The value to check.
  181. * @returns {boolean} Returns `true` if the given value is a number, else `false`.
  182. */
  183. function isNumber(value) {
  184. return typeof value === 'number' && !isNaN(value);
  185. }
  186. /**
  187. * Check if the given value is undefined.
  188. * @param {*} value - The value to check.
  189. * @returns {boolean} Returns `true` if the given value is undefined, else `false`.
  190. */
  191. function isUndefined(value) {
  192. return typeof value === 'undefined';
  193. }
  194. /**
  195. * Check if the given value is an object.
  196. * @param {*} value - The value to check.
  197. * @returns {boolean} Returns `true` if the given value is an object, else `false`.
  198. */
  199. function isObject(value) {
  200. return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null;
  201. }
  202. var hasOwnProperty = Object.prototype.hasOwnProperty;
  203. /**
  204. * Check if the given value is a plain object.
  205. * @param {*} value - The value to check.
  206. * @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
  207. */
  208. function isPlainObject(value) {
  209. if (!isObject(value)) {
  210. return false;
  211. }
  212. try {
  213. var _constructor = value.constructor;
  214. var prototype = _constructor.prototype;
  215. return _constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
  216. } catch (e) {
  217. return false;
  218. }
  219. }
  220. /**
  221. * Check if the given value is a function.
  222. * @param {*} value - The value to check.
  223. * @returns {boolean} Returns `true` if the given value is a function, else `false`.
  224. */
  225. function isFunction(value) {
  226. return typeof value === 'function';
  227. }
  228. /**
  229. * Iterate the given data.
  230. * @param {*} data - The data to iterate.
  231. * @param {Function} callback - The process function for each element.
  232. * @returns {*} The original data.
  233. */
  234. function forEach(data, callback) {
  235. if (data && isFunction(callback)) {
  236. if (Array.isArray(data) || isNumber(data.length) /* array-like */) {
  237. var length = data.length;
  238. var i = void 0;
  239. for (i = 0; i < length; i += 1) {
  240. if (callback.call(data, data[i], i, data) === false) {
  241. break;
  242. }
  243. }
  244. } else if (isObject(data)) {
  245. Object.keys(data).forEach(function (key) {
  246. callback.call(data, data[key], key, data);
  247. });
  248. }
  249. }
  250. return data;
  251. }
  252. /**
  253. * Extend the given object.
  254. * @param {*} obj - The object to be extended.
  255. * @param {*} args - The rest objects which will be merged to the first object.
  256. * @returns {Object} The extended object.
  257. */
  258. var assign = Object.assign || function assign(obj) {
  259. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  260. args[_key - 1] = arguments[_key];
  261. }
  262. if (isObject(obj) && args.length > 0) {
  263. args.forEach(function (arg) {
  264. if (isObject(arg)) {
  265. Object.keys(arg).forEach(function (key) {
  266. obj[key] = arg[key];
  267. });
  268. }
  269. });
  270. }
  271. return obj;
  272. };
  273. var REGEXP_SUFFIX = /^(?:width|height|left|top|marginLeft|marginTop)$/;
  274. /**
  275. * Apply styles to the given element.
  276. * @param {Element} element - The target element.
  277. * @param {Object} styles - The styles for applying.
  278. */
  279. function setStyle(element, styles) {
  280. var style = element.style;
  281. forEach(styles, function (value, property) {
  282. if (REGEXP_SUFFIX.test(property) && isNumber(value)) {
  283. value += 'px';
  284. }
  285. style[property] = value;
  286. });
  287. }
  288. /**
  289. * Check if the given element has a special class.
  290. * @param {Element} element - The element to check.
  291. * @param {string} value - The class to search.
  292. * @returns {boolean} Returns `true` if the special class was found.
  293. */
  294. function hasClass(element, value) {
  295. return element.classList ? element.classList.contains(value) : element.className.indexOf(value) > -1;
  296. }
  297. /**
  298. * Add classes to the given element.
  299. * @param {Element} element - The target element.
  300. * @param {string} value - The classes to be added.
  301. */
  302. function addClass(element, value) {
  303. if (!value) {
  304. return;
  305. }
  306. if (isNumber(element.length)) {
  307. forEach(element, function (elem) {
  308. addClass(elem, value);
  309. });
  310. return;
  311. }
  312. if (element.classList) {
  313. element.classList.add(value);
  314. return;
  315. }
  316. var className = element.className.trim();
  317. if (!className) {
  318. element.className = value;
  319. } else if (className.indexOf(value) < 0) {
  320. element.className = className + ' ' + value;
  321. }
  322. }
  323. /**
  324. * Remove classes from the given element.
  325. * @param {Element} element - The target element.
  326. * @param {string} value - The classes to be removed.
  327. */
  328. function removeClass(element, value) {
  329. if (!value) {
  330. return;
  331. }
  332. if (isNumber(element.length)) {
  333. forEach(element, function (elem) {
  334. removeClass(elem, value);
  335. });
  336. return;
  337. }
  338. if (element.classList) {
  339. element.classList.remove(value);
  340. return;
  341. }
  342. if (element.className.indexOf(value) >= 0) {
  343. element.className = element.className.replace(value, '');
  344. }
  345. }
  346. /**
  347. * Add or remove classes from the given element.
  348. * @param {Element} element - The target element.
  349. * @param {string} value - The classes to be toggled.
  350. * @param {boolean} added - Add only.
  351. */
  352. function toggleClass(element, value, added) {
  353. if (!value) {
  354. return;
  355. }
  356. if (isNumber(element.length)) {
  357. forEach(element, function (elem) {
  358. toggleClass(elem, value, added);
  359. });
  360. return;
  361. }
  362. // IE10-11 doesn't support the second parameter of `classList.toggle`
  363. if (added) {
  364. addClass(element, value);
  365. } else {
  366. removeClass(element, value);
  367. }
  368. }
  369. var REGEXP_HYPHENATE = /([a-z\d])([A-Z])/g;
  370. /**
  371. * Transform the given string from camelCase to kebab-case
  372. * @param {string} value - The value to transform.
  373. * @returns {string} The transformed value.
  374. */
  375. function hyphenate(value) {
  376. return value.replace(REGEXP_HYPHENATE, '$1-$2').toLowerCase();
  377. }
  378. /**
  379. * Get data from the given element.
  380. * @param {Element} element - The target element.
  381. * @param {string} name - The data key to get.
  382. * @returns {string} The data value.
  383. */
  384. function getData(element, name) {
  385. if (isObject(element[name])) {
  386. return element[name];
  387. } else if (element.dataset) {
  388. return element.dataset[name];
  389. }
  390. return element.getAttribute('data-' + hyphenate(name));
  391. }
  392. /**
  393. * Set data to the given element.
  394. * @param {Element} element - The target element.
  395. * @param {string} name - The data key to set.
  396. * @param {string} data - The data value.
  397. */
  398. function setData(element, name, data) {
  399. if (isObject(data)) {
  400. element[name] = data;
  401. } else if (element.dataset) {
  402. element.dataset[name] = data;
  403. } else {
  404. element.setAttribute('data-' + hyphenate(name), data);
  405. }
  406. }
  407. /**
  408. * Remove data from the given element.
  409. * @param {Element} element - The target element.
  410. * @param {string} name - The data key to remove.
  411. */
  412. function removeData(element, name) {
  413. if (isObject(element[name])) {
  414. try {
  415. delete element[name];
  416. } catch (e) {
  417. element[name] = undefined;
  418. }
  419. } else if (element.dataset) {
  420. // #128 Safari not allows to delete dataset property
  421. try {
  422. delete element.dataset[name];
  423. } catch (e) {
  424. element.dataset[name] = undefined;
  425. }
  426. } else {
  427. element.removeAttribute('data-' + hyphenate(name));
  428. }
  429. }
  430. var REGEXP_SPACES = /\s\s*/;
  431. var onceSupported = function () {
  432. var supported = false;
  433. if (IN_BROWSER) {
  434. var once = false;
  435. var listener = function listener() {};
  436. var options = Object.defineProperty({}, 'once', {
  437. get: function get$$1() {
  438. supported = true;
  439. return once;
  440. },
  441. /**
  442. * This setter can fix a `TypeError` in strict mode
  443. * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only}
  444. * @param {boolean} value - The value to set
  445. */
  446. set: function set$$1(value) {
  447. once = value;
  448. }
  449. });
  450. WINDOW.addEventListener('test', listener, options);
  451. WINDOW.removeEventListener('test', listener, options);
  452. }
  453. return supported;
  454. }();
  455. /**
  456. * Remove event listener from the target element.
  457. * @param {Element} element - The event target.
  458. * @param {string} type - The event type(s).
  459. * @param {Function} listener - The event listener.
  460. * @param {Object} options - The event options.
  461. */
  462. function removeListener(element, type, listener) {
  463. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  464. var handler = listener;
  465. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  466. if (!onceSupported) {
  467. var listeners = element.listeners;
  468. if (listeners && listeners[event] && listeners[event][listener]) {
  469. handler = listeners[event][listener];
  470. delete listeners[event][listener];
  471. if (Object.keys(listeners[event]).length === 0) {
  472. delete listeners[event];
  473. }
  474. if (Object.keys(listeners).length === 0) {
  475. delete element.listeners;
  476. }
  477. }
  478. }
  479. element.removeEventListener(event, handler, options);
  480. });
  481. }
  482. /**
  483. * Add event listener to the target element.
  484. * @param {Element} element - The event target.
  485. * @param {string} type - The event type(s).
  486. * @param {Function} listener - The event listener.
  487. * @param {Object} options - The event options.
  488. */
  489. function addListener(element, type, listener) {
  490. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  491. var _handler = listener;
  492. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  493. if (options.once && !onceSupported) {
  494. var _element$listeners = element.listeners,
  495. listeners = _element$listeners === undefined ? {} : _element$listeners;
  496. _handler = function handler() {
  497. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  498. args[_key2] = arguments[_key2];
  499. }
  500. delete listeners[event][listener];
  501. element.removeEventListener(event, _handler, options);
  502. listener.apply(element, args);
  503. };
  504. if (!listeners[event]) {
  505. listeners[event] = {};
  506. }
  507. if (listeners[event][listener]) {
  508. element.removeEventListener(event, listeners[event][listener], options);
  509. }
  510. listeners[event][listener] = _handler;
  511. element.listeners = listeners;
  512. }
  513. element.addEventListener(event, _handler, options);
  514. });
  515. }
  516. /**
  517. * Dispatch event on the target element.
  518. * @param {Element} element - The event target.
  519. * @param {string} type - The event type(s).
  520. * @param {Object} data - The additional event data.
  521. * @returns {boolean} Indicate if the event is default prevented or not.
  522. */
  523. function dispatchEvent(element, type, data) {
  524. var event = void 0;
  525. // Event and CustomEvent on IE9-11 are global objects, not constructors
  526. if (isFunction(Event) && isFunction(CustomEvent)) {
  527. event = new CustomEvent(type, {
  528. detail: data,
  529. bubbles: true,
  530. cancelable: true
  531. });
  532. } else {
  533. event = document.createEvent('CustomEvent');
  534. event.initCustomEvent(type, true, true, data);
  535. }
  536. return element.dispatchEvent(event);
  537. }
  538. /**
  539. * Get the offset base on the document.
  540. * @param {Element} element - The target element.
  541. * @returns {Object} The offset data.
  542. */
  543. function getOffset(element) {
  544. var box = element.getBoundingClientRect();
  545. return {
  546. left: box.left + (window.pageXOffset - document.documentElement.clientLeft),
  547. top: box.top + (window.pageYOffset - document.documentElement.clientTop)
  548. };
  549. }
  550. /**
  551. * Get transforms base on the given object.
  552. * @param {Object} obj - The target object.
  553. * @returns {string} A string contains transform values.
  554. */
  555. function getTransforms(_ref) {
  556. var rotate = _ref.rotate,
  557. scaleX = _ref.scaleX,
  558. scaleY = _ref.scaleY,
  559. translateX = _ref.translateX,
  560. translateY = _ref.translateY;
  561. var values = [];
  562. if (isNumber(translateX) && translateX !== 0) {
  563. values.push('translateX(' + translateX + 'px)');
  564. }
  565. if (isNumber(translateY) && translateY !== 0) {
  566. values.push('translateY(' + translateY + 'px)');
  567. }
  568. // Rotate should come first before scale to match orientation transform
  569. if (isNumber(rotate) && rotate !== 0) {
  570. values.push('rotate(' + rotate + 'deg)');
  571. }
  572. if (isNumber(scaleX) && scaleX !== 1) {
  573. values.push('scaleX(' + scaleX + ')');
  574. }
  575. if (isNumber(scaleY) && scaleY !== 1) {
  576. values.push('scaleY(' + scaleY + ')');
  577. }
  578. var transform = values.length ? values.join(' ') : 'none';
  579. return {
  580. WebkitTransform: transform,
  581. msTransform: transform,
  582. transform: transform
  583. };
  584. }
  585. /**
  586. * Get an image name from an image url.
  587. * @param {string} url - The target url.
  588. * @example
  589. * // picture.jpg
  590. * getImageNameFromURL('http://domain.com/path/to/picture.jpg?size=1280×960')
  591. * @returns {string} A string contains the image name.
  592. */
  593. function getImageNameFromURL(url) {
  594. return isString(url) ? url.replace(/^.*\//, '').replace(/[?&#].*$/, '') : '';
  595. }
  596. var IS_SAFARI = WINDOW.navigator && /(Macintosh|iPhone|iPod|iPad).*AppleWebKit/i.test(WINDOW.navigator.userAgent);
  597. /**
  598. * Get an image's natural sizes.
  599. * @param {string} image - The target image.
  600. * @param {Function} callback - The callback function.
  601. * @returns {HTMLImageElement} The new image.
  602. */
  603. function getImageNaturalSizes(image, callback) {
  604. var newImage = document.createElement('img');
  605. // Modern browsers (except Safari)
  606. if (image.naturalWidth && !IS_SAFARI) {
  607. callback(image.naturalWidth, image.naturalHeight);
  608. return newImage;
  609. }
  610. var body = document.body || document.documentElement;
  611. newImage.onload = function () {
  612. callback(newImage.width, newImage.height);
  613. if (!IS_SAFARI) {
  614. body.removeChild(newImage);
  615. }
  616. };
  617. newImage.src = image.src;
  618. // iOS Safari will convert the image automatically
  619. // with its orientation once append it into DOM
  620. if (!IS_SAFARI) {
  621. newImage.style.cssText = 'left:0;' + 'max-height:none!important;' + 'max-width:none!important;' + 'min-height:0!important;' + 'min-width:0!important;' + 'opacity:0;' + 'position:absolute;' + 'top:0;' + 'z-index:-1;';
  622. body.appendChild(newImage);
  623. }
  624. return newImage;
  625. }
  626. /**
  627. * Get the related class name of a responsive type number.
  628. * @param {string} type - The responsive type.
  629. * @returns {string} The related class name.
  630. */
  631. function getResponsiveClass(type) {
  632. switch (type) {
  633. case 2:
  634. return CLASS_HIDE_XS_DOWN;
  635. case 3:
  636. return CLASS_HIDE_SM_DOWN;
  637. case 4:
  638. return CLASS_HIDE_MD_DOWN;
  639. default:
  640. return '';
  641. }
  642. }
  643. /**
  644. * Get the max ratio of a group of pointers.
  645. * @param {string} pointers - The target pointers.
  646. * @returns {number} The result ratio.
  647. */
  648. function getMaxZoomRatio(pointers) {
  649. var pointers2 = assign({}, pointers);
  650. var ratios = [];
  651. forEach(pointers, function (pointer, pointerId) {
  652. delete pointers2[pointerId];
  653. forEach(pointers2, function (pointer2) {
  654. var x1 = Math.abs(pointer.startX - pointer2.startX);
  655. var y1 = Math.abs(pointer.startY - pointer2.startY);
  656. var x2 = Math.abs(pointer.endX - pointer2.endX);
  657. var y2 = Math.abs(pointer.endY - pointer2.endY);
  658. var z1 = Math.sqrt(x1 * x1 + y1 * y1);
  659. var z2 = Math.sqrt(x2 * x2 + y2 * y2);
  660. var ratio = (z2 - z1) / z1;
  661. ratios.push(ratio);
  662. });
  663. });
  664. ratios.sort(function (a, b) {
  665. return Math.abs(a) < Math.abs(b);
  666. });
  667. return ratios[0];
  668. }
  669. /**
  670. * Get a pointer from an event object.
  671. * @param {Object} event - The target event object.
  672. * @param {boolean} endOnly - Indicates if only returns the end point coordinate or not.
  673. * @returns {Object} The result pointer contains start and/or end point coordinates.
  674. */
  675. function getPointer(_ref2, endOnly) {
  676. var pageX = _ref2.pageX,
  677. pageY = _ref2.pageY;
  678. var end = {
  679. endX: pageX,
  680. endY: pageY
  681. };
  682. return endOnly ? end : assign({
  683. startX: pageX,
  684. startY: pageY
  685. }, end);
  686. }
  687. /**
  688. * Get the center point coordinate of a group of pointers.
  689. * @param {Object} pointers - The target pointers.
  690. * @returns {Object} The center point coordinate.
  691. */
  692. function getPointersCenter(pointers) {
  693. var pageX = 0;
  694. var pageY = 0;
  695. var count = 0;
  696. forEach(pointers, function (_ref3) {
  697. var startX = _ref3.startX,
  698. startY = _ref3.startY;
  699. pageX += startX;
  700. pageY += startY;
  701. count += 1;
  702. });
  703. pageX /= count;
  704. pageY /= count;
  705. return {
  706. pageX: pageX,
  707. pageY: pageY
  708. };
  709. }
  710. var render = {
  711. render: function render() {
  712. this.initContainer();
  713. this.initViewer();
  714. this.initList();
  715. this.renderViewer();
  716. },
  717. initContainer: function initContainer() {
  718. this.containerData = {
  719. width: window.innerWidth,
  720. height: window.innerHeight
  721. };
  722. },
  723. initViewer: function initViewer() {
  724. var options = this.options,
  725. parent = this.parent;
  726. var viewerData = void 0;
  727. if (options.inline) {
  728. viewerData = {
  729. width: Math.max(parent.offsetWidth, options.minWidth),
  730. height: Math.max(parent.offsetHeight, options.minHeight)
  731. };
  732. this.parentData = viewerData;
  733. }
  734. if (this.fulled || !viewerData) {
  735. viewerData = this.containerData;
  736. }
  737. this.viewerData = assign({}, viewerData);
  738. },
  739. renderViewer: function renderViewer() {
  740. if (this.options.inline && !this.fulled) {
  741. setStyle(this.viewer, this.viewerData);
  742. }
  743. },
  744. initList: function initList() {
  745. var _this = this;
  746. var element = this.element,
  747. options = this.options,
  748. list = this.list;
  749. var items = [];
  750. var dealOriginal= this.dealOriginal;
  751. forEach(this.images, function (image, i) {
  752. var src = image.src;
  753. var alt = image.alt;// || getImageNameFromURL(src);
  754. var url = options.url;
  755. var url_original = options.url_original;
  756. if (isString(url_original)) {
  757. url_original = image.getAttribute(url_original);
  758. } else if (isFunction(url_original)) {
  759. url_original = url_original.call(_this, image);
  760. }
  761. if (dealOriginal.indexOf(url_original)>-1)
  762. {
  763. url=url_original;
  764. }else {
  765. if (isString(url)) {
  766. url = image.getAttribute(url);
  767. } else if (isFunction(url)) {
  768. url = url.call(_this, image);
  769. }
  770. }
  771. if (src || url) {
  772. items.push('<li>' + '<img' + (' src="' + (src || url) + '"') + ' role="button"' + ' data-action="view"' + (' data-index="' + i + '"') + (' data-original-url="' + (url || src) + '"') + (' data-deal-url="' + (url_original || src) + '"') + (' alt="' + alt + '"') + '>' + '</li>');
  773. }
  774. });
  775. list.innerHTML = items.join('');
  776. this.items = list.getElementsByTagName('li');
  777. forEach(this.items, function (item) {
  778. var image = item.firstElementChild;
  779. setData(image, 'filled', true);
  780. if (options.loading) {
  781. addClass(item, CLASS_LOADING);
  782. }
  783. addListener(image, EVENT_LOAD, function (event) {
  784. if (options.loading) {
  785. removeClass(item, CLASS_LOADING);
  786. }
  787. _this.loadImage(event);
  788. }, {
  789. once: true
  790. });
  791. });
  792. if (options.transition) {
  793. addListener(element, EVENT_VIEWED, function () {
  794. addClass(list, CLASS_TRANSITION);
  795. }, {
  796. once: true
  797. });
  798. }
  799. },
  800. renderList: function renderList(index) {
  801. var i = index || this.index;
  802. var width = this.items[i].offsetWidth || 30;
  803. var outerWidth = width + 1; // 1 pixel of `margin-left` width
  804. // Place the active item in the center of the screen
  805. setStyle(this.list, assign({
  806. width: outerWidth * this.length
  807. }, getTransforms({
  808. translateX: (this.viewerData.width - width) / 2 - outerWidth * i
  809. })));
  810. },
  811. resetList: function resetList() {
  812. var list = this.list;
  813. list.innerHTML = '';
  814. removeClass(list, CLASS_TRANSITION);
  815. setStyle(list, getTransforms({
  816. translateX: 0
  817. }));
  818. },
  819. initImage: function initImage(done) {
  820. var _this2 = this;
  821. var options = this.options,
  822. image = this.image,
  823. viewerData = this.viewerData;
  824. var footerHeight = this.footer.offsetHeight;
  825. var viewerWidth = viewerData.width;
  826. var viewerHeight = Math.max(viewerData.height - footerHeight, footerHeight);
  827. var oldImageData = this.imageData || {};
  828. var sizingImage = void 0;
  829. this.imageInitializing = {
  830. abort: function abort() {
  831. sizingImage.onload = null;
  832. }
  833. };
  834. sizingImage = getImageNaturalSizes(image, function (naturalWidth, naturalHeight) {
  835. var aspectRatio = naturalWidth / naturalHeight;
  836. var width = viewerWidth;
  837. var height = viewerHeight;
  838. _this2.imageInitializing = false;
  839. if (viewerHeight * aspectRatio > viewerWidth) {
  840. height = viewerWidth / aspectRatio;
  841. } else {
  842. width = viewerHeight * aspectRatio;
  843. }
  844. // width = Math.min(width * 0.9, naturalWidth);
  845. // height = Math.min(height * 0.9, naturalHeight);
  846. width = Math.min(width , naturalWidth);
  847. height = Math.min(height, naturalHeight);
  848. var imageData = {
  849. naturalWidth: naturalWidth,
  850. naturalHeight: naturalHeight,
  851. aspectRatio: aspectRatio,
  852. ratio: width / naturalWidth,
  853. width: width,
  854. height: height,
  855. left: (viewerWidth - width) / 2,
  856. top: (viewerHeight - height) / 2
  857. };
  858. var initialImageData = assign({}, imageData);
  859. if (options.rotatable) {
  860. imageData.rotate = oldImageData.rotate || 0;
  861. initialImageData.rotate = 0;
  862. }
  863. if (options.scalable) {
  864. imageData.scaleX = oldImageData.scaleX || 1;
  865. imageData.scaleY = oldImageData.scaleY || 1;
  866. initialImageData.scaleX = 1;
  867. initialImageData.scaleY = 1;
  868. }
  869. _this2.imageData = imageData;
  870. _this2.initialImageData = initialImageData;
  871. if (done) {
  872. done();
  873. }
  874. });
  875. },
  876. renderImage: function renderImage(done) {
  877. var _this3 = this;
  878. var image = this.image,
  879. viewerData = this.viewerData,
  880. imageData = this.imageData;
  881. setStyle(image, assign({
  882. width: imageData.width,
  883. height: imageData.height,
  884. marginLeft: imageData.left,
  885. marginTop: imageData.top
  886. }, getTransforms(imageData)));
  887. if (done) {
  888. if (this.viewing && this.options.transition) {
  889. var onTransitionEnd = function onTransitionEnd() {
  890. _this3.imageRendering = false;
  891. done();
  892. };
  893. this.imageRendering = {
  894. abort: function abort() {
  895. removeListener(image, EVENT_TRANSITION_END, onTransitionEnd);
  896. }
  897. };
  898. addListener(image, EVENT_TRANSITION_END, onTransitionEnd, {
  899. once: true
  900. });
  901. } else {
  902. done();
  903. }
  904. }
  905. var userAgent = window.navigator.userAgent;
  906. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  907. {
  908. var imageLi=image.parentNode;
  909. if (image.width>viewerData.width)
  910. {
  911. imageLi.style.width='auto';
  912. if (imageData.left<0)
  913. {
  914. imageLi.style.paddingLeft =(imageData.left*-1).toString()+'px';
  915. }
  916. }else {
  917. imageLi.style.width=viewerData.width.toString()+'px';
  918. imageLi.style.paddingLeft='0';
  919. }
  920. this.isSwitchable4();
  921. }
  922. },
  923. resetImage: function resetImage() {
  924. // this.image only defined after viewed
  925. if (this.viewing || this.viewed) {
  926. var image = this.image;
  927. if (this.viewing) {
  928. this.viewing.abort();
  929. }
  930. image.parentNode.removeChild(image);
  931. this.image = null;
  932. }
  933. }
  934. };
  935. var events = {
  936. bind: function bind() {
  937. var element = this.element,
  938. viewer = this.viewer;
  939. addListener(viewer, EVENT_CLICK, this.onClick = this.click.bind(this));
  940. addListener(viewer, EVENT_WHEEL, this.onWheel = this.wheel.bind(this));
  941. addListener(viewer, EVENT_DRAG_START, this.onDragStart = this.dragstart.bind(this));
  942. addListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown = this.pointerdown.bind(this));
  943. addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove = this.pointermove.bind(this));
  944. addListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp = this.pointerup.bind(this));
  945. addListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown = this.keydown.bind(this));
  946. addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
  947. },
  948. unbind: function unbind() {
  949. var element = this.element,
  950. viewer = this.viewer;
  951. removeListener(viewer, EVENT_CLICK, this.onClick);
  952. removeListener(viewer, EVENT_WHEEL, this.onWheel);
  953. removeListener(viewer, EVENT_DRAG_START, this.onDragStart);
  954. removeListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown);
  955. removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove);
  956. removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp);
  957. removeListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown);
  958. removeListener(window, EVENT_RESIZE, this.onResize);
  959. }
  960. };
  961. var handlers = {
  962. click: function click(_ref) {
  963. var target = _ref.target;
  964. var options = this.options,
  965. imageData = this.imageData;
  966. var action = getData(target, 'action');
  967. switch (action) {
  968. case 'mix':
  969. if (this.played) {
  970. this.stop();
  971. } else if (options.inline) {
  972. if (this.fulled) {
  973. this.exit();
  974. } else {
  975. this.full();
  976. }
  977. } else {
  978. this.hide();
  979. }
  980. break;
  981. case 'hide':
  982. this.hide();
  983. break;
  984. case 'view':
  985. this.view(getData(target, 'index'));
  986. break;
  987. case 'zoom-ins':
  988. this.zoom(0.1, true);
  989. break;
  990. case 'zoom-outs':
  991. this.zoom(-0.1, true);
  992. break;
  993. case 'reset':
  994. this.reset();
  995. break;
  996. case 'prevs':
  997. this.prev(options.loop);
  998. break;
  999. case 'plays':
  1000. this.play(options.fullscreen);
  1001. break;
  1002. case 'nexts':
  1003. this.next(options.loop);
  1004. break;
  1005. case 'one-to-ones':
  1006. case 'one-to-ones2':
  1007. this.normal();
  1008. break;
  1009. //放大
  1010. case 'enlarge':
  1011. this.toggle();
  1012. break;
  1013. case 'toolbar':
  1014. break;
  1015. //case 'rotate-left':
  1016. // this.rotate(-90);
  1017. // break;
  1018. //
  1019. //case 'rotate-right':
  1020. // this.rotate(90);
  1021. // break;
  1022. //
  1023. //case 'flip-horizontal':
  1024. // this.scaleX(-imageData.scaleX || -1);
  1025. // break;
  1026. //
  1027. //case 'flip-vertical':
  1028. // this.scaleY(-imageData.scaleY || -1);
  1029. // break;
  1030. default:
  1031. if (this.played) {
  1032. this.stop();
  1033. return;
  1034. }
  1035. if (target.className ==""
  1036. ||target.className =="viewer-toolbar"
  1037. ||target.className =="viewer-footer"
  1038. ||target.className =="viewer-title")
  1039. {
  1040. return;
  1041. }
  1042. if (target.className==='viewer-move'&&this.myMove !== undefined && this.myMove === true)
  1043. {
  1044. return;
  1045. }
  1046. this.hide();
  1047. }
  1048. },
  1049. load: function load() {
  1050. var _this = this;
  1051. if (this.timeout) {
  1052. clearTimeout(this.timeout);
  1053. this.timeout = false;
  1054. }
  1055. var element = this.element,
  1056. options = this.options,
  1057. image = this.image,
  1058. index = this.index,
  1059. viewerData = this.viewerData;
  1060. removeClass(image, CLASS_INVISIBLE);
  1061. if (options.loading) {
  1062. removeClass(this.canvas, CLASS_LOADING);
  1063. }
  1064. image.style.cssText = 'height:0;' + ('margin-left:' + viewerData.width / 2 + 'px;') + ('margin-top:' + viewerData.height / 2 + 'px;') + 'max-width:none!important;' + 'position:absolute;' + 'width:0;';
  1065. this.initImage(function () {
  1066. toggleClass(image, CLASS_MOVE, options.movable);
  1067. toggleClass(image, CLASS_TRANSITION, options.transition);
  1068. _this.renderImage(function () {
  1069. _this.viewed = true;
  1070. _this.viewing = false;
  1071. if (isFunction(options.viewed)) {
  1072. addListener(element, EVENT_VIEWED, options.viewed, {
  1073. once: true
  1074. });
  1075. }
  1076. dispatchEvent(element, EVENT_VIEWED, {
  1077. originalImage: _this.images[index],
  1078. index: index,
  1079. image: image
  1080. });
  1081. });
  1082. });
  1083. },
  1084. loadImage: function loadImage(e) {
  1085. var image = e.target;
  1086. var parent = image.parentNode;
  1087. var parentWidth = parent.offsetWidth || 30;
  1088. var parentHeight = parent.offsetHeight || 50;
  1089. var filled = !!getData(image, 'filled');
  1090. getImageNaturalSizes(image, function (naturalWidth, naturalHeight) {
  1091. var aspectRatio = naturalWidth / naturalHeight;
  1092. var width = parentWidth;
  1093. var height = parentHeight;
  1094. if (parentHeight * aspectRatio > parentWidth) {
  1095. if (filled) {
  1096. width = parentHeight * aspectRatio;
  1097. } else {
  1098. height = parentWidth / aspectRatio;
  1099. }
  1100. } else if (filled) {
  1101. height = parentWidth / aspectRatio;
  1102. } else {
  1103. width = parentHeight * aspectRatio;
  1104. }
  1105. setStyle(image, assign({
  1106. width: width,
  1107. height: height
  1108. }, getTransforms({
  1109. translateX: (parentWidth - width) / 2,
  1110. translateY: (parentHeight - height) / 2
  1111. })));
  1112. });
  1113. },
  1114. keydown: function keydown(e) {
  1115. var options = this.options;
  1116. if (!this.fulled || !options.keyboard) {
  1117. return;
  1118. }
  1119. switch (e.keyCode || e.which || e.charCode) {
  1120. // Escape
  1121. case 27:
  1122. if (this.played) {
  1123. this.stop();
  1124. } else if (options.inline) {
  1125. if (this.fulled) {
  1126. this.exit();
  1127. }
  1128. } else {
  1129. this.hide();
  1130. }
  1131. break;
  1132. // Space
  1133. case 32:
  1134. if (this.played) {
  1135. this.stop();
  1136. }
  1137. break;
  1138. // ArrowLeft
  1139. case 37:
  1140. this.prev(options.loop);
  1141. break;
  1142. // ArrowUp
  1143. case 38:
  1144. // Prevent scroll on Firefox
  1145. e.preventDefault();
  1146. // Zoom in
  1147. this.zoom(options.zoomRatio, true);
  1148. break;
  1149. // ArrowRight
  1150. case 39:
  1151. this.next(options.loop);
  1152. break;
  1153. // ArrowDown
  1154. case 40:
  1155. // Prevent scroll on Firefox
  1156. e.preventDefault();
  1157. // Zoom out
  1158. this.zoom(-options.zoomRatio, true);
  1159. break;
  1160. // Ctrl + 0
  1161. case 48:
  1162. // Fall through
  1163. // Ctrl + 1
  1164. // eslint-disable-next-line no-fallthrough
  1165. case 49:
  1166. if (e.ctrlKey) {
  1167. e.preventDefault();
  1168. this.toggle();
  1169. }
  1170. break;
  1171. default:
  1172. }
  1173. },
  1174. dragstart: function dragstart(e) {
  1175. if (e.target.tagName.toLowerCase() === 'img') {
  1176. e.preventDefault();
  1177. }
  1178. },
  1179. pointerdown: function pointerdown(e) {
  1180. var options = this.options,
  1181. pointers = this.pointers;
  1182. if (!this.viewed || this.showing || this.viewing || this.hiding) {
  1183. return;
  1184. }
  1185. this.myMove=false;
  1186. if (this.intervalTime!==undefined)
  1187. {
  1188. clearInterval(this.intervalTime);
  1189. this.intervalTime=null;
  1190. if (this.intervalTimeiTarget!==undefined&&this.intervalTimeiTarget!==null)
  1191. {
  1192. var ul=this.canvas.querySelector("ul");
  1193. if (ul!==null&&ul!==undefined)
  1194. {
  1195. this.ulLeft=this.intervalTimeiTarget;
  1196. ul.style.marginLeft =this.intervalTimeiTarget+ 'px';
  1197. }
  1198. this.intervalTimeiTarget=null;
  1199. }
  1200. }
  1201. if (e.changedTouches) {
  1202. forEach(e.changedTouches, function (touch) {
  1203. pointers[touch.identifier] = getPointer(touch);
  1204. });
  1205. } else {
  1206. pointers[e.pointerId || 0] = getPointer(e);
  1207. }
  1208. var action = options.movable ? ACTION_MOVE : false;
  1209. if (Object.keys(pointers).length > 1) {
  1210. action = ACTION_ZOOM;
  1211. } else if ((e.pointerType === 'touch' || e.type === 'touchstart')) {
  1212. var ul=this.canvas.querySelector("ul");
  1213. if (ul!==null&&ul!==undefined)
  1214. this.ulLeft= parseInt(ul.style.marginLeft);
  1215. }
  1216. this.action = action;
  1217. },
  1218. pointermove: function pointermove(e) {
  1219. var options = this.options,
  1220. pointers = this.pointers,
  1221. action = this.action,
  1222. image = this.image;
  1223. if (!this.viewed || !action) {
  1224. return;
  1225. }
  1226. this.myMove=true;
  1227. e.preventDefault();
  1228. if (e.changedTouches) {
  1229. forEach(e.changedTouches, function (touch) {
  1230. assign(pointers[touch.identifier], getPointer(touch, true));
  1231. });
  1232. } else {
  1233. var pointerTmep=pointers[e.pointerId || 0];
  1234. if (pointerTmep==null||pointerTmep==undefined)
  1235. {
  1236. return;
  1237. }
  1238. assign(pointerTmep, getPointer(e, true));
  1239. }
  1240. if (action === ACTION_MOVE && options.transition && hasClass(image, CLASS_TRANSITION)) {
  1241. removeClass(image, CLASS_TRANSITION);
  1242. }
  1243. if (Object.keys(pointers).length > 1|| action===ACTION_SWITCH) {
  1244. }else if ((e.pointerType === 'touch' || e.type === 'touchmove') && this.isSwitchable2()) {
  1245. this.action = ACTION_SWITCH;
  1246. }
  1247. this.change(e);
  1248. },
  1249. pointerup: function pointerup(e) {
  1250. var action = this.action,
  1251. pointers = this.pointers;
  1252. if (e.changedTouches) {
  1253. forEach(e.changedTouches, function (touch) {
  1254. delete pointers[touch.identifier];
  1255. });
  1256. } else {
  1257. delete pointers[e.pointerId || 0];
  1258. }
  1259. if (!action) {
  1260. return;
  1261. }
  1262. if (action === ACTION_MOVE && this.options.transition) {
  1263. addClass(this.image, CLASS_TRANSITION);
  1264. }
  1265. var userAgent = window.navigator.userAgent;
  1266. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1267. {
  1268. if (Object.keys(pointers).length<=1&&action===ACTION_MOVE) {
  1269. this.isSwitchable3();
  1270. }
  1271. }
  1272. this.action = false;
  1273. },
  1274. resize: function resize() {
  1275. var _this2 = this;
  1276. if (!this.isShown || this.hiding) {
  1277. return;
  1278. }
  1279. this.initContainer();
  1280. this.initViewer();
  1281. this.renderViewer();
  1282. this.renderList();
  1283. if (this.viewed) {
  1284. this.initImage(function () {
  1285. _this2.renderImage();
  1286. });
  1287. }
  1288. if (this.played) {
  1289. if (this.options.fullscreen && this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  1290. this.stop();
  1291. return;
  1292. }
  1293. forEach(this.player.getElementsByTagName('img'), function (image) {
  1294. addListener(image, EVENT_LOAD, _this2.loadImage.bind(_this2), {
  1295. once: true
  1296. });
  1297. dispatchEvent(image, EVENT_LOAD);
  1298. });
  1299. }
  1300. },
  1301. wheel: function wheel(e) {
  1302. var _this3 = this;
  1303. if (!this.viewed) {
  1304. return;
  1305. }
  1306. e.preventDefault();
  1307. // Limit wheel speed to prevent zoom too fast
  1308. if (this.wheeling) {
  1309. return;
  1310. }
  1311. this.wheeling = true;
  1312. setTimeout(function () {
  1313. _this3.wheeling = false;
  1314. }, 50);
  1315. var ratio = Number(this.options.zoomRatio) || 0.1;
  1316. var delta = 1;
  1317. if (e.deltaY) {
  1318. delta = e.deltaY > 0 ? 1 : -1;
  1319. } else if (e.wheelDelta) {
  1320. delta = -e.wheelDelta / 120;
  1321. } else if (e.detail) {
  1322. delta = e.detail > 0 ? 1 : -1;
  1323. }
  1324. this.zoom(-delta * ratio, true, e);
  1325. }
  1326. };
  1327. var methods = {
  1328. /** Show the viewer (only available in modal mode)
  1329. * @param {boolean} [immediate=false] - Indicates if show the viewer immediately or not.
  1330. * @returns {Viewer} this
  1331. */
  1332. show: function show() {
  1333. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1334. var element = this.element,
  1335. options = this.options;
  1336. if (options.inline || this.showing || this.isShown || this.showing) {
  1337. return this;
  1338. }
  1339. if (!this.ready) {
  1340. this.build();
  1341. if (this.ready) {
  1342. this.show(immediate);
  1343. }
  1344. return this;
  1345. }
  1346. if (isFunction(options.show)) {
  1347. addListener(element, EVENT_SHOW, options.show, {
  1348. once: true
  1349. });
  1350. }
  1351. if (dispatchEvent(element, EVENT_SHOW) === false || !this.ready) {
  1352. return this;
  1353. }
  1354. if (this.hiding) {
  1355. this.transitioning.abort();
  1356. }
  1357. this.showing = true;
  1358. this.open();
  1359. var viewer = this.viewer;
  1360. removeClass(viewer, CLASS_HIDE);
  1361. if (options.transition && !immediate) {
  1362. var shown = this.shown.bind(this);
  1363. this.transitioning = {
  1364. abort: function abort() {
  1365. removeListener(viewer, EVENT_TRANSITION_END, shown);
  1366. removeClass(viewer, CLASS_IN);
  1367. }
  1368. };
  1369. addClass(viewer, CLASS_TRANSITION);
  1370. // Force reflow to enable CSS3 transition
  1371. // eslint-disable-next-line
  1372. viewer.offsetWidth;
  1373. addListener(viewer, EVENT_TRANSITION_END, shown, {
  1374. once: true
  1375. });
  1376. addClass(viewer, CLASS_IN);
  1377. } else {
  1378. addClass(viewer, CLASS_IN);
  1379. this.shown();
  1380. }
  1381. return this;
  1382. },
  1383. /**
  1384. * Hide the viewer (only available in modal mode)
  1385. * @param {boolean} [immediate=false] - Indicates if hide the viewer immediately or not.
  1386. * @returns {Viewer} this
  1387. */
  1388. hide: function hide() {
  1389. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1390. var element = this.element,
  1391. options = this.options;
  1392. if (options.inline || this.hiding || !(this.isShown || this.showing)) {
  1393. return this;
  1394. }
  1395. if (isFunction(options.hide)) {
  1396. addListener(element, EVENT_HIDE, options.hide, {
  1397. once: true
  1398. });
  1399. }
  1400. if (dispatchEvent(element, EVENT_HIDE) === false) {
  1401. return this;
  1402. }
  1403. if (this.showing) {
  1404. this.transitioning.abort();
  1405. }
  1406. this.hiding = true;
  1407. if (this.played) {
  1408. this.stop();
  1409. } else if (this.viewing) {
  1410. this.viewing.abort();
  1411. }
  1412. var viewer = this.viewer;
  1413. if (options.transition && !immediate) {
  1414. var hidden = this.hidden.bind(this);
  1415. var hide = function hide() {
  1416. addListener(viewer, EVENT_TRANSITION_END, hidden, {
  1417. once: true
  1418. });
  1419. removeClass(viewer, CLASS_IN);
  1420. };
  1421. this.transitioning = {
  1422. abort: function abort() {
  1423. if (this.viewed) {
  1424. removeListener(this.image, EVENT_TRANSITION_END, hide);
  1425. } else {
  1426. removeListener(viewer, EVENT_TRANSITION_END, hidden);
  1427. }
  1428. }
  1429. };
  1430. if (this.viewed) {
  1431. addListener(this.image, EVENT_TRANSITION_END, hide, {
  1432. once: true
  1433. });
  1434. this.zoomTo(0, false, false, true);
  1435. } else {
  1436. hide();
  1437. }
  1438. } else {
  1439. removeClass(viewer, CLASS_IN);
  1440. this.hidden();
  1441. }
  1442. return this;
  1443. },
  1444. /**
  1445. * View one of the images with image's index
  1446. * @param {number} index - The index of the image to view.
  1447. * @returns {Viewer} this
  1448. */
  1449. view: function view() {
  1450. var _this = this;
  1451. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1452. var mandatory = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1453. index = Number(index) || 0;
  1454. if (!this.isShown) {
  1455. this.index = index;
  1456. return this.show();
  1457. }
  1458. if (this.hiding || this.played || index < 0 || index >= this.length) {
  1459. return this;
  1460. }
  1461. if (!mandatory)
  1462. {
  1463. if (this.viewed && index === this.index) {
  1464. return this;
  1465. }
  1466. }
  1467. if (this.viewing) {
  1468. this.viewing.abort();
  1469. }
  1470. var element = this.element,
  1471. options = this.options,
  1472. title = this.title,
  1473. canvas = this.canvas;
  1474. var item = this.items[index];
  1475. var img = item.querySelector('img');
  1476. var url = getData(img, 'originalUrl');
  1477. var alt = img.getAttribute('alt');
  1478. var image = document.createElement('img');
  1479. image.src = url;
  1480. image.alt = alt;
  1481. image.style.display='none';
  1482. if (isFunction(options.view)) {
  1483. addListener(element, EVENT_VIEW, options.view, {
  1484. once: true
  1485. });
  1486. }
  1487. if (dispatchEvent(element, EVENT_VIEW, {
  1488. originalImage: this.images[index],
  1489. index: index,
  1490. image: image
  1491. }) === false || !this.isShown || this.hiding || this.played) {
  1492. return this;
  1493. }
  1494. this.image = image;
  1495. removeClass(this.items[this.index], CLASS_ACTIVE);
  1496. addClass(item, CLASS_ACTIVE);
  1497. this.viewed = false;
  1498. this.index = index;
  1499. this.imageData = {};
  1500. addClass(image, CLASS_INVISIBLE);
  1501. if (options.loading) {
  1502. addClass(canvas, CLASS_LOADING);
  1503. }
  1504. canvas.innerHTML = '';
  1505. var viewerData = _this.viewerData;
  1506. this.intervalTime=null;
  1507. this.ulLeft= this.ulLeftInitial=-10;
  1508. var is_sj=false;
  1509. var imageBf=null;
  1510. var imageAf=null;
  1511. var liBf=null;
  1512. var liAf=null;
  1513. var ul=null;
  1514. var userAgent = window.navigator.userAgent;
  1515. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1516. {
  1517. is_sj=true;
  1518. }
  1519. if (is_sj)
  1520. {
  1521. ul = document.createElement('ul');
  1522. if (index-1>=0)
  1523. {
  1524. var itemBf = this.items[index-1];
  1525. var imgBf = itemBf.querySelector('img');
  1526. var urlBf = getData(imgBf, 'originalUrl');
  1527. var altBf = imgBf.getAttribute('alt');
  1528. imageBf = document.createElement('img');
  1529. imageBf.src = urlBf;
  1530. imageBf.alt = altBf;
  1531. imageBf.style.display='none';
  1532. imageBf.style.width='100%';
  1533. setData(imageBf, 'action', 'img');
  1534. liBf = document.createElement('li');
  1535. liBf.appendChild(imageBf);
  1536. liBf.style.width=viewerData.width.toString()+'px';
  1537. ul.appendChild(liBf);
  1538. }
  1539. var li = document.createElement('li');
  1540. li.appendChild(image);
  1541. li.style.width=viewerData.width.toString()+'px';
  1542. ul.appendChild(li);
  1543. if (index+1<this.length)
  1544. {
  1545. var itemAf = this.items[index+1];
  1546. var imgAf = itemAf.querySelector('img');
  1547. var urlAf = getData(imgAf, 'originalUrl');
  1548. var altAf = imgAf.getAttribute('alt');
  1549. imageAf = document.createElement('img');
  1550. imageAf.src = urlAf;
  1551. imageAf.alt = altAf;
  1552. imageAf.style.display='none';
  1553. imageAf.style.width='100%';
  1554. setData(imageAf, 'action', 'img');
  1555. liAf = document.createElement('li');
  1556. liAf.appendChild(imageAf);
  1557. liAf.style.width=viewerData.width.toString()+'px';
  1558. ul.appendChild(liAf);
  1559. }
  1560. canvas.appendChild(ul);
  1561. }else {
  1562. canvas.appendChild(image);
  1563. }
  1564. // Center current item
  1565. this.renderList();
  1566. // Clear title
  1567. // title.innerHTML = '';
  1568. title.textContent = (_this.index+1)+'/'+options.totalCount;
  1569. if (this.dealOriginal.indexOf(url)>-1)
  1570. {
  1571. this.toolbar.querySelector(".viewer-one-to-ones2").style.display='block';
  1572. this.toolbar.querySelector(".viewer-one-to-ones").style.display='none';
  1573. }else {
  1574. this.toolbar.querySelector(".viewer-one-to-ones").style.display='block';
  1575. this.toolbar.querySelector(".viewer-one-to-ones2").style.display='none';
  1576. }
  1577. // Generate title after viewed
  1578. var onViewed = function onViewed() {
  1579. // var imageData = _this.imageData;
  1580. image.style.display='block';
  1581. if (is_sj)
  1582. {
  1583. if (imageBf!=null)
  1584. {
  1585. _this.ulLeft= _this.ulLeftInitial=-viewerData.width-20;
  1586. }
  1587. ul.style.marginLeft=_this.ulLeft.toString()+'px';
  1588. if (imageBf!=null)
  1589. {
  1590. imageBf.style.height=image.style.height;
  1591. imageBf.style.marginTop=image.style.marginTop;
  1592. imageBf.style.display='block';
  1593. }
  1594. if (imageAf!=null) {
  1595. imageAf.style.height=image.style.height;
  1596. imageAf.style.marginTop=image.style.marginTop;
  1597. imageAf.style.display='block';
  1598. }
  1599. }
  1600. // title.textContent = alt + ' (' + imageData.naturalWidth + ' \xD7 ' + imageData.naturalHeight + ')';
  1601. };
  1602. var onLoad = void 0;
  1603. addListener(element, EVENT_VIEWED, onViewed, {
  1604. once: true
  1605. });
  1606. this.viewing = {
  1607. abort: function abort() {
  1608. removeListener(element, EVENT_VIEWED, onViewed);
  1609. if (image.complete) {
  1610. if (this.imageRendering) {
  1611. this.imageRendering.abort();
  1612. } else if (this.imageInitializing) {
  1613. this.imageInitializing.abort();
  1614. }
  1615. } else {
  1616. removeListener(image, EVENT_LOAD, onLoad);
  1617. if (this.timeout) {
  1618. clearTimeout(this.timeout);
  1619. }
  1620. }
  1621. }
  1622. };
  1623. if (image.complete) {
  1624. this.load();
  1625. } else {
  1626. addListener(image, EVENT_LOAD, onLoad = this.load.bind(this), {
  1627. once: true
  1628. });
  1629. if (this.timeout) {
  1630. clearTimeout(this.timeout);
  1631. }
  1632. // Make the image visible if it fails to load within 1s
  1633. this.timeout = setTimeout(function () {
  1634. removeClass(image, CLASS_INVISIBLE);
  1635. _this.timeout = false;
  1636. }, 1000);
  1637. }
  1638. return this;
  1639. },
  1640. /**
  1641. * View the previous image
  1642. * @param {boolean} [loop=false] - Indicate if view the last one
  1643. * when it is the first one at present.
  1644. * @returns {Viewer} this
  1645. */
  1646. prev: function prev() {
  1647. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1648. var index = this.index - 1;
  1649. if (index < 0) {
  1650. index = loop ? this.length - 1 : 0;
  1651. swal("当前是第一个", {
  1652. buttons: false,
  1653. timer: 1500,
  1654. });
  1655. return this;
  1656. }
  1657. if (this.intervalTime!==undefined)
  1658. {
  1659. clearInterval(this.intervalTime);
  1660. this.intervalTime=null;
  1661. this.intervalTimeiTarget=null;
  1662. }
  1663. this.view(index,false,'prev');
  1664. return this;
  1665. },
  1666. /**
  1667. * View the next image
  1668. * @param {boolean} [loop=false] - Indicate if view the first one
  1669. * when it is the last one at present.
  1670. * @returns {Viewer} this
  1671. */
  1672. next: function next() {
  1673. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1674. var options = this.options;
  1675. var maxIndex =options.totalCount-1; //this.length - 1;
  1676. var index = this.index + 1;
  1677. if (index > maxIndex) {
  1678. swal("当前是最后一个", {
  1679. buttons: false,
  1680. timer: 1500,
  1681. });
  1682. index = loop ? 0 : maxIndex;
  1683. return this;
  1684. }else {
  1685. if (index>=this.length - 1)
  1686. {
  1687. var element = this.element;
  1688. if (isFunction(options.next)) {
  1689. addListener(element, EVENT_NEXT, options.next, {
  1690. once: true
  1691. });
  1692. }
  1693. if (dispatchEvent(element, EVENT_NEXT) === false) {
  1694. return;
  1695. }
  1696. this.update();
  1697. }
  1698. }
  1699. if (this.intervalTime!==undefined)
  1700. {
  1701. clearInterval(this.intervalTime);
  1702. this.intervalTime=null;
  1703. this.intervalTimeiTarget=null;
  1704. }
  1705. this.view(index,false,'next');
  1706. return this;
  1707. },
  1708. normal: function normal() {
  1709. var index = this.index;
  1710. var item = this.items[index];
  1711. var img = item.querySelector('img');
  1712. var dealUrl = getData(img, 'dealUrl');
  1713. if (this.dealOriginal.indexOf(dealUrl)>-1)
  1714. {
  1715. return this;
  1716. }
  1717. this.dealOriginal.push(dealUrl);
  1718. setData(img,'originalUrl',dealUrl);
  1719. this.view(index,true);
  1720. return this;
  1721. },
  1722. /**
  1723. * Move the image with relative offsets.
  1724. * @param {number} offsetX - The relative offset distance on the x-axis.
  1725. * @param {number} offsetY - The relative offset distance on the y-axis.
  1726. * @returns {Viewer} this
  1727. */
  1728. move: function move(offsetX, offsetY) {
  1729. var imageData = this.imageData;
  1730. this.moveTo(isUndefined(offsetX) ? offsetX : imageData.left + Number(offsetX), isUndefined(offsetY) ? offsetY : imageData.top + Number(offsetY));
  1731. return this;
  1732. },
  1733. moveSj: function move(offsetX, offsetY) {
  1734. var ul=this.canvas.querySelector("ul");
  1735. this.ulLeft=this.ulLeft+offsetX;
  1736. ul.style.marginLeft = this.ulLeft+ 'px';
  1737. if (this.isSwitchable2())
  1738. {
  1739. var than=this,
  1740. image = this.image,
  1741. viewerData = this.viewerData;
  1742. if (offsetX > 0) {
  1743. this.action=false;
  1744. var index = this.index - 1;
  1745. if (index < 0) {
  1746. this.springMove(this.ulLeftInitial);
  1747. }
  1748. this.pointers = {};
  1749. than.prev(false);
  1750. } else if (offsetX < 0) {
  1751. this.action=false;
  1752. var imageLi=image.parentNode;
  1753. var maxIndex =this.options.totalCount-1; //this.length - 1;
  1754. var index = this.index + 1;
  1755. if (index > maxIndex) {
  1756. var gjLeft2 = parseInt(this.ulLeftInitial);
  1757. var diff = parseInt(parseInt(image.style.width) - parseInt(viewerData.width));
  1758. if (diff > 0) {
  1759. gjLeft2 = gjLeft2 - diff;
  1760. }
  1761. this.springMove(gjLeft2);
  1762. }
  1763. this.pointers = {};
  1764. than.next(false);
  1765. }
  1766. }
  1767. },
  1768. testPromise:function testPromise() {
  1769. var than=this;
  1770. var promise1 = new Promise(function(resolve, reject) {
  1771. setTimeout(function() {
  1772. resolve('foo');
  1773. }, 1000);
  1774. });
  1775. var rush=false;
  1776. promise1.then(function(value) {
  1777. than.next(false);
  1778. rush=true;
  1779. });
  1780. return rush;
  1781. },
  1782. /**
  1783. * Move the image to an absolute point.
  1784. * @param {number} x - The x-axis coordinate.
  1785. * @param {number} [y=x] - The y-axis coordinate.
  1786. * @returns {Viewer} this
  1787. */
  1788. moveTo: function moveTo(x) {
  1789. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
  1790. var imageData = this.imageData;
  1791. x = Number(x);
  1792. y = Number(y);
  1793. if (this.viewed && !this.played && this.options.movable) {
  1794. var changed = false;
  1795. if (isNumber(x)) {
  1796. imageData.left = x;
  1797. changed = true;
  1798. }
  1799. if (isNumber(y)) {
  1800. imageData.top = y;
  1801. changed = true;
  1802. }
  1803. if (changed) {
  1804. this.renderImage();
  1805. }
  1806. }
  1807. return this;
  1808. },
  1809. /**
  1810. * Zoom the image with a relative ratio.
  1811. * @param {number} ratio - The target ratio.
  1812. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1813. * @param {Event} [_originalEvent=null] - The original event if any.
  1814. * @returns {Viewer} this
  1815. */
  1816. zoom: function zoom(ratio) {
  1817. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1818. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1819. var imageData = this.imageData;
  1820. ratio = Number(ratio);
  1821. if (ratio < 0) {
  1822. ratio = 1 / (1 - ratio);
  1823. } else {
  1824. ratio = 1 + ratio;
  1825. }
  1826. this.zoomTo(imageData.width * ratio / imageData.naturalWidth, hasTooltip, _originalEvent);
  1827. return this;
  1828. },
  1829. /**
  1830. * Zoom the image to an absolute ratio.
  1831. * @param {number} ratio - The target ratio.
  1832. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1833. * @param {Event} [_originalEvent=null] - The original event if any.
  1834. * @param {Event} [_zoomable=false] - Indicates if the current zoom is available or not.
  1835. * @returns {Viewer} this
  1836. */
  1837. zoomTo: function zoomTo(ratio) {
  1838. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1839. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1840. var _zoomable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  1841. var options = this.options,
  1842. pointers = this.pointers,
  1843. imageData = this.imageData;
  1844. ratio = Math.max(0, ratio);
  1845. if (isNumber(ratio) && this.viewed && !this.played && (_zoomable || options.zoomable)) {
  1846. if (!_zoomable) {
  1847. var minZoomRatio = Math.max(0.01, options.minZoomRatio);
  1848. var maxZoomRatio = Math.min(100, options.maxZoomRatio);
  1849. ratio = Math.min(Math.max(ratio, minZoomRatio), maxZoomRatio);
  1850. }
  1851. if (_originalEvent && ratio > 0.95 && ratio < 1.05) {
  1852. ratio = 1;
  1853. }
  1854. var newWidth = imageData.naturalWidth * ratio;
  1855. var newHeight = imageData.naturalHeight * ratio;
  1856. if (_originalEvent) {
  1857. var offset = getOffset(this.viewer);
  1858. var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
  1859. pageX: _originalEvent.pageX,
  1860. pageY: _originalEvent.pageY
  1861. };
  1862. // Zoom from the triggering point of the event
  1863. imageData.left -= (newWidth - imageData.width) * ((center.pageX - offset.left - imageData.left) / imageData.width);
  1864. imageData.top -= (newHeight - imageData.height) * ((center.pageY - offset.top - imageData.top) / imageData.height);
  1865. } else {
  1866. // Zoom from the center of the image
  1867. imageData.left -= (newWidth - imageData.width) / 2;
  1868. imageData.top -= (newHeight - imageData.height) / 2;
  1869. }
  1870. imageData.width = newWidth;
  1871. imageData.height = newHeight;
  1872. imageData.ratio = ratio;
  1873. this.renderImage();
  1874. if (hasTooltip) {
  1875. this.tooltip();
  1876. }
  1877. }
  1878. var userAgent = window.navigator.userAgent;
  1879. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1880. {
  1881. var image = this.image,
  1882. viewerData = this.viewerData;
  1883. if (image.width>viewerData.width)
  1884. {
  1885. var gjLeft2=parseInt(this.ulLeftInitial);
  1886. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  1887. if (diff>0)
  1888. {
  1889. if (this.intervalTime!==undefined)
  1890. {
  1891. clearInterval(this.intervalTime);
  1892. this.intervalTime=null;
  1893. this.intervalTimeiTarget=null;
  1894. }
  1895. gjLeft2=gjLeft2-diff/2;
  1896. var ul=this.canvas.querySelector("ul");
  1897. this.ulLeft=gjLeft2;
  1898. ul.style.marginLeft = this.ulLeft+ 'px';
  1899. }
  1900. }
  1901. }
  1902. return this;
  1903. },
  1904. /**
  1905. * Rotate the image with a relative degree.
  1906. * @param {number} degree - The rotate degree.
  1907. * @returns {Viewer} this
  1908. */
  1909. rotate: function rotate(degree) {
  1910. this.rotateTo((this.imageData.rotate || 0) + Number(degree));
  1911. return this;
  1912. },
  1913. /**
  1914. * Rotate the image to an absolute degree.
  1915. * @param {number} degree - The rotate degree.
  1916. * @returns {Viewer} this
  1917. */
  1918. rotateTo: function rotateTo(degree) {
  1919. var imageData = this.imageData;
  1920. degree = Number(degree);
  1921. if (isNumber(degree) && this.viewed && !this.played && this.options.rotatable) {
  1922. imageData.rotate = degree;
  1923. this.renderImage();
  1924. }
  1925. return this;
  1926. },
  1927. /**
  1928. * Scale the image on the x-axis.
  1929. * @param {number} scaleX - The scale ratio on the x-axis.
  1930. * @returns {Viewer} this
  1931. */
  1932. scaleX: function scaleX(_scaleX) {
  1933. this.scale(_scaleX, this.imageData.scaleY);
  1934. return this;
  1935. },
  1936. /**
  1937. * Scale the image on the y-axis.
  1938. * @param {number} scaleY - The scale ratio on the y-axis.
  1939. * @returns {Viewer} this
  1940. */
  1941. scaleY: function scaleY(_scaleY) {
  1942. this.scale(this.imageData.scaleX, _scaleY);
  1943. return this;
  1944. },
  1945. /**
  1946. * Scale the image.
  1947. * @param {number} scaleX - The scale ratio on the x-axis.
  1948. * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
  1949. * @returns {Viewer} this
  1950. */
  1951. scale: function scale(scaleX) {
  1952. var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
  1953. var imageData = this.imageData;
  1954. scaleX = Number(scaleX);
  1955. scaleY = Number(scaleY);
  1956. if (this.viewed && !this.played && this.options.scalable) {
  1957. var changed = false;
  1958. if (isNumber(scaleX)) {
  1959. imageData.scaleX = scaleX;
  1960. changed = true;
  1961. }
  1962. if (isNumber(scaleY)) {
  1963. imageData.scaleY = scaleY;
  1964. changed = true;
  1965. }
  1966. if (changed) {
  1967. this.renderImage();
  1968. }
  1969. }
  1970. return this;
  1971. },
  1972. /**
  1973. * Play the images
  1974. * @param {boolean} [fullscreen=false] - Indicate if request fullscreen or not.
  1975. * @returns {Viewer} this
  1976. */
  1977. play: function play() {
  1978. var _this2 = this;
  1979. var fullscreen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1980. if (!this.isShown || this.played) {
  1981. return this;
  1982. }
  1983. var options = this.options,
  1984. player = this.player;
  1985. var onLoad = this.loadImage.bind(this);
  1986. var list = [];
  1987. var total = 0;
  1988. var index = 0;
  1989. this.played = true;
  1990. this.onLoadWhenPlay = onLoad;
  1991. if (fullscreen) {
  1992. this.requestFullscreen();
  1993. }
  1994. addClass(player, CLASS_SHOW);
  1995. forEach(this.items, function (item, i) {
  1996. var img = item.querySelector('img');
  1997. var image = document.createElement('img');
  1998. image.src = getData(img, 'originalUrl');
  1999. image.alt = img.getAttribute('alt');
  2000. total += 1;
  2001. addClass(image, CLASS_FADE);
  2002. toggleClass(image, CLASS_TRANSITION, options.transition);
  2003. if (hasClass(item, CLASS_ACTIVE)) {
  2004. addClass(image, CLASS_IN);
  2005. index = i;
  2006. }
  2007. list.push(image);
  2008. addListener(image, EVENT_LOAD, onLoad, {
  2009. once: true
  2010. });
  2011. player.appendChild(image);
  2012. });
  2013. if (isNumber(options.interval) && options.interval > 0) {
  2014. var play = function play() {
  2015. _this2.playing = setTimeout(function () {
  2016. removeClass(list[index], CLASS_IN);
  2017. index += 1;
  2018. index = index < total ? index : 0;
  2019. addClass(list[index], CLASS_IN);
  2020. play();
  2021. }, options.interval);
  2022. };
  2023. if (total > 1) {
  2024. play();
  2025. }
  2026. }
  2027. return this;
  2028. },
  2029. // Stop play
  2030. stop: function stop() {
  2031. var _this3 = this;
  2032. if (!this.played) {
  2033. return this;
  2034. }
  2035. var player = this.player;
  2036. this.played = false;
  2037. clearTimeout(this.playing);
  2038. forEach(player.getElementsByTagName('img'), function (image) {
  2039. removeListener(image, EVENT_LOAD, _this3.onLoadWhenPlay);
  2040. });
  2041. removeClass(player, CLASS_SHOW);
  2042. player.innerHTML = '';
  2043. this.exitFullscreen();
  2044. return this;
  2045. },
  2046. // Enter modal mode (only available in inline mode)
  2047. full: function full() {
  2048. var _this4 = this;
  2049. var options = this.options,
  2050. viewer = this.viewer,
  2051. image = this.image,
  2052. list = this.list;
  2053. if (!this.isShown || this.played || this.fulled || !options.inline) {
  2054. return this;
  2055. }
  2056. this.fulled = true;
  2057. this.open();
  2058. addClass(this.button, CLASS_FULLSCREEN_EXIT);
  2059. if (options.transition) {
  2060. removeClass(list, CLASS_TRANSITION);
  2061. if (this.viewed) {
  2062. removeClass(image, CLASS_TRANSITION);
  2063. }
  2064. }
  2065. addClass(viewer, CLASS_FIXED);
  2066. viewer.setAttribute('style', '');
  2067. setStyle(viewer, {
  2068. zIndex: options.zIndex
  2069. });
  2070. this.initContainer();
  2071. this.viewerData = assign({}, this.containerData);
  2072. this.renderList();
  2073. if (this.viewed) {
  2074. this.initImage(function () {
  2075. _this4.renderImage(function () {
  2076. if (options.transition) {
  2077. setTimeout(function () {
  2078. addClass(image, CLASS_TRANSITION);
  2079. addClass(list, CLASS_TRANSITION);
  2080. }, 0);
  2081. }
  2082. });
  2083. });
  2084. }
  2085. return this;
  2086. },
  2087. // Exit modal mode (only available in inline mode)
  2088. exit: function exit() {
  2089. var _this5 = this;
  2090. var options = this.options,
  2091. viewer = this.viewer,
  2092. image = this.image,
  2093. list = this.list;
  2094. if (!this.isShown || this.played || !this.fulled || !options.inline) {
  2095. return this;
  2096. }
  2097. this.fulled = false;
  2098. this.close();
  2099. removeClass(this.button, CLASS_FULLSCREEN_EXIT);
  2100. if (options.transition) {
  2101. removeClass(list, CLASS_TRANSITION);
  2102. if (this.viewed) {
  2103. removeClass(image, CLASS_TRANSITION);
  2104. }
  2105. }
  2106. removeClass(viewer, CLASS_FIXED);
  2107. setStyle(viewer, {
  2108. zIndex: options.zIndexInline
  2109. });
  2110. this.viewerData = assign({}, this.parentData);
  2111. this.renderViewer();
  2112. this.renderList();
  2113. if (this.viewed) {
  2114. this.initImage(function () {
  2115. _this5.renderImage(function () {
  2116. if (options.transition) {
  2117. setTimeout(function () {
  2118. addClass(image, CLASS_TRANSITION);
  2119. addClass(list, CLASS_TRANSITION);
  2120. }, 0);
  2121. }
  2122. });
  2123. });
  2124. }
  2125. return this;
  2126. },
  2127. // Show the current ratio of the image with percentage
  2128. tooltip: function tooltip() {
  2129. var _this6 = this;
  2130. var options = this.options,
  2131. tooltipBox = this.tooltipBox,
  2132. imageData = this.imageData;
  2133. if (!this.viewed || this.played || !options.tooltip) {
  2134. return this;
  2135. }
  2136. tooltipBox.textContent = Math.round(imageData.ratio * 100) + '%';
  2137. if (!this.tooltipping) {
  2138. if (options.transition) {
  2139. if (this.fading) {
  2140. dispatchEvent(tooltipBox, EVENT_TRANSITION_END);
  2141. }
  2142. addClass(tooltipBox, CLASS_SHOW);
  2143. addClass(tooltipBox, CLASS_FADE);
  2144. addClass(tooltipBox, CLASS_TRANSITION);
  2145. // Force reflow to enable CSS3 transition
  2146. // eslint-disable-next-line
  2147. tooltipBox.offsetWidth;
  2148. addClass(tooltipBox, CLASS_IN);
  2149. } else {
  2150. addClass(tooltipBox, CLASS_SHOW);
  2151. }
  2152. } else {
  2153. clearTimeout(this.tooltipping);
  2154. }
  2155. this.tooltipping = setTimeout(function () {
  2156. if (options.transition) {
  2157. addListener(tooltipBox, EVENT_TRANSITION_END, function () {
  2158. removeClass(tooltipBox, CLASS_SHOW);
  2159. removeClass(tooltipBox, CLASS_FADE);
  2160. removeClass(tooltipBox, CLASS_TRANSITION);
  2161. _this6.fading = false;
  2162. }, {
  2163. once: true
  2164. });
  2165. removeClass(tooltipBox, CLASS_IN);
  2166. _this6.fading = true;
  2167. } else {
  2168. removeClass(tooltipBox, CLASS_SHOW);
  2169. }
  2170. _this6.tooltipping = false;
  2171. }, 1000);
  2172. return this;
  2173. },
  2174. // Toggle the image size between its natural size and initial size
  2175. toggle: function toggle() {
  2176. if (this.imageData.ratio === 1) {
  2177. this.zoomTo(this.initialImageData.ratio, true);
  2178. } else {
  2179. this.zoomTo(1, true);
  2180. }
  2181. return this;
  2182. },
  2183. // Reset the image to its initial state
  2184. reset: function reset() {
  2185. if (this.viewed && !this.played) {
  2186. this.imageData = assign({}, this.initialImageData);
  2187. this.renderImage();
  2188. }
  2189. return this;
  2190. },
  2191. // Update viewer when images changed
  2192. update: function update() {
  2193. var element = this.element,
  2194. options = this.options,
  2195. isImg = this.isImg;
  2196. // Destroy viewer if the target image was deleted
  2197. if (isImg && !element.parentNode) {
  2198. return this.destroy();
  2199. }
  2200. var images = [];
  2201. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  2202. if (options.filter) {
  2203. if (options.filter(image)) {
  2204. images.push(image);
  2205. }
  2206. } else {
  2207. images.push(image);
  2208. }
  2209. });
  2210. if (!images.length) {
  2211. return this;
  2212. }
  2213. this.images = images;
  2214. this.length = images.length;
  2215. if (this.ready) {
  2216. var indexes = [];
  2217. forEach(this.items, function (item, i) {
  2218. var img = item.querySelector('img');
  2219. var image = images[i];
  2220. if (image) {
  2221. if (image.src !== img.src) {
  2222. indexes.push(i);
  2223. }
  2224. } else {
  2225. indexes.push(i);
  2226. }
  2227. });
  2228. setStyle(this.list, {
  2229. width: 'auto'
  2230. });
  2231. this.initList();
  2232. if (this.isShown) {
  2233. if (this.length) {
  2234. if (this.viewed) {
  2235. var index = indexes.indexOf(this.index);
  2236. if (index >= 0) {
  2237. this.viewed = false;
  2238. this.view(Math.max(this.index - (index + 1), 0));
  2239. } else {
  2240. addClass(this.items[this.index], CLASS_ACTIVE);
  2241. }
  2242. }
  2243. } else {
  2244. this.image = null;
  2245. this.viewed = false;
  2246. this.index = 0;
  2247. this.imageData = null;
  2248. this.canvas.innerHTML = '';
  2249. this.title.innerHTML = '';
  2250. }
  2251. }
  2252. } else {
  2253. this.build();
  2254. }
  2255. return this;
  2256. },
  2257. // Destroy the viewer
  2258. destroy: function destroy() {
  2259. var element = this.element,
  2260. options = this.options;
  2261. if (!getData(element, NAMESPACE)) {
  2262. return this;
  2263. }
  2264. this.destroyed = true;
  2265. if (this.ready) {
  2266. if (this.played) {
  2267. this.stop();
  2268. }
  2269. if (options.inline) {
  2270. if (this.fulled) {
  2271. this.exit();
  2272. }
  2273. this.unbind();
  2274. } else if (this.isShown) {
  2275. if (this.viewing) {
  2276. if (this.imageRendering) {
  2277. this.imageRendering.abort();
  2278. } else if (this.imageInitializing) {
  2279. this.imageInitializing.abort();
  2280. }
  2281. }
  2282. if (this.hiding) {
  2283. this.transitioning.abort();
  2284. }
  2285. this.hidden();
  2286. } else if (this.showing) {
  2287. this.transitioning.abort();
  2288. this.hidden();
  2289. }
  2290. this.ready = false;
  2291. this.viewer.parentNode.removeChild(this.viewer);
  2292. } else if (options.inline) {
  2293. if (this.delaying) {
  2294. this.delaying.abort();
  2295. } else if (this.initializing) {
  2296. this.initializing.abort();
  2297. }
  2298. }
  2299. if (!options.inline) {
  2300. removeListener(element, EVENT_CLICK, this.onStart);
  2301. }
  2302. removeData(element, NAMESPACE);
  2303. return this;
  2304. }
  2305. };
  2306. var others = {
  2307. open: function open() {
  2308. var body = this.body;
  2309. addClass(body, CLASS_OPEN);
  2310. body.style.paddingRight = this.scrollbarWidth + (parseFloat(this.initialBodyPaddingRight) || 0) + 'px';
  2311. },
  2312. close: function close() {
  2313. var body = this.body;
  2314. removeClass(body, CLASS_OPEN);
  2315. body.style.paddingRight = this.initialBodyPaddingRight;
  2316. },
  2317. shown: function shown() {
  2318. var element = this.element,
  2319. options = this.options;
  2320. this.fulled = true;
  2321. this.isShown = true;
  2322. this.render();
  2323. this.bind();
  2324. this.showing = false;
  2325. if (isFunction(options.shown)) {
  2326. addListener(element, EVENT_SHOWN, options.shown, {
  2327. once: true
  2328. });
  2329. }
  2330. if (dispatchEvent(element, EVENT_SHOWN) === false) {
  2331. return;
  2332. }
  2333. if (this.ready && this.isShown && !this.hiding) {
  2334. this.view(this.index);
  2335. }
  2336. },
  2337. hidden: function hidden() {
  2338. var element = this.element,
  2339. options = this.options;
  2340. this.fulled = false;
  2341. this.viewed = false;
  2342. this.isShown = false;
  2343. this.close();
  2344. this.unbind();
  2345. addClass(this.viewer, CLASS_HIDE);
  2346. this.resetList();
  2347. this.resetImage();
  2348. this.hiding = false;
  2349. if (!this.destroyed) {
  2350. if (isFunction(options.hidden)) {
  2351. addListener(element, EVENT_HIDDEN, options.hidden, {
  2352. once: true
  2353. });
  2354. }
  2355. dispatchEvent(element, EVENT_HIDDEN);
  2356. }
  2357. },
  2358. requestFullscreen: function requestFullscreen() {
  2359. var document = this.element.ownerDocument;
  2360. if (this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  2361. var documentElement = document.documentElement;
  2362. if (documentElement.requestFullscreen) {
  2363. documentElement.requestFullscreen();
  2364. } else if (documentElement.msRequestFullscreen) {
  2365. documentElement.msRequestFullscreen();
  2366. } else if (documentElement.mozRequestFullScreen) {
  2367. documentElement.mozRequestFullScreen();
  2368. } else if (documentElement.webkitRequestFullscreen) {
  2369. documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  2370. }
  2371. }
  2372. },
  2373. exitFullscreen: function exitFullscreen() {
  2374. if (this.fulled) {
  2375. var document = this.element.ownerDocument;
  2376. if (document.exitFullscreen) {
  2377. document.exitFullscreen();
  2378. } else if (document.msExitFullscreen) {
  2379. document.msExitFullscreen();
  2380. } else if (document.mozCancelFullScreen) {
  2381. document.mozCancelFullScreen();
  2382. } else if (document.webkitExitFullscreen) {
  2383. document.webkitExitFullscreen();
  2384. }
  2385. }
  2386. },
  2387. change: function change(e) {
  2388. var options = this.options,
  2389. pointers = this.pointers;
  2390. var pointer = pointers[Object.keys(pointers)[0]];
  2391. var offsetX = pointer.endX - pointer.startX;
  2392. var offsetY = pointer.endY - pointer.startY;
  2393. switch (this.action) {
  2394. // Move the current image
  2395. case ACTION_MOVE:
  2396. var userAgent = window.navigator.userAgent;
  2397. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  2398. {
  2399. this.moveSj(offsetX,offsetY);
  2400. }else {
  2401. this.move(offsetX, offsetY);
  2402. }
  2403. break;
  2404. // Zoom the current image
  2405. case ACTION_ZOOM:
  2406. this.zoom(getMaxZoomRatio(pointers), false, e);
  2407. break;
  2408. case ACTION_SWITCH:
  2409. this.action = 'switched';
  2410. // Empty `pointers` as `touchend` event will not be fired after swiped in iOS browsers.
  2411. this.pointers = {};
  2412. if (Math.abs(offsetX) > Math.abs(offsetY)) {
  2413. if (offsetX > 1) {
  2414. this.prev(options.loop);
  2415. } else if (offsetX < -1) {
  2416. this.next(options.loop);
  2417. }
  2418. }
  2419. break;
  2420. default:
  2421. }
  2422. // Override
  2423. forEach(pointers, function (p) {
  2424. p.startX = p.endX;
  2425. p.startY = p.endY;
  2426. });
  2427. },
  2428. isSwitchable: function isSwitchable() {
  2429. var imageData = this.imageData,
  2430. viewerData = this.viewerData;
  2431. return this.length > 1&&imageData.width <= viewerData.width && imageData.height <= viewerData.height;
  2432. // return this.length > 1&& imageData.left >= 0&& imageData.top >= 0 && imageData.width <= viewerData.width && imageData.height <= viewerData.height;
  2433. },
  2434. isSwitchable2: function isSwitchable2() {
  2435. var imageData = this.imageData,
  2436. image = this.image,
  2437. viewerData = this.viewerData,
  2438. canvas = this.canvas;
  2439. if (this.length <= 1)
  2440. {
  2441. return false;
  2442. }
  2443. var moveLeft=parseInt(this.ulLeft);
  2444. if (moveLeft<this.ulLeftInitial)
  2445. {
  2446. var gjLeft=parseInt(this.ulLeftInitial)-parseInt(1/3*viewerData.width);
  2447. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2448. if (diff>0)
  2449. {
  2450. gjLeft=gjLeft-diff;
  2451. }
  2452. if (moveLeft<gjLeft)
  2453. {
  2454. return true;
  2455. }
  2456. }else if (moveLeft>this.ulLeftInitial) {
  2457. if (moveLeft>parseInt(this.ulLeftInitial)+parseInt(1/3*viewerData.width))
  2458. {
  2459. return true;
  2460. }
  2461. }
  2462. return false;
  2463. },
  2464. isSwitchable3: function isSwitchable3() {
  2465. var image = this.image,
  2466. viewerData = this.viewerData;
  2467. if (this.length <= 1)
  2468. {
  2469. return ;
  2470. }
  2471. var moveLeft=parseInt(this.ulLeft);
  2472. if (moveLeft<this.ulLeftInitial)
  2473. {
  2474. var gjLeft2=parseInt(this.ulLeftInitial);
  2475. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2476. if (diff>0)
  2477. {
  2478. gjLeft2=gjLeft2-diff;
  2479. }
  2480. if (moveLeft>gjLeft2)
  2481. {
  2482. return ;
  2483. }else {
  2484. this.ulLeft=gjLeft2;
  2485. this.springMove(gjLeft2);
  2486. }
  2487. }else if (moveLeft>this.ulLeftInitial) {
  2488. this.springMove(this.ulLeftInitial);
  2489. }else {
  2490. this.springMove(this.ulLeftInitial);
  2491. }
  2492. return;
  2493. },
  2494. isSwitchable4: function isSwitchable4() {
  2495. var image = this.image,
  2496. viewerData = this.viewerData;
  2497. if (this.length <= 1)
  2498. {
  2499. return ;
  2500. }
  2501. var ul=this.canvas.querySelector("ul");
  2502. if (ul===null||ul===undefined)
  2503. {
  2504. return;
  2505. }
  2506. var moveLeft=parseInt(this.ulLeft);
  2507. if (moveLeft<this.ulLeftInitial)
  2508. {
  2509. var gjLeft2=parseInt(this.ulLeftInitial);
  2510. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2511. if (diff>0)
  2512. {
  2513. gjLeft2=gjLeft2-diff;
  2514. }
  2515. if (moveLeft>gjLeft2)
  2516. {
  2517. return ;
  2518. }else {
  2519. this.ulLeft=gjLeft2;
  2520. ul.style.marginLeft= this.ulLeft + 'px';
  2521. }
  2522. }else if (moveLeft>this.ulLeftInitial) {
  2523. this.ulLeft=this.ulLeftInitial;
  2524. ul.style.marginLeft= this.ulLeft + 'px';
  2525. }else {
  2526. this.ulLeft=this.ulLeftInitial;
  2527. ul.style.marginLeft= this.ulLeft + 'px';
  2528. }
  2529. return;
  2530. },
  2531. springMove:function springMove(iTarget) {
  2532. var than=this;
  2533. clearInterval(this.intervalTime);//清空定时器
  2534. than.intervalTime=null;
  2535. than.intervalTimeiTarget=null;
  2536. var iSpeed = 0;
  2537. this.intervalTime = setInterval(function(){ //开启定时器
  2538. var ul=than.canvas.querySelector("ul");
  2539. if (ul===null||ul===undefined)
  2540. {
  2541. clearInterval(than.intervalTime);
  2542. than.intervalTime=null;
  2543. return;
  2544. }
  2545. iSpeed += (iTarget - parseInt(ul.style.marginLeft))/6;
  2546. iSpeed *= 0.75;
  2547. if(
  2548. Math.abs(iSpeed)<=1&&
  2549. Math.abs(iTarget - parseInt(ul.style.marginLeft))<=1
  2550. ){
  2551. clearInterval(than.intervalTime);
  2552. than.intervalTime=null;
  2553. than.intervalTimeiTarget=null;
  2554. iSpeed = 0;
  2555. ul.style.marginLeft= iTarget + 'px';
  2556. }
  2557. else{
  2558. ul.style.marginLeft = (parseInt(ul.style.marginLeft)+ parseInt(iSpeed)).toString() + 'px';
  2559. }
  2560. },30);
  2561. than.intervalTimeiTarget=iTarget;
  2562. },
  2563. };
  2564. var AnotherViewer = WINDOW.Viewer;
  2565. var Viewer = function () {
  2566. /**
  2567. * Create a new Viewer.
  2568. * @param {Element} element - The target element for viewing.
  2569. * @param {Object} [options={}] - The configuration options.
  2570. */
  2571. function Viewer(element) {
  2572. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2573. classCallCheck(this, Viewer);
  2574. if (!element || element.nodeType !== 1) {
  2575. throw new Error('The first argument is required and must be an element.');
  2576. }
  2577. this.element = element;
  2578. this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
  2579. this.action = false;
  2580. this.fading = false;
  2581. this.fulled = false;
  2582. this.hiding = false;
  2583. this.index = 0;
  2584. this.isImg = false;
  2585. this.length = 0;
  2586. this.played = false;
  2587. this.playing = false;
  2588. this.pointers = {};
  2589. this.ready = false;
  2590. this.showing = false;
  2591. this.timeout = false;
  2592. this.tooltipping = false;
  2593. this.viewed = false;
  2594. this.viewing = false;
  2595. this.isShown = false;
  2596. this.wheeling = false;
  2597. this.init();
  2598. }
  2599. createClass(Viewer, [{
  2600. key: 'init',
  2601. value: function init() {
  2602. var _this = this;
  2603. var element = this.element,
  2604. options = this.options;
  2605. if (getData(element, NAMESPACE)) {
  2606. return;
  2607. }
  2608. setData(element, NAMESPACE, this);
  2609. var isImg = element.tagName.toLowerCase() === 'img';
  2610. var images = [];
  2611. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  2612. if (isFunction(options.filter)) {
  2613. if (options.filter.call(_this, image)) {
  2614. images.push(image);
  2615. }
  2616. } else {
  2617. images.push(image);
  2618. }
  2619. });
  2620. if (!images.length) {
  2621. return;
  2622. }
  2623. this.isImg = isImg;
  2624. this.length = images.length;
  2625. this.images = images;
  2626. this.dealOriginal=[];
  2627. var ownerDocument = element.ownerDocument;
  2628. var body = ownerDocument.body || ownerDocument.documentElement;
  2629. this.body = body;
  2630. this.scrollbarWidth = window.innerWidth - ownerDocument.documentElement.clientWidth;
  2631. this.initialBodyPaddingRight = window.getComputedStyle(body).paddingRight;
  2632. // Override `transition` option if it is not supported
  2633. if (isUndefined(document.createElement(NAMESPACE).style.transition)) {
  2634. options.transition = false;
  2635. }
  2636. if (options.inline) {
  2637. var count = 0;
  2638. var progress = function progress() {
  2639. count += 1;
  2640. if (count === _this.length) {
  2641. var timeout = void 0;
  2642. _this.initializing = false;
  2643. _this.delaying = {
  2644. abort: function abort() {
  2645. clearTimeout(timeout);
  2646. }
  2647. };
  2648. // build asynchronously to keep `this.viewer` is accessible in `ready` event handler.
  2649. timeout = setTimeout(function () {
  2650. _this.delaying = false;
  2651. _this.build();
  2652. }, 0);
  2653. }
  2654. };
  2655. this.initializing = {
  2656. abort: function abort() {
  2657. forEach(images, function (image) {
  2658. if (!image.complete) {
  2659. removeListener(image, EVENT_LOAD, progress);
  2660. }
  2661. });
  2662. }
  2663. };
  2664. forEach(images, function (image) {
  2665. if (image.complete) {
  2666. progress();
  2667. } else {
  2668. addListener(image, EVENT_LOAD, progress, {
  2669. once: true
  2670. });
  2671. }
  2672. });
  2673. } else {
  2674. addListener(element, EVENT_CLICK, this.onStart = function (_ref) {
  2675. var target = _ref.target;
  2676. var dataUpdate=document.getElementById("docsPictures").getAttribute('data-update');
  2677. if (dataUpdate=='1')
  2678. {
  2679. _this.update();
  2680. document.getElementById("docsPictures").setAttribute('data-update','0');
  2681. }
  2682. if (target.tagName.toLowerCase() === 'img') {
  2683. _this.view(_this.images.indexOf(target));
  2684. }
  2685. });
  2686. }
  2687. }
  2688. }, {
  2689. key: 'build',
  2690. value: function build() {
  2691. if (this.ready) {
  2692. return;
  2693. }
  2694. var element = this.element,
  2695. options = this.options;
  2696. var parent = element.parentNode;
  2697. var template = document.createElement('div');
  2698. template.innerHTML = TEMPLATE;
  2699. var viewer = template.querySelector('.' + NAMESPACE + '-container');
  2700. var title = viewer.querySelector('.' + NAMESPACE + '-title');
  2701. var toolbar = viewer.querySelector('.' + NAMESPACE + '-toolbar');
  2702. var navbar = viewer.querySelector('.' + NAMESPACE + '-navbar');
  2703. var button = viewer.querySelector('.' + NAMESPACE + '-button');
  2704. var canvas = viewer.querySelector('.' + NAMESPACE + '-canvas');
  2705. if ($(window).width()>700)
  2706. {
  2707. toolbar.style.display = "flex";
  2708. toolbar.style.justifyContent= "center";
  2709. }
  2710. this.parent = parent;
  2711. this.viewer = viewer;
  2712. this.title = title;
  2713. this.toolbar = toolbar;
  2714. this.navbar = navbar;
  2715. this.button = button;
  2716. this.canvas = canvas;
  2717. this.footer = viewer.querySelector('.' + NAMESPACE + '-footer');
  2718. this.tooltipBox = viewer.querySelector('.' + NAMESPACE + '-tooltip');
  2719. this.player = viewer.querySelector('.' + NAMESPACE + '-player');
  2720. this.list = viewer.querySelector('.' + NAMESPACE + '-list');
  2721. addClass(title, !options.title ? CLASS_HIDE : getResponsiveClass(options.title));
  2722. addClass(navbar, !options.navbar ? CLASS_HIDE : getResponsiveClass(options.navbar));
  2723. toggleClass(button, CLASS_HIDE, !options.button);
  2724. if (options.backdrop) {
  2725. addClass(viewer, NAMESPACE + '-backdrop');
  2726. if (!options.inline && options.backdrop === true) {
  2727. setData(canvas, 'action', 'hide');
  2728. }
  2729. }
  2730. if (options.toolbar) {
  2731. var list = document.createElement('ul');
  2732. var custom = isPlainObject(options.toolbar);
  2733. var zoomButtons = BUTTONS.slice(0, 3);
  2734. var rotateButtons = BUTTONS.slice(7, 9);
  2735. var scaleButtons = BUTTONS.slice(9);
  2736. if (!custom) {
  2737. addClass(toolbar, getResponsiveClass(options.toolbar));
  2738. }
  2739. forEach(custom ? options.toolbar : BUTTONS, function (value, index) {
  2740. var deep = custom && isPlainObject(value);
  2741. var name = custom ? hyphenate(index) : value;
  2742. var show = deep && !isUndefined(value.show) ? value.show : value;
  2743. if (!show || !options.zoomable && zoomButtons.indexOf(name) !== -1 || !options.rotatable && rotateButtons.indexOf(name) !== -1 || !options.scalable && scaleButtons.indexOf(name) !== -1) {
  2744. return;
  2745. }
  2746. var size = deep && !isUndefined(value.size) ? value.size : value;
  2747. var click = deep && !isUndefined(value.click) ? value.click : value;
  2748. var item = document.createElement('li');
  2749. item.setAttribute('role', 'button');
  2750. addClass(item, NAMESPACE + '-' + name);
  2751. if (!isFunction(click)) {
  2752. setData(item, 'action', name);
  2753. }
  2754. if (isNumber(show)) {
  2755. addClass(item, getResponsiveClass(show));
  2756. }
  2757. if (['small', 'large'].indexOf(size) !== -1) {
  2758. addClass(item, NAMESPACE + '-' + size);
  2759. } else if (name === 'play') {
  2760. addClass(item, NAMESPACE + '-large');
  2761. }
  2762. if (isFunction(click)) {
  2763. addListener(item, EVENT_CLICK, click);
  2764. }
  2765. list.appendChild(item);
  2766. });
  2767. toolbar.appendChild(list);
  2768. } else {
  2769. addClass(toolbar, CLASS_HIDE);
  2770. }
  2771. if (!options.rotatable) {
  2772. var rotates = toolbar.querySelectorAll('li[class*="rotate"]');
  2773. addClass(rotates, CLASS_INVISIBLE);
  2774. forEach(rotates, function (rotate) {
  2775. toolbar.appendChild(rotate);
  2776. });
  2777. }
  2778. if (options.inline) {
  2779. addClass(button, CLASS_FULLSCREEN);
  2780. setStyle(viewer, {
  2781. zIndex: options.zIndexInline
  2782. });
  2783. if (window.getComputedStyle(parent).position === 'static') {
  2784. setStyle(parent, {
  2785. position: 'relative'
  2786. });
  2787. }
  2788. parent.insertBefore(viewer, element.nextSibling);
  2789. } else {
  2790. addClass(button, CLASS_CLOSE);
  2791. addClass(viewer, CLASS_FIXED);
  2792. addClass(viewer, CLASS_FADE);
  2793. addClass(viewer, CLASS_HIDE);
  2794. setStyle(viewer, {
  2795. zIndex: options.zIndex
  2796. });
  2797. var container = options.container;
  2798. if (isString(container)) {
  2799. container = element.ownerDocument.querySelector(container);
  2800. }
  2801. if (!container) {
  2802. container = this.body;
  2803. }
  2804. container.appendChild(viewer);
  2805. }
  2806. if (options.inline) {
  2807. this.render();
  2808. this.bind();
  2809. this.isShown = true;
  2810. }
  2811. this.ready = true;
  2812. if (isFunction(options.ready)) {
  2813. addListener(element, EVENT_READY, options.ready, {
  2814. once: true
  2815. });
  2816. }
  2817. if (dispatchEvent(element, EVENT_READY) === false) {
  2818. this.ready = false;
  2819. return;
  2820. }
  2821. if (this.ready && options.inline) {
  2822. this.view();
  2823. }
  2824. }
  2825. /**
  2826. * Get the no conflict viewer class.
  2827. * @returns {Viewer} The viewer class.
  2828. */
  2829. }], [{
  2830. key: 'noConflict',
  2831. value: function noConflict() {
  2832. window.Viewer = AnotherViewer;
  2833. return Viewer;
  2834. }
  2835. /**
  2836. * Change the default options.
  2837. * @param {Object} options - The new default options.
  2838. */
  2839. }, {
  2840. key: 'setDefaults',
  2841. value: function setDefaults(options) {
  2842. assign(DEFAULTS, isPlainObject(options) && options);
  2843. }
  2844. }]);
  2845. return Viewer;
  2846. }();
  2847. assign(Viewer.prototype, render, events, handlers, methods, others);
  2848. if ($.fn) {
  2849. var AnotherViewer$1 = $.fn.viewer;
  2850. var NAMESPACE$1 = 'viewer';
  2851. $.fn.viewer = function jQueryViewer(option) {
  2852. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  2853. args[_key - 1] = arguments[_key];
  2854. }
  2855. var result = void 0;
  2856. this.each(function (i, element) {
  2857. var $element = $(element);
  2858. var isDestroy = option === 'destroy';
  2859. var viewer = $element.data(NAMESPACE$1);
  2860. if (!viewer) {
  2861. if (isDestroy) {
  2862. return;
  2863. }
  2864. var options = $.extend({}, $element.data(), $.isPlainObject(option) && option);
  2865. viewer = new Viewer(element, options);
  2866. $element.data(NAMESPACE$1, viewer);
  2867. }
  2868. if (typeof option === 'string') {
  2869. var fn = viewer[option];
  2870. if ($.isFunction(fn)) {
  2871. result = fn.apply(viewer, args);
  2872. if (result === viewer) {
  2873. result = undefined;
  2874. }
  2875. if (isDestroy) {
  2876. $element.removeData(NAMESPACE$1);
  2877. }
  2878. }
  2879. }
  2880. });
  2881. return result !== undefined ? result : this;
  2882. };
  2883. $.fn.viewer.Constructor = Viewer;
  2884. $.fn.viewer.setDefaults = Viewer.setDefaults;
  2885. $.fn.viewer.noConflict = function noConflict() {
  2886. $.fn.viewer = AnotherViewer$1;
  2887. return this;
  2888. };
  2889. }
  2890. })));