viewer.js 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  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. //pxn
  906. var userAgent = window.navigator.userAgent;
  907. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  908. {
  909. var imageLi=image.parentNode;
  910. if (image.width>viewerData.width)
  911. {
  912. imageLi.style.width='auto';
  913. if (imageData.left<0)
  914. {
  915. imageLi.style.paddingLeft =(imageData.left*-1).toString()+'px';
  916. }
  917. }else {
  918. imageLi.style.width=viewerData.width.toString()+'px';
  919. imageLi.style.paddingLeft='0';
  920. }
  921. this.isSwitchable4();
  922. }
  923. },
  924. resetImage: function resetImage() {
  925. // this.image only defined after viewed
  926. if (this.viewing || this.viewed) {
  927. var image = this.image;
  928. if (this.viewing) {
  929. this.viewing.abort();
  930. }
  931. image.parentNode.removeChild(image);
  932. this.image = null;
  933. }
  934. }
  935. };
  936. var events = {
  937. bind: function bind() {
  938. var element = this.element,
  939. viewer = this.viewer;
  940. addListener(viewer, EVENT_CLICK, this.onClick = this.click.bind(this));
  941. addListener(viewer, EVENT_WHEEL, this.onWheel = this.wheel.bind(this));
  942. addListener(viewer, EVENT_DRAG_START, this.onDragStart = this.dragstart.bind(this));
  943. addListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown = this.pointerdown.bind(this));
  944. addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove = this.pointermove.bind(this));
  945. addListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp = this.pointerup.bind(this));
  946. addListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown = this.keydown.bind(this));
  947. addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
  948. },
  949. unbind: function unbind() {
  950. var element = this.element,
  951. viewer = this.viewer;
  952. removeListener(viewer, EVENT_CLICK, this.onClick);
  953. removeListener(viewer, EVENT_WHEEL, this.onWheel);
  954. removeListener(viewer, EVENT_DRAG_START, this.onDragStart);
  955. removeListener(this.canvas, EVENT_POINTER_DOWN, this.onPointerDown);
  956. removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onPointerMove);
  957. removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onPointerUp);
  958. removeListener(element.ownerDocument, EVENT_KEY_DOWN, this.onKeyDown);
  959. removeListener(window, EVENT_RESIZE, this.onResize);
  960. }
  961. };
  962. var handlers = {
  963. click: function click(_ref) {
  964. var target = _ref.target;
  965. var options = this.options,
  966. imageData = this.imageData;
  967. var action = getData(target, 'action');
  968. switch (action) {
  969. case 'mix':
  970. if (this.played) {
  971. this.stop();
  972. } else if (options.inline) {
  973. if (this.fulled) {
  974. this.exit();
  975. } else {
  976. this.full();
  977. }
  978. } else {
  979. this.hide();
  980. }
  981. break;
  982. case 'hide':
  983. this.hide();
  984. break;
  985. case 'view':
  986. this.view(getData(target, 'index'));
  987. break;
  988. case 'zoom-ins':
  989. this.zoom(0.1, true);
  990. break;
  991. case 'zoom-outs':
  992. this.zoom(-0.1, true);
  993. break;
  994. case 'reset':
  995. this.reset();
  996. break;
  997. case 'prevs':
  998. this.prev(options.loop);
  999. break;
  1000. case 'plays':
  1001. this.play(options.fullscreen);
  1002. break;
  1003. case 'nexts':
  1004. this.next(options.loop);
  1005. break;
  1006. case 'one-to-ones':
  1007. case 'one-to-ones2':
  1008. this.normal();
  1009. break;
  1010. //放大
  1011. case 'enlarge':
  1012. this.toggle();
  1013. break;
  1014. case 'toolbar':
  1015. break;
  1016. //case 'rotate-left':
  1017. // this.rotate(-90);
  1018. // break;
  1019. //
  1020. //case 'rotate-right':
  1021. // this.rotate(90);
  1022. // break;
  1023. //
  1024. //case 'flip-horizontal':
  1025. // this.scaleX(-imageData.scaleX || -1);
  1026. // break;
  1027. //
  1028. //case 'flip-vertical':
  1029. // this.scaleY(-imageData.scaleY || -1);
  1030. // break;
  1031. default:
  1032. if (this.played) {
  1033. this.stop();
  1034. return;
  1035. }
  1036. if (target.className ==""
  1037. ||target.className =="viewer-toolbar"
  1038. ||target.className =="viewer-footer"
  1039. ||target.className =="viewer-title")
  1040. {
  1041. return;
  1042. }
  1043. if (target.className==='viewer-move'&&this.myMove !== undefined && this.myMove === true)
  1044. {
  1045. return;
  1046. }
  1047. this.hide();
  1048. }
  1049. },
  1050. load: function load() {
  1051. var _this = this;
  1052. if (this.timeout) {
  1053. clearTimeout(this.timeout);
  1054. this.timeout = false;
  1055. }
  1056. var element = this.element,
  1057. options = this.options,
  1058. image = this.image,
  1059. index = this.index,
  1060. viewerData = this.viewerData;
  1061. removeClass(image, CLASS_INVISIBLE);
  1062. if (options.loading) {
  1063. removeClass(this.canvas, CLASS_LOADING);
  1064. }
  1065. 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;';
  1066. this.initImage(function () {
  1067. toggleClass(image, CLASS_MOVE, options.movable);
  1068. toggleClass(image, CLASS_TRANSITION, options.transition);
  1069. _this.renderImage(function () {
  1070. _this.viewed = true;
  1071. _this.viewing = false;
  1072. if (isFunction(options.viewed)) {
  1073. addListener(element, EVENT_VIEWED, options.viewed, {
  1074. once: true
  1075. });
  1076. }
  1077. dispatchEvent(element, EVENT_VIEWED, {
  1078. originalImage: _this.images[index],
  1079. index: index,
  1080. image: image
  1081. });
  1082. });
  1083. });
  1084. },
  1085. loadImage: function loadImage(e) {
  1086. var image = e.target;
  1087. var parent = image.parentNode;
  1088. var parentWidth = parent.offsetWidth || 30;
  1089. var parentHeight = parent.offsetHeight || 50;
  1090. var filled = !!getData(image, 'filled');
  1091. getImageNaturalSizes(image, function (naturalWidth, naturalHeight) {
  1092. var aspectRatio = naturalWidth / naturalHeight;
  1093. var width = parentWidth;
  1094. var height = parentHeight;
  1095. if (parentHeight * aspectRatio > parentWidth) {
  1096. if (filled) {
  1097. width = parentHeight * aspectRatio;
  1098. } else {
  1099. height = parentWidth / aspectRatio;
  1100. }
  1101. } else if (filled) {
  1102. height = parentWidth / aspectRatio;
  1103. } else {
  1104. width = parentHeight * aspectRatio;
  1105. }
  1106. setStyle(image, assign({
  1107. width: width,
  1108. height: height
  1109. }, getTransforms({
  1110. translateX: (parentWidth - width) / 2,
  1111. translateY: (parentHeight - height) / 2
  1112. })));
  1113. });
  1114. },
  1115. keydown: function keydown(e) {
  1116. var options = this.options;
  1117. if (!this.fulled || !options.keyboard) {
  1118. return;
  1119. }
  1120. switch (e.keyCode || e.which || e.charCode) {
  1121. // Escape
  1122. case 27:
  1123. if (this.played) {
  1124. this.stop();
  1125. } else if (options.inline) {
  1126. if (this.fulled) {
  1127. this.exit();
  1128. }
  1129. } else {
  1130. this.hide();
  1131. }
  1132. break;
  1133. // Space
  1134. case 32:
  1135. if (this.played) {
  1136. this.stop();
  1137. }
  1138. break;
  1139. // ArrowLeft
  1140. case 37:
  1141. this.prev(options.loop);
  1142. break;
  1143. // ArrowUp
  1144. case 38:
  1145. // Prevent scroll on Firefox
  1146. e.preventDefault();
  1147. // Zoom in
  1148. this.zoom(options.zoomRatio, true);
  1149. break;
  1150. // ArrowRight
  1151. case 39:
  1152. this.next(options.loop);
  1153. break;
  1154. // ArrowDown
  1155. case 40:
  1156. // Prevent scroll on Firefox
  1157. e.preventDefault();
  1158. // Zoom out
  1159. this.zoom(-options.zoomRatio, true);
  1160. break;
  1161. // Ctrl + 0
  1162. case 48:
  1163. // Fall through
  1164. // Ctrl + 1
  1165. // eslint-disable-next-line no-fallthrough
  1166. case 49:
  1167. if (e.ctrlKey) {
  1168. e.preventDefault();
  1169. this.toggle();
  1170. }
  1171. break;
  1172. default:
  1173. }
  1174. },
  1175. dragstart: function dragstart(e) {
  1176. if (e.target.tagName.toLowerCase() === 'img') {
  1177. e.preventDefault();
  1178. }
  1179. },
  1180. pointerdown: function pointerdown(e) {
  1181. var options = this.options,
  1182. pointers = this.pointers;
  1183. if (!this.viewed || this.showing || this.viewing || this.hiding) {
  1184. return;
  1185. }
  1186. this.myMove=false;
  1187. if (this.intervalTime!==undefined)
  1188. {
  1189. clearInterval(this.intervalTime);
  1190. this.intervalTime=null;
  1191. if (this.intervalTimeiTarget!==undefined&&this.intervalTimeiTarget!==null)
  1192. {
  1193. var ul=this.canvas.querySelector("ul");
  1194. if (ul!==null&&ul!==undefined)
  1195. {
  1196. this.ulLeft=this.intervalTimeiTarget;
  1197. ul.style.marginLeft =this.intervalTimeiTarget+ 'px';
  1198. }
  1199. this.intervalTimeiTarget=null;
  1200. }
  1201. }
  1202. if (e.changedTouches) {
  1203. forEach(e.changedTouches, function (touch) {
  1204. pointers[touch.identifier] = getPointer(touch);
  1205. });
  1206. } else {
  1207. pointers[e.pointerId || 0] = getPointer(e);
  1208. }
  1209. var action = options.movable ? ACTION_MOVE : false;
  1210. if (Object.keys(pointers).length > 1) {
  1211. action = ACTION_ZOOM;
  1212. } else if ((e.pointerType === 'touch' || e.type === 'touchstart')) {
  1213. var ul=this.canvas.querySelector("ul");
  1214. if (ul!==null&&ul!==undefined)
  1215. this.ulLeft= parseInt(ul.style.marginLeft);
  1216. }
  1217. this.action = action;
  1218. },
  1219. pointermove: function pointermove(e) {
  1220. var options = this.options,
  1221. pointers = this.pointers,
  1222. action = this.action,
  1223. image = this.image;
  1224. if (!this.viewed || !action) {
  1225. return;
  1226. }
  1227. this.myMove=true;
  1228. e.preventDefault();
  1229. if (e.changedTouches) {
  1230. forEach(e.changedTouches, function (touch) {
  1231. assign(pointers[touch.identifier], getPointer(touch, true));
  1232. });
  1233. } else {
  1234. var pointerTmep=pointers[e.pointerId || 0];
  1235. if (pointerTmep==null||pointerTmep==undefined)
  1236. {
  1237. return;
  1238. }
  1239. assign(pointerTmep, getPointer(e, true));
  1240. }
  1241. if (action === ACTION_MOVE && options.transition && hasClass(image, CLASS_TRANSITION)) {
  1242. removeClass(image, CLASS_TRANSITION);
  1243. }
  1244. if (Object.keys(pointers).length > 1|| action===ACTION_SWITCH) {
  1245. }else if ((e.pointerType === 'touch' || e.type === 'touchmove') && this.isSwitchable2()) {
  1246. this.action = ACTION_SWITCH;
  1247. }
  1248. this.change(e);
  1249. },
  1250. pointerup: function pointerup(e) {
  1251. var action = this.action,
  1252. pointers = this.pointers;
  1253. if (e.changedTouches) {
  1254. forEach(e.changedTouches, function (touch) {
  1255. delete pointers[touch.identifier];
  1256. });
  1257. } else {
  1258. delete pointers[e.pointerId || 0];
  1259. }
  1260. if (!action) {
  1261. return;
  1262. }
  1263. if (action === ACTION_MOVE && this.options.transition) {
  1264. addClass(this.image, CLASS_TRANSITION);
  1265. }
  1266. var userAgent = window.navigator.userAgent;
  1267. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1268. {
  1269. if (Object.keys(pointers).length<=1&&action===ACTION_MOVE) {
  1270. this.isSwitchable3();
  1271. }
  1272. }
  1273. this.action = false;
  1274. },
  1275. resize: function resize() {
  1276. var _this2 = this;
  1277. if (!this.isShown || this.hiding) {
  1278. return;
  1279. }
  1280. this.initContainer();
  1281. this.initViewer();
  1282. this.renderViewer();
  1283. this.renderList();
  1284. if (this.viewed) {
  1285. this.initImage(function () {
  1286. _this2.renderImage();
  1287. });
  1288. }
  1289. if (this.played) {
  1290. if (this.options.fullscreen && this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  1291. this.stop();
  1292. return;
  1293. }
  1294. forEach(this.player.getElementsByTagName('img'), function (image) {
  1295. addListener(image, EVENT_LOAD, _this2.loadImage.bind(_this2), {
  1296. once: true
  1297. });
  1298. dispatchEvent(image, EVENT_LOAD);
  1299. });
  1300. }
  1301. },
  1302. wheel: function wheel(e) {
  1303. var _this3 = this;
  1304. if (!this.viewed) {
  1305. return;
  1306. }
  1307. e.preventDefault();
  1308. // Limit wheel speed to prevent zoom too fast
  1309. if (this.wheeling) {
  1310. return;
  1311. }
  1312. this.wheeling = true;
  1313. setTimeout(function () {
  1314. _this3.wheeling = false;
  1315. }, 50);
  1316. var ratio = Number(this.options.zoomRatio) || 0.1;
  1317. var delta = 1;
  1318. if (e.deltaY) {
  1319. delta = e.deltaY > 0 ? 1 : -1;
  1320. } else if (e.wheelDelta) {
  1321. delta = -e.wheelDelta / 120;
  1322. } else if (e.detail) {
  1323. delta = e.detail > 0 ? 1 : -1;
  1324. }
  1325. this.zoom(-delta * ratio, true, e);
  1326. }
  1327. };
  1328. var methods = {
  1329. /** Show the viewer (only available in modal mode)
  1330. * @param {boolean} [immediate=false] - Indicates if show the viewer immediately or not.
  1331. * @returns {Viewer} this
  1332. */
  1333. show: function show() {
  1334. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1335. var element = this.element,
  1336. options = this.options;
  1337. if (options.inline || this.showing || this.isShown || this.showing) {
  1338. return this;
  1339. }
  1340. if (!this.ready) {
  1341. this.build();
  1342. if (this.ready) {
  1343. this.show(immediate);
  1344. }
  1345. return this;
  1346. }
  1347. if (isFunction(options.show)) {
  1348. addListener(element, EVENT_SHOW, options.show, {
  1349. once: true
  1350. });
  1351. }
  1352. if (dispatchEvent(element, EVENT_SHOW) === false || !this.ready) {
  1353. return this;
  1354. }
  1355. if (this.hiding) {
  1356. this.transitioning.abort();
  1357. }
  1358. this.showing = true;
  1359. this.open();
  1360. var viewer = this.viewer;
  1361. removeClass(viewer, CLASS_HIDE);
  1362. if (options.transition && !immediate) {
  1363. var shown = this.shown.bind(this);
  1364. this.transitioning = {
  1365. abort: function abort() {
  1366. removeListener(viewer, EVENT_TRANSITION_END, shown);
  1367. removeClass(viewer, CLASS_IN);
  1368. }
  1369. };
  1370. addClass(viewer, CLASS_TRANSITION);
  1371. // Force reflow to enable CSS3 transition
  1372. // eslint-disable-next-line
  1373. viewer.offsetWidth;
  1374. addListener(viewer, EVENT_TRANSITION_END, shown, {
  1375. once: true
  1376. });
  1377. addClass(viewer, CLASS_IN);
  1378. } else {
  1379. addClass(viewer, CLASS_IN);
  1380. this.shown();
  1381. }
  1382. return this;
  1383. },
  1384. /**
  1385. * Hide the viewer (only available in modal mode)
  1386. * @param {boolean} [immediate=false] - Indicates if hide the viewer immediately or not.
  1387. * @returns {Viewer} this
  1388. */
  1389. hide: function hide() {
  1390. var immediate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1391. var element = this.element,
  1392. options = this.options;
  1393. if (options.inline || this.hiding || !(this.isShown || this.showing)) {
  1394. return this;
  1395. }
  1396. if (isFunction(options.hide)) {
  1397. addListener(element, EVENT_HIDE, options.hide, {
  1398. once: true
  1399. });
  1400. }
  1401. if (dispatchEvent(element, EVENT_HIDE) === false) {
  1402. return this;
  1403. }
  1404. if (this.showing) {
  1405. this.transitioning.abort();
  1406. }
  1407. this.hiding = true;
  1408. if (this.played) {
  1409. this.stop();
  1410. } else if (this.viewing) {
  1411. this.viewing.abort();
  1412. }
  1413. var viewer = this.viewer;
  1414. if (options.transition && !immediate) {
  1415. var hidden = this.hidden.bind(this);
  1416. var hide = function hide() {
  1417. addListener(viewer, EVENT_TRANSITION_END, hidden, {
  1418. once: true
  1419. });
  1420. removeClass(viewer, CLASS_IN);
  1421. };
  1422. this.transitioning = {
  1423. abort: function abort() {
  1424. if (this.viewed) {
  1425. removeListener(this.image, EVENT_TRANSITION_END, hide);
  1426. } else {
  1427. removeListener(viewer, EVENT_TRANSITION_END, hidden);
  1428. }
  1429. }
  1430. };
  1431. if (this.viewed) {
  1432. addListener(this.image, EVENT_TRANSITION_END, hide, {
  1433. once: true
  1434. });
  1435. this.zoomTo(0, false, false, true);
  1436. } else {
  1437. hide();
  1438. }
  1439. } else {
  1440. removeClass(viewer, CLASS_IN);
  1441. this.hidden();
  1442. }
  1443. return this;
  1444. },
  1445. /**
  1446. * View one of the images with image's index
  1447. * @param {number} index - The index of the image to view.
  1448. * @returns {Viewer} this
  1449. */
  1450. view: function view() {
  1451. var _this = this;
  1452. var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  1453. var mandatory = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1454. index = Number(index) || 0;
  1455. if (!this.isShown) {
  1456. this.index = index;
  1457. return this.show();
  1458. }
  1459. if (this.hiding || this.played || index < 0 || index >= this.length) {
  1460. return this;
  1461. }
  1462. if (!mandatory)
  1463. {
  1464. if (this.viewed && index === this.index) {
  1465. return this;
  1466. }
  1467. }
  1468. if (this.viewing) {
  1469. this.viewing.abort();
  1470. }
  1471. var element = this.element,
  1472. options = this.options,
  1473. title = this.title,
  1474. canvas = this.canvas;
  1475. var item = this.items[index];
  1476. var img = item.querySelector('img');
  1477. var url = getData(img, 'originalUrl');
  1478. var alt = img.getAttribute('alt');
  1479. var image = document.createElement('img');
  1480. image.src = url;
  1481. image.alt = alt;
  1482. image.style.display='none';
  1483. if (isFunction(options.view)) {
  1484. addListener(element, EVENT_VIEW, options.view, {
  1485. once: true
  1486. });
  1487. }
  1488. if (dispatchEvent(element, EVENT_VIEW, {
  1489. originalImage: this.images[index],
  1490. index: index,
  1491. image: image
  1492. }) === false || !this.isShown || this.hiding || this.played) {
  1493. return this;
  1494. }
  1495. this.image = image;
  1496. removeClass(this.items[this.index], CLASS_ACTIVE);
  1497. addClass(item, CLASS_ACTIVE);
  1498. this.viewed = false;
  1499. this.index = index;
  1500. this.imageData = {};
  1501. addClass(image, CLASS_INVISIBLE);
  1502. if (options.loading) {
  1503. addClass(canvas, CLASS_LOADING);
  1504. }
  1505. canvas.innerHTML = '';
  1506. var viewerData = _this.viewerData;
  1507. this.intervalTime=null;
  1508. this.ulLeft= this.ulLeftInitial=-10;
  1509. var is_sj=false;
  1510. var imageBf=null;
  1511. var imageAf=null;
  1512. var liBf=null;
  1513. var liAf=null;
  1514. var ul=null;
  1515. var userAgent = window.navigator.userAgent;
  1516. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1517. {
  1518. is_sj=true;
  1519. }
  1520. if (is_sj)
  1521. {
  1522. ul = document.createElement('ul');
  1523. if (index-1>=0)
  1524. {
  1525. var itemBf = this.items[index-1];
  1526. var imgBf = itemBf.querySelector('img');
  1527. var urlBf = getData(imgBf, 'originalUrl');
  1528. var altBf = imgBf.getAttribute('alt');
  1529. imageBf = document.createElement('img');
  1530. imageBf.src = urlBf;
  1531. imageBf.alt = altBf;
  1532. imageBf.style.display='none';
  1533. imageBf.style.width='100%';
  1534. setData(imageBf, 'action', 'img');
  1535. liBf = document.createElement('li');
  1536. liBf.appendChild(imageBf);
  1537. liBf.style.width=viewerData.width.toString()+'px';
  1538. ul.appendChild(liBf);
  1539. }
  1540. var li = document.createElement('li');
  1541. li.appendChild(image);
  1542. li.style.width=viewerData.width.toString()+'px';
  1543. ul.appendChild(li);
  1544. if (index+1<this.length)
  1545. {
  1546. var itemAf = this.items[index+1];
  1547. var imgAf = itemAf.querySelector('img');
  1548. var urlAf = getData(imgAf, 'originalUrl');
  1549. var altAf = imgAf.getAttribute('alt');
  1550. imageAf = document.createElement('img');
  1551. imageAf.src = urlAf;
  1552. imageAf.alt = altAf;
  1553. imageAf.style.display='none';
  1554. imageAf.style.width='100%';
  1555. setData(imageAf, 'action', 'img');
  1556. liAf = document.createElement('li');
  1557. liAf.appendChild(imageAf);
  1558. liAf.style.width=viewerData.width.toString()+'px';
  1559. ul.appendChild(liAf);
  1560. }
  1561. canvas.appendChild(ul);
  1562. }else {
  1563. canvas.appendChild(image);
  1564. }
  1565. // Center current item
  1566. this.renderList();
  1567. // Clear title
  1568. // title.innerHTML = '';
  1569. title.textContent = (_this.index+1)+'/'+options.totalCount;
  1570. if (this.dealOriginal.indexOf(url)>-1)
  1571. {
  1572. this.toolbar.querySelector(".viewer-one-to-ones2").style.display='block';
  1573. this.toolbar.querySelector(".viewer-one-to-ones").style.display='none';
  1574. }else {
  1575. this.toolbar.querySelector(".viewer-one-to-ones").style.display='block';
  1576. this.toolbar.querySelector(".viewer-one-to-ones2").style.display='none';
  1577. }
  1578. // Generate title after viewed
  1579. var onViewed = function onViewed() {
  1580. // var imageData = _this.imageData;
  1581. image.style.display='block';
  1582. if (is_sj)
  1583. {
  1584. if (imageBf!=null)
  1585. {
  1586. _this.ulLeft= _this.ulLeftInitial=-viewerData.width-20;
  1587. }
  1588. ul.style.marginLeft=_this.ulLeft.toString()+'px';
  1589. if (imageBf!=null)
  1590. {
  1591. imageBf.style.height=image.style.height;
  1592. imageBf.style.marginTop=image.style.marginTop;
  1593. imageBf.style.display='block';
  1594. }
  1595. if (imageAf!=null) {
  1596. imageAf.style.height=image.style.height;
  1597. imageAf.style.marginTop=image.style.marginTop;
  1598. imageAf.style.display='block';
  1599. }
  1600. }
  1601. // title.textContent = alt + ' (' + imageData.naturalWidth + ' \xD7 ' + imageData.naturalHeight + ')';
  1602. };
  1603. var onLoad = void 0;
  1604. addListener(element, EVENT_VIEWED, onViewed, {
  1605. once: true
  1606. });
  1607. this.viewing = {
  1608. abort: function abort() {
  1609. removeListener(element, EVENT_VIEWED, onViewed);
  1610. if (image.complete) {
  1611. if (this.imageRendering) {
  1612. this.imageRendering.abort();
  1613. } else if (this.imageInitializing) {
  1614. this.imageInitializing.abort();
  1615. }
  1616. } else {
  1617. removeListener(image, EVENT_LOAD, onLoad);
  1618. if (this.timeout) {
  1619. clearTimeout(this.timeout);
  1620. }
  1621. }
  1622. }
  1623. };
  1624. if (image.complete) {
  1625. this.load();
  1626. } else {
  1627. addListener(image, EVENT_LOAD, onLoad = this.load.bind(this), {
  1628. once: true
  1629. });
  1630. if (this.timeout) {
  1631. clearTimeout(this.timeout);
  1632. }
  1633. // Make the image visible if it fails to load within 1s
  1634. this.timeout = setTimeout(function () {
  1635. removeClass(image, CLASS_INVISIBLE);
  1636. _this.timeout = false;
  1637. }, 1000);
  1638. }
  1639. return this;
  1640. },
  1641. /**
  1642. * View the previous image
  1643. * @param {boolean} [loop=false] - Indicate if view the last one
  1644. * when it is the first one at present.
  1645. * @returns {Viewer} this
  1646. */
  1647. prev: function prev() {
  1648. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1649. var index = this.index - 1;
  1650. if (index < 0) {
  1651. index = loop ? this.length - 1 : 0;
  1652. swal("当前是第一个", {
  1653. buttons: false,
  1654. timer: 1500,
  1655. });
  1656. return this;
  1657. }
  1658. if (this.intervalTime!==undefined)
  1659. {
  1660. clearInterval(this.intervalTime);
  1661. this.intervalTime=null;
  1662. this.intervalTimeiTarget=null;
  1663. }
  1664. this.view(index,false,'prev');
  1665. return this;
  1666. },
  1667. /**
  1668. * View the next image
  1669. * @param {boolean} [loop=false] - Indicate if view the first one
  1670. * when it is the last one at present.
  1671. * @returns {Viewer} this
  1672. */
  1673. next: function next() {
  1674. var loop = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1675. var options = this.options;
  1676. var maxIndex =options.totalCount-1; //this.length - 1;
  1677. var index = this.index + 1;
  1678. if (index > maxIndex) {
  1679. swal("当前是最后一个", {
  1680. buttons: false,
  1681. timer: 1500,
  1682. });
  1683. index = loop ? 0 : maxIndex;
  1684. return this;
  1685. }else {
  1686. if (index>=this.length - 1)
  1687. {
  1688. var element = this.element;
  1689. if (isFunction(options.next)) {
  1690. addListener(element, EVENT_NEXT, options.next, {
  1691. once: true
  1692. });
  1693. }
  1694. if (dispatchEvent(element, EVENT_NEXT) === false) {
  1695. return;
  1696. }
  1697. this.update();
  1698. }
  1699. }
  1700. if (this.intervalTime!==undefined)
  1701. {
  1702. clearInterval(this.intervalTime);
  1703. this.intervalTime=null;
  1704. this.intervalTimeiTarget=null;
  1705. }
  1706. this.view(index,false,'next');
  1707. return this;
  1708. },
  1709. normal: function normal() {
  1710. var index = this.index;
  1711. var item = this.items[index];
  1712. var img = item.querySelector('img');
  1713. var dealUrl = getData(img, 'dealUrl');
  1714. if (this.dealOriginal.indexOf(dealUrl)>-1)
  1715. {
  1716. return this;
  1717. }
  1718. this.dealOriginal.push(dealUrl);
  1719. setData(img,'originalUrl',dealUrl);
  1720. this.view(index,true);
  1721. return this;
  1722. },
  1723. /**
  1724. * Move the image with relative offsets.
  1725. * @param {number} offsetX - The relative offset distance on the x-axis.
  1726. * @param {number} offsetY - The relative offset distance on the y-axis.
  1727. * @returns {Viewer} this
  1728. */
  1729. move: function move(offsetX, offsetY) {
  1730. var imageData = this.imageData;
  1731. this.moveTo(isUndefined(offsetX) ? offsetX : imageData.left + Number(offsetX), isUndefined(offsetY) ? offsetY : imageData.top + Number(offsetY));
  1732. return this;
  1733. },
  1734. moveSj: function move(offsetX, offsetY) {
  1735. var ul=this.canvas.querySelector("ul");
  1736. this.ulLeft=this.ulLeft+offsetX;
  1737. ul.style.marginLeft = this.ulLeft+ 'px';
  1738. if (this.isSwitchable2())
  1739. {
  1740. var than=this,
  1741. image = this.image,
  1742. viewerData = this.viewerData;
  1743. if (offsetX > 0) {
  1744. this.action=false;
  1745. var index = this.index - 1;
  1746. if (index < 0) {
  1747. this.springMove(this.ulLeftInitial);
  1748. }
  1749. this.pointers = {};
  1750. than.prev(false);
  1751. } else if (offsetX < 0) {
  1752. this.action=false;
  1753. var imageLi=image.parentNode;
  1754. var maxIndex =this.options.totalCount-1; //this.length - 1;
  1755. var index = this.index + 1;
  1756. if (index > maxIndex) {
  1757. var gjLeft2 = parseInt(this.ulLeftInitial);
  1758. var diff = parseInt(parseInt(image.style.width) - parseInt(viewerData.width));
  1759. if (diff > 0) {
  1760. gjLeft2 = gjLeft2 - diff;
  1761. }
  1762. this.springMove(gjLeft2);
  1763. }
  1764. this.pointers = {};
  1765. than.next(false);
  1766. }
  1767. }
  1768. },
  1769. testPromise:function testPromise() {
  1770. var than=this;
  1771. var promise1 = new Promise(function(resolve, reject) {
  1772. setTimeout(function() {
  1773. resolve('foo');
  1774. }, 1000);
  1775. });
  1776. var rush=false;
  1777. promise1.then(function(value) {
  1778. than.next(false);
  1779. rush=true;
  1780. });
  1781. return rush;
  1782. },
  1783. /**
  1784. * Move the image to an absolute point.
  1785. * @param {number} x - The x-axis coordinate.
  1786. * @param {number} [y=x] - The y-axis coordinate.
  1787. * @returns {Viewer} this
  1788. */
  1789. moveTo: function moveTo(x) {
  1790. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
  1791. var imageData = this.imageData;
  1792. x = Number(x);
  1793. y = Number(y);
  1794. if (this.viewed && !this.played && this.options.movable) {
  1795. var changed = false;
  1796. if (isNumber(x)) {
  1797. imageData.left = x;
  1798. changed = true;
  1799. }
  1800. if (isNumber(y)) {
  1801. imageData.top = y;
  1802. changed = true;
  1803. }
  1804. if (changed) {
  1805. this.renderImage();
  1806. }
  1807. }
  1808. return this;
  1809. },
  1810. /**
  1811. * Zoom the image with a relative ratio.
  1812. * @param {number} ratio - The target ratio.
  1813. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1814. * @param {Event} [_originalEvent=null] - The original event if any.
  1815. * @returns {Viewer} this
  1816. */
  1817. zoom: function zoom(ratio) {
  1818. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1819. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1820. var imageData = this.imageData;
  1821. ratio = Number(ratio);
  1822. if (ratio < 0) {
  1823. ratio = 1 / (1 - ratio);
  1824. } else {
  1825. ratio = 1 + ratio;
  1826. }
  1827. this.zoomTo(imageData.width * ratio / imageData.naturalWidth, hasTooltip, _originalEvent);
  1828. return this;
  1829. },
  1830. /**
  1831. * Zoom the image to an absolute ratio.
  1832. * @param {number} ratio - The target ratio.
  1833. * @param {boolean} [hasTooltip=false] - Indicates if it has a tooltip or not.
  1834. * @param {Event} [_originalEvent=null] - The original event if any.
  1835. * @param {Event} [_zoomable=false] - Indicates if the current zoom is available or not.
  1836. * @returns {Viewer} this
  1837. */
  1838. zoomTo: function zoomTo(ratio) {
  1839. var hasTooltip = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  1840. var _originalEvent = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
  1841. var _zoomable = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  1842. var options = this.options,
  1843. pointers = this.pointers,
  1844. imageData = this.imageData;
  1845. ratio = Math.max(0, ratio);
  1846. if (isNumber(ratio) && this.viewed && !this.played && (_zoomable || options.zoomable)) {
  1847. if (!_zoomable) {
  1848. var minZoomRatio = Math.max(0.01, options.minZoomRatio);
  1849. var maxZoomRatio = Math.min(100, options.maxZoomRatio);
  1850. ratio = Math.min(Math.max(ratio, minZoomRatio), maxZoomRatio);
  1851. }
  1852. if (_originalEvent && ratio > 0.95 && ratio < 1.05) {
  1853. ratio = 1;
  1854. }
  1855. var newWidth = imageData.naturalWidth * ratio;
  1856. var newHeight = imageData.naturalHeight * ratio;
  1857. if (_originalEvent) {
  1858. var offset = getOffset(this.viewer);
  1859. var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
  1860. pageX: _originalEvent.pageX,
  1861. pageY: _originalEvent.pageY
  1862. };
  1863. // Zoom from the triggering point of the event
  1864. imageData.left -= (newWidth - imageData.width) * ((center.pageX - offset.left - imageData.left) / imageData.width);
  1865. imageData.top -= (newHeight - imageData.height) * ((center.pageY - offset.top - imageData.top) / imageData.height);
  1866. } else {
  1867. // Zoom from the center of the image
  1868. imageData.left -= (newWidth - imageData.width) / 2;
  1869. imageData.top -= (newHeight - imageData.height) / 2;
  1870. }
  1871. imageData.width = newWidth;
  1872. imageData.height = newHeight;
  1873. imageData.ratio = ratio;
  1874. this.renderImage();
  1875. if (hasTooltip) {
  1876. this.tooltip();
  1877. }
  1878. }
  1879. //pxn
  1880. var userAgent = window.navigator.userAgent;
  1881. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  1882. {
  1883. var image = this.image,
  1884. viewerData = this.viewerData;
  1885. if (image.width>viewerData.width)
  1886. {
  1887. var gjLeft2=parseInt(this.ulLeftInitial);
  1888. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  1889. if (diff>0)
  1890. {
  1891. if (this.intervalTime!==undefined)
  1892. {
  1893. clearInterval(this.intervalTime);
  1894. this.intervalTime=null;
  1895. this.intervalTimeiTarget=null;
  1896. }
  1897. gjLeft2=gjLeft2-diff/2;
  1898. var ul=this.canvas.querySelector("ul");
  1899. this.ulLeft=gjLeft2;
  1900. ul.style.marginLeft = this.ulLeft+ 'px';
  1901. }
  1902. }
  1903. }
  1904. return this;
  1905. },
  1906. /**
  1907. * Rotate the image with a relative degree.
  1908. * @param {number} degree - The rotate degree.
  1909. * @returns {Viewer} this
  1910. */
  1911. rotate: function rotate(degree) {
  1912. this.rotateTo((this.imageData.rotate || 0) + Number(degree));
  1913. return this;
  1914. },
  1915. /**
  1916. * Rotate the image to an absolute degree.
  1917. * @param {number} degree - The rotate degree.
  1918. * @returns {Viewer} this
  1919. */
  1920. rotateTo: function rotateTo(degree) {
  1921. var imageData = this.imageData;
  1922. degree = Number(degree);
  1923. if (isNumber(degree) && this.viewed && !this.played && this.options.rotatable) {
  1924. imageData.rotate = degree;
  1925. this.renderImage();
  1926. }
  1927. return this;
  1928. },
  1929. /**
  1930. * Scale the image on the x-axis.
  1931. * @param {number} scaleX - The scale ratio on the x-axis.
  1932. * @returns {Viewer} this
  1933. */
  1934. scaleX: function scaleX(_scaleX) {
  1935. this.scale(_scaleX, this.imageData.scaleY);
  1936. return this;
  1937. },
  1938. /**
  1939. * Scale the image on the y-axis.
  1940. * @param {number} scaleY - The scale ratio on the y-axis.
  1941. * @returns {Viewer} this
  1942. */
  1943. scaleY: function scaleY(_scaleY) {
  1944. this.scale(this.imageData.scaleX, _scaleY);
  1945. return this;
  1946. },
  1947. /**
  1948. * Scale the image.
  1949. * @param {number} scaleX - The scale ratio on the x-axis.
  1950. * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
  1951. * @returns {Viewer} this
  1952. */
  1953. scale: function scale(scaleX) {
  1954. var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
  1955. var imageData = this.imageData;
  1956. scaleX = Number(scaleX);
  1957. scaleY = Number(scaleY);
  1958. if (this.viewed && !this.played && this.options.scalable) {
  1959. var changed = false;
  1960. if (isNumber(scaleX)) {
  1961. imageData.scaleX = scaleX;
  1962. changed = true;
  1963. }
  1964. if (isNumber(scaleY)) {
  1965. imageData.scaleY = scaleY;
  1966. changed = true;
  1967. }
  1968. if (changed) {
  1969. this.renderImage();
  1970. }
  1971. }
  1972. return this;
  1973. },
  1974. /**
  1975. * Play the images
  1976. * @param {boolean} [fullscreen=false] - Indicate if request fullscreen or not.
  1977. * @returns {Viewer} this
  1978. */
  1979. play: function play() {
  1980. var _this2 = this;
  1981. var fullscreen = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  1982. if (!this.isShown || this.played) {
  1983. return this;
  1984. }
  1985. var options = this.options,
  1986. player = this.player;
  1987. var onLoad = this.loadImage.bind(this);
  1988. var list = [];
  1989. var total = 0;
  1990. var index = 0;
  1991. this.played = true;
  1992. this.onLoadWhenPlay = onLoad;
  1993. if (fullscreen) {
  1994. this.requestFullscreen();
  1995. }
  1996. addClass(player, CLASS_SHOW);
  1997. forEach(this.items, function (item, i) {
  1998. var img = item.querySelector('img');
  1999. var image = document.createElement('img');
  2000. image.src = getData(img, 'originalUrl');
  2001. image.alt = img.getAttribute('alt');
  2002. total += 1;
  2003. addClass(image, CLASS_FADE);
  2004. toggleClass(image, CLASS_TRANSITION, options.transition);
  2005. if (hasClass(item, CLASS_ACTIVE)) {
  2006. addClass(image, CLASS_IN);
  2007. index = i;
  2008. }
  2009. list.push(image);
  2010. addListener(image, EVENT_LOAD, onLoad, {
  2011. once: true
  2012. });
  2013. player.appendChild(image);
  2014. });
  2015. if (isNumber(options.interval) && options.interval > 0) {
  2016. var play = function play() {
  2017. _this2.playing = setTimeout(function () {
  2018. removeClass(list[index], CLASS_IN);
  2019. index += 1;
  2020. index = index < total ? index : 0;
  2021. addClass(list[index], CLASS_IN);
  2022. play();
  2023. }, options.interval);
  2024. };
  2025. if (total > 1) {
  2026. play();
  2027. }
  2028. }
  2029. return this;
  2030. },
  2031. // Stop play
  2032. stop: function stop() {
  2033. var _this3 = this;
  2034. if (!this.played) {
  2035. return this;
  2036. }
  2037. var player = this.player;
  2038. this.played = false;
  2039. clearTimeout(this.playing);
  2040. forEach(player.getElementsByTagName('img'), function (image) {
  2041. removeListener(image, EVENT_LOAD, _this3.onLoadWhenPlay);
  2042. });
  2043. removeClass(player, CLASS_SHOW);
  2044. player.innerHTML = '';
  2045. this.exitFullscreen();
  2046. return this;
  2047. },
  2048. // Enter modal mode (only available in inline mode)
  2049. full: function full() {
  2050. var _this4 = this;
  2051. var options = this.options,
  2052. viewer = this.viewer,
  2053. image = this.image,
  2054. list = this.list;
  2055. if (!this.isShown || this.played || this.fulled || !options.inline) {
  2056. return this;
  2057. }
  2058. this.fulled = true;
  2059. this.open();
  2060. addClass(this.button, CLASS_FULLSCREEN_EXIT);
  2061. if (options.transition) {
  2062. removeClass(list, CLASS_TRANSITION);
  2063. if (this.viewed) {
  2064. removeClass(image, CLASS_TRANSITION);
  2065. }
  2066. }
  2067. addClass(viewer, CLASS_FIXED);
  2068. viewer.setAttribute('style', '');
  2069. setStyle(viewer, {
  2070. zIndex: options.zIndex
  2071. });
  2072. this.initContainer();
  2073. this.viewerData = assign({}, this.containerData);
  2074. this.renderList();
  2075. if (this.viewed) {
  2076. this.initImage(function () {
  2077. _this4.renderImage(function () {
  2078. if (options.transition) {
  2079. setTimeout(function () {
  2080. addClass(image, CLASS_TRANSITION);
  2081. addClass(list, CLASS_TRANSITION);
  2082. }, 0);
  2083. }
  2084. });
  2085. });
  2086. }
  2087. return this;
  2088. },
  2089. // Exit modal mode (only available in inline mode)
  2090. exit: function exit() {
  2091. var _this5 = this;
  2092. var options = this.options,
  2093. viewer = this.viewer,
  2094. image = this.image,
  2095. list = this.list;
  2096. if (!this.isShown || this.played || !this.fulled || !options.inline) {
  2097. return this;
  2098. }
  2099. this.fulled = false;
  2100. this.close();
  2101. removeClass(this.button, CLASS_FULLSCREEN_EXIT);
  2102. if (options.transition) {
  2103. removeClass(list, CLASS_TRANSITION);
  2104. if (this.viewed) {
  2105. removeClass(image, CLASS_TRANSITION);
  2106. }
  2107. }
  2108. removeClass(viewer, CLASS_FIXED);
  2109. setStyle(viewer, {
  2110. zIndex: options.zIndexInline
  2111. });
  2112. this.viewerData = assign({}, this.parentData);
  2113. this.renderViewer();
  2114. this.renderList();
  2115. if (this.viewed) {
  2116. this.initImage(function () {
  2117. _this5.renderImage(function () {
  2118. if (options.transition) {
  2119. setTimeout(function () {
  2120. addClass(image, CLASS_TRANSITION);
  2121. addClass(list, CLASS_TRANSITION);
  2122. }, 0);
  2123. }
  2124. });
  2125. });
  2126. }
  2127. return this;
  2128. },
  2129. // Show the current ratio of the image with percentage
  2130. tooltip: function tooltip() {
  2131. var _this6 = this;
  2132. var options = this.options,
  2133. tooltipBox = this.tooltipBox,
  2134. imageData = this.imageData;
  2135. if (!this.viewed || this.played || !options.tooltip) {
  2136. return this;
  2137. }
  2138. tooltipBox.textContent = Math.round(imageData.ratio * 100) + '%';
  2139. if (!this.tooltipping) {
  2140. if (options.transition) {
  2141. if (this.fading) {
  2142. dispatchEvent(tooltipBox, EVENT_TRANSITION_END);
  2143. }
  2144. addClass(tooltipBox, CLASS_SHOW);
  2145. addClass(tooltipBox, CLASS_FADE);
  2146. addClass(tooltipBox, CLASS_TRANSITION);
  2147. // Force reflow to enable CSS3 transition
  2148. // eslint-disable-next-line
  2149. tooltipBox.offsetWidth;
  2150. addClass(tooltipBox, CLASS_IN);
  2151. } else {
  2152. addClass(tooltipBox, CLASS_SHOW);
  2153. }
  2154. } else {
  2155. clearTimeout(this.tooltipping);
  2156. }
  2157. this.tooltipping = setTimeout(function () {
  2158. if (options.transition) {
  2159. addListener(tooltipBox, EVENT_TRANSITION_END, function () {
  2160. removeClass(tooltipBox, CLASS_SHOW);
  2161. removeClass(tooltipBox, CLASS_FADE);
  2162. removeClass(tooltipBox, CLASS_TRANSITION);
  2163. _this6.fading = false;
  2164. }, {
  2165. once: true
  2166. });
  2167. removeClass(tooltipBox, CLASS_IN);
  2168. _this6.fading = true;
  2169. } else {
  2170. removeClass(tooltipBox, CLASS_SHOW);
  2171. }
  2172. _this6.tooltipping = false;
  2173. }, 1000);
  2174. return this;
  2175. },
  2176. // Toggle the image size between its natural size and initial size
  2177. toggle: function toggle() {
  2178. if (this.imageData.ratio === 1) {
  2179. this.zoomTo(this.initialImageData.ratio, true);
  2180. } else {
  2181. this.zoomTo(1, true);
  2182. }
  2183. return this;
  2184. },
  2185. // Reset the image to its initial state
  2186. reset: function reset() {
  2187. if (this.viewed && !this.played) {
  2188. this.imageData = assign({}, this.initialImageData);
  2189. this.renderImage();
  2190. }
  2191. return this;
  2192. },
  2193. // Update viewer when images changed
  2194. update: function update() {
  2195. var element = this.element,
  2196. options = this.options,
  2197. isImg = this.isImg;
  2198. // Destroy viewer if the target image was deleted
  2199. if (isImg && !element.parentNode) {
  2200. return this.destroy();
  2201. }
  2202. var images = [];
  2203. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  2204. if (options.filter) {
  2205. if (options.filter(image)) {
  2206. images.push(image);
  2207. }
  2208. } else {
  2209. images.push(image);
  2210. }
  2211. });
  2212. if (!images.length) {
  2213. return this;
  2214. }
  2215. this.images = images;
  2216. this.length = images.length;
  2217. if (this.ready) {
  2218. var indexes = [];
  2219. forEach(this.items, function (item, i) {
  2220. var img = item.querySelector('img');
  2221. var image = images[i];
  2222. if (image) {
  2223. if (image.src !== img.src) {
  2224. indexes.push(i);
  2225. }
  2226. } else {
  2227. indexes.push(i);
  2228. }
  2229. });
  2230. setStyle(this.list, {
  2231. width: 'auto'
  2232. });
  2233. this.initList();
  2234. if (this.isShown) {
  2235. if (this.length) {
  2236. if (this.viewed) {
  2237. var index = indexes.indexOf(this.index);
  2238. if (index >= 0) {
  2239. this.viewed = false;
  2240. this.view(Math.max(this.index - (index + 1), 0));
  2241. } else {
  2242. addClass(this.items[this.index], CLASS_ACTIVE);
  2243. }
  2244. }
  2245. } else {
  2246. this.image = null;
  2247. this.viewed = false;
  2248. this.index = 0;
  2249. this.imageData = null;
  2250. this.canvas.innerHTML = '';
  2251. this.title.innerHTML = '';
  2252. }
  2253. }
  2254. } else {
  2255. this.build();
  2256. }
  2257. return this;
  2258. },
  2259. // Destroy the viewer
  2260. destroy: function destroy() {
  2261. var element = this.element,
  2262. options = this.options;
  2263. if (!getData(element, NAMESPACE)) {
  2264. return this;
  2265. }
  2266. this.destroyed = true;
  2267. if (this.ready) {
  2268. if (this.played) {
  2269. this.stop();
  2270. }
  2271. if (options.inline) {
  2272. if (this.fulled) {
  2273. this.exit();
  2274. }
  2275. this.unbind();
  2276. } else if (this.isShown) {
  2277. if (this.viewing) {
  2278. if (this.imageRendering) {
  2279. this.imageRendering.abort();
  2280. } else if (this.imageInitializing) {
  2281. this.imageInitializing.abort();
  2282. }
  2283. }
  2284. if (this.hiding) {
  2285. this.transitioning.abort();
  2286. }
  2287. this.hidden();
  2288. } else if (this.showing) {
  2289. this.transitioning.abort();
  2290. this.hidden();
  2291. }
  2292. this.ready = false;
  2293. this.viewer.parentNode.removeChild(this.viewer);
  2294. } else if (options.inline) {
  2295. if (this.delaying) {
  2296. this.delaying.abort();
  2297. } else if (this.initializing) {
  2298. this.initializing.abort();
  2299. }
  2300. }
  2301. if (!options.inline) {
  2302. removeListener(element, EVENT_CLICK, this.onStart);
  2303. }
  2304. removeData(element, NAMESPACE);
  2305. return this;
  2306. }
  2307. };
  2308. var others = {
  2309. open: function open() {
  2310. var body = this.body;
  2311. addClass(body, CLASS_OPEN);
  2312. body.style.paddingRight = this.scrollbarWidth + (parseFloat(this.initialBodyPaddingRight) || 0) + 'px';
  2313. },
  2314. close: function close() {
  2315. var body = this.body;
  2316. removeClass(body, CLASS_OPEN);
  2317. body.style.paddingRight = this.initialBodyPaddingRight;
  2318. },
  2319. shown: function shown() {
  2320. var element = this.element,
  2321. options = this.options;
  2322. this.fulled = true;
  2323. this.isShown = true;
  2324. this.render();
  2325. this.bind();
  2326. this.showing = false;
  2327. if (isFunction(options.shown)) {
  2328. addListener(element, EVENT_SHOWN, options.shown, {
  2329. once: true
  2330. });
  2331. }
  2332. if (dispatchEvent(element, EVENT_SHOWN) === false) {
  2333. return;
  2334. }
  2335. if (this.ready && this.isShown && !this.hiding) {
  2336. this.view(this.index);
  2337. }
  2338. },
  2339. hidden: function hidden() {
  2340. var element = this.element,
  2341. options = this.options;
  2342. this.fulled = false;
  2343. this.viewed = false;
  2344. this.isShown = false;
  2345. this.close();
  2346. this.unbind();
  2347. addClass(this.viewer, CLASS_HIDE);
  2348. this.resetList();
  2349. this.resetImage();
  2350. this.hiding = false;
  2351. if (!this.destroyed) {
  2352. if (isFunction(options.hidden)) {
  2353. addListener(element, EVENT_HIDDEN, options.hidden, {
  2354. once: true
  2355. });
  2356. }
  2357. dispatchEvent(element, EVENT_HIDDEN);
  2358. }
  2359. },
  2360. requestFullscreen: function requestFullscreen() {
  2361. var document = this.element.ownerDocument;
  2362. if (this.fulled && !document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement && !document.msFullscreenElement) {
  2363. var documentElement = document.documentElement;
  2364. if (documentElement.requestFullscreen) {
  2365. documentElement.requestFullscreen();
  2366. } else if (documentElement.msRequestFullscreen) {
  2367. documentElement.msRequestFullscreen();
  2368. } else if (documentElement.mozRequestFullScreen) {
  2369. documentElement.mozRequestFullScreen();
  2370. } else if (documentElement.webkitRequestFullscreen) {
  2371. documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  2372. }
  2373. }
  2374. },
  2375. exitFullscreen: function exitFullscreen() {
  2376. if (this.fulled) {
  2377. var document = this.element.ownerDocument;
  2378. if (document.exitFullscreen) {
  2379. document.exitFullscreen();
  2380. } else if (document.msExitFullscreen) {
  2381. document.msExitFullscreen();
  2382. } else if (document.mozCancelFullScreen) {
  2383. document.mozCancelFullScreen();
  2384. } else if (document.webkitExitFullscreen) {
  2385. document.webkitExitFullscreen();
  2386. }
  2387. }
  2388. },
  2389. change: function change(e) {
  2390. var options = this.options,
  2391. pointers = this.pointers;
  2392. var pointer = pointers[Object.keys(pointers)[0]];
  2393. var offsetX = pointer.endX - pointer.startX;
  2394. var offsetY = pointer.endY - pointer.startY;
  2395. switch (this.action) {
  2396. // Move the current image
  2397. case ACTION_MOVE:
  2398. var userAgent = window.navigator.userAgent;
  2399. if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i) || userAgent.match(/android/i))
  2400. {
  2401. this.moveSj(offsetX,offsetY);
  2402. }else {
  2403. this.move(offsetX, offsetY);
  2404. }
  2405. break;
  2406. // Zoom the current image
  2407. case ACTION_ZOOM:
  2408. this.zoom(getMaxZoomRatio(pointers), false, e);
  2409. break;
  2410. case ACTION_SWITCH:
  2411. this.action = 'switched';
  2412. // Empty `pointers` as `touchend` event will not be fired after swiped in iOS browsers.
  2413. this.pointers = {};
  2414. if (Math.abs(offsetX) > Math.abs(offsetY)) {
  2415. if (offsetX > 1) {
  2416. this.prev(options.loop);
  2417. } else if (offsetX < -1) {
  2418. this.next(options.loop);
  2419. }
  2420. }
  2421. break;
  2422. default:
  2423. }
  2424. // Override
  2425. forEach(pointers, function (p) {
  2426. p.startX = p.endX;
  2427. p.startY = p.endY;
  2428. });
  2429. },
  2430. isSwitchable: function isSwitchable() {
  2431. var imageData = this.imageData,
  2432. viewerData = this.viewerData;
  2433. return this.length > 1&&imageData.width <= viewerData.width && imageData.height <= viewerData.height;
  2434. // return this.length > 1&& imageData.left >= 0&& imageData.top >= 0 && imageData.width <= viewerData.width && imageData.height <= viewerData.height;
  2435. },
  2436. isSwitchable2: function isSwitchable2() {
  2437. var imageData = this.imageData,
  2438. image = this.image,
  2439. viewerData = this.viewerData,
  2440. canvas = this.canvas;
  2441. if (this.length <= 1)
  2442. {
  2443. return false;
  2444. }
  2445. var moveLeft=parseInt(this.ulLeft);
  2446. if (moveLeft<this.ulLeftInitial)
  2447. {
  2448. var gjLeft=parseInt(this.ulLeftInitial)-parseInt(1/3*viewerData.width);
  2449. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2450. if (diff>0)
  2451. {
  2452. gjLeft=gjLeft-diff;
  2453. }
  2454. if (moveLeft<gjLeft)
  2455. {
  2456. return true;
  2457. }
  2458. }else if (moveLeft>this.ulLeftInitial) {
  2459. if (moveLeft>parseInt(this.ulLeftInitial)+parseInt(1/3*viewerData.width))
  2460. {
  2461. return true;
  2462. }
  2463. }
  2464. return false;
  2465. },
  2466. isSwitchable3: function isSwitchable3() {
  2467. var image = this.image,
  2468. viewerData = this.viewerData;
  2469. if (this.length <= 1)
  2470. {
  2471. return ;
  2472. }
  2473. var moveLeft=parseInt(this.ulLeft);
  2474. if (moveLeft<this.ulLeftInitial)
  2475. {
  2476. var gjLeft2=parseInt(this.ulLeftInitial);
  2477. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2478. if (diff>0)
  2479. {
  2480. gjLeft2=gjLeft2-diff;
  2481. }
  2482. if (moveLeft>gjLeft2)
  2483. {
  2484. return ;
  2485. }else {
  2486. this.ulLeft=gjLeft2;
  2487. this.springMove(gjLeft2);
  2488. }
  2489. }else if (moveLeft>this.ulLeftInitial) {
  2490. this.springMove(this.ulLeftInitial);
  2491. }else {
  2492. this.springMove(this.ulLeftInitial);
  2493. }
  2494. return;
  2495. },
  2496. isSwitchable4: function isSwitchable4() {
  2497. var image = this.image,
  2498. viewerData = this.viewerData;
  2499. if (this.length <= 1)
  2500. {
  2501. return ;
  2502. }
  2503. var ul=this.canvas.querySelector("ul");
  2504. if (ul===null||ul===undefined)
  2505. {
  2506. return;
  2507. }
  2508. var moveLeft=parseInt(this.ulLeft);
  2509. if (moveLeft<this.ulLeftInitial)
  2510. {
  2511. var gjLeft2=parseInt(this.ulLeftInitial);
  2512. var diff=parseInt(parseInt(image.style.width)-parseInt(viewerData.width));
  2513. if (diff>0)
  2514. {
  2515. gjLeft2=gjLeft2-diff;
  2516. }
  2517. if (moveLeft>gjLeft2)
  2518. {
  2519. return ;
  2520. }else {
  2521. this.ulLeft=gjLeft2;
  2522. ul.style.marginLeft= this.ulLeft + 'px';
  2523. }
  2524. }else if (moveLeft>this.ulLeftInitial) {
  2525. this.ulLeft=this.ulLeftInitial;
  2526. ul.style.marginLeft= this.ulLeft + 'px';
  2527. }else {
  2528. this.ulLeft=this.ulLeftInitial;
  2529. ul.style.marginLeft= this.ulLeft + 'px';
  2530. }
  2531. return;
  2532. },
  2533. springMove:function springMove(iTarget) {
  2534. var than=this;
  2535. clearInterval(this.intervalTime);//清空定时器
  2536. than.intervalTime=null;
  2537. than.intervalTimeiTarget=null;
  2538. var iSpeed = 0;
  2539. this.intervalTime = setInterval(function(){ //开启定时器
  2540. var ul=than.canvas.querySelector("ul");
  2541. if (ul===null||ul===undefined)
  2542. {
  2543. clearInterval(than.intervalTime);
  2544. than.intervalTime=null;
  2545. return;
  2546. }
  2547. iSpeed += (iTarget - parseInt(ul.style.marginLeft))/6;
  2548. iSpeed *= 0.75;
  2549. if(
  2550. Math.abs(iSpeed)<=1&&
  2551. Math.abs(iTarget - parseInt(ul.style.marginLeft))<=1
  2552. ){
  2553. clearInterval(than.intervalTime);
  2554. than.intervalTime=null;
  2555. than.intervalTimeiTarget=null;
  2556. iSpeed = 0;
  2557. ul.style.marginLeft= iTarget + 'px';
  2558. }
  2559. else{
  2560. ul.style.marginLeft = (parseInt(ul.style.marginLeft)+ parseInt(iSpeed)).toString() + 'px';
  2561. }
  2562. },30);
  2563. than.intervalTimeiTarget=iTarget;
  2564. },
  2565. };
  2566. var AnotherViewer = WINDOW.Viewer;
  2567. var Viewer = function () {
  2568. /**
  2569. * Create a new Viewer.
  2570. * @param {Element} element - The target element for viewing.
  2571. * @param {Object} [options={}] - The configuration options.
  2572. */
  2573. function Viewer(element) {
  2574. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2575. classCallCheck(this, Viewer);
  2576. if (!element || element.nodeType !== 1) {
  2577. throw new Error('The first argument is required and must be an element.');
  2578. }
  2579. this.element = element;
  2580. this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
  2581. this.action = false;
  2582. this.fading = false;
  2583. this.fulled = false;
  2584. this.hiding = false;
  2585. this.index = 0;
  2586. this.isImg = false;
  2587. this.length = 0;
  2588. this.played = false;
  2589. this.playing = false;
  2590. this.pointers = {};
  2591. this.ready = false;
  2592. this.showing = false;
  2593. this.timeout = false;
  2594. this.tooltipping = false;
  2595. this.viewed = false;
  2596. this.viewing = false;
  2597. this.isShown = false;
  2598. this.wheeling = false;
  2599. this.init();
  2600. }
  2601. createClass(Viewer, [{
  2602. key: 'init',
  2603. value: function init() {
  2604. var _this = this;
  2605. var element = this.element,
  2606. options = this.options;
  2607. if (getData(element, NAMESPACE)) {
  2608. return;
  2609. }
  2610. setData(element, NAMESPACE, this);
  2611. var isImg = element.tagName.toLowerCase() === 'img';
  2612. var images = [];
  2613. forEach(isImg ? [element] : element.querySelectorAll('img'), function (image) {
  2614. if (isFunction(options.filter)) {
  2615. if (options.filter.call(_this, image)) {
  2616. images.push(image);
  2617. }
  2618. } else {
  2619. images.push(image);
  2620. }
  2621. });
  2622. if (!images.length) {
  2623. return;
  2624. }
  2625. this.isImg = isImg;
  2626. this.length = images.length;
  2627. this.images = images;
  2628. this.dealOriginal=[];
  2629. var ownerDocument = element.ownerDocument;
  2630. var body = ownerDocument.body || ownerDocument.documentElement;
  2631. this.body = body;
  2632. this.scrollbarWidth = window.innerWidth - ownerDocument.documentElement.clientWidth;
  2633. this.initialBodyPaddingRight = window.getComputedStyle(body).paddingRight;
  2634. // Override `transition` option if it is not supported
  2635. if (isUndefined(document.createElement(NAMESPACE).style.transition)) {
  2636. options.transition = false;
  2637. }
  2638. if (options.inline) {
  2639. var count = 0;
  2640. var progress = function progress() {
  2641. count += 1;
  2642. if (count === _this.length) {
  2643. var timeout = void 0;
  2644. _this.initializing = false;
  2645. _this.delaying = {
  2646. abort: function abort() {
  2647. clearTimeout(timeout);
  2648. }
  2649. };
  2650. // build asynchronously to keep `this.viewer` is accessible in `ready` event handler.
  2651. timeout = setTimeout(function () {
  2652. _this.delaying = false;
  2653. _this.build();
  2654. }, 0);
  2655. }
  2656. };
  2657. this.initializing = {
  2658. abort: function abort() {
  2659. forEach(images, function (image) {
  2660. if (!image.complete) {
  2661. removeListener(image, EVENT_LOAD, progress);
  2662. }
  2663. });
  2664. }
  2665. };
  2666. forEach(images, function (image) {
  2667. if (image.complete) {
  2668. progress();
  2669. } else {
  2670. addListener(image, EVENT_LOAD, progress, {
  2671. once: true
  2672. });
  2673. }
  2674. });
  2675. } else {
  2676. addListener(element, EVENT_CLICK, this.onStart = function (_ref) {
  2677. var target = _ref.target;
  2678. var dataUpdate=document.getElementById("docsPictures").getAttribute('data-update');
  2679. if (dataUpdate=='1')
  2680. {
  2681. _this.update();
  2682. document.getElementById("docsPictures").setAttribute('data-update','0');
  2683. }
  2684. if (target.tagName.toLowerCase() === 'img') {
  2685. _this.view(_this.images.indexOf(target));
  2686. }
  2687. });
  2688. }
  2689. }
  2690. }, {
  2691. key: 'build',
  2692. value: function build() {
  2693. if (this.ready) {
  2694. return;
  2695. }
  2696. var element = this.element,
  2697. options = this.options;
  2698. var parent = element.parentNode;
  2699. var template = document.createElement('div');
  2700. template.innerHTML = TEMPLATE;
  2701. var viewer = template.querySelector('.' + NAMESPACE + '-container');
  2702. var title = viewer.querySelector('.' + NAMESPACE + '-title');
  2703. var toolbar = viewer.querySelector('.' + NAMESPACE + '-toolbar');
  2704. var navbar = viewer.querySelector('.' + NAMESPACE + '-navbar');
  2705. var button = viewer.querySelector('.' + NAMESPACE + '-button');
  2706. var canvas = viewer.querySelector('.' + NAMESPACE + '-canvas');
  2707. if ($(window).width()>700)
  2708. {
  2709. toolbar.style.display = "flex";
  2710. toolbar.style.justifyContent= "center";
  2711. }
  2712. this.parent = parent;
  2713. this.viewer = viewer;
  2714. this.title = title;
  2715. this.toolbar = toolbar;
  2716. this.navbar = navbar;
  2717. this.button = button;
  2718. this.canvas = canvas;
  2719. this.footer = viewer.querySelector('.' + NAMESPACE + '-footer');
  2720. this.tooltipBox = viewer.querySelector('.' + NAMESPACE + '-tooltip');
  2721. this.player = viewer.querySelector('.' + NAMESPACE + '-player');
  2722. this.list = viewer.querySelector('.' + NAMESPACE + '-list');
  2723. addClass(title, !options.title ? CLASS_HIDE : getResponsiveClass(options.title));
  2724. addClass(navbar, !options.navbar ? CLASS_HIDE : getResponsiveClass(options.navbar));
  2725. toggleClass(button, CLASS_HIDE, !options.button);
  2726. if (options.backdrop) {
  2727. addClass(viewer, NAMESPACE + '-backdrop');
  2728. if (!options.inline && options.backdrop === true) {
  2729. setData(canvas, 'action', 'hide');
  2730. }
  2731. }
  2732. if (options.toolbar) {
  2733. var list = document.createElement('ul');
  2734. var custom = isPlainObject(options.toolbar);
  2735. var zoomButtons = BUTTONS.slice(0, 3);
  2736. var rotateButtons = BUTTONS.slice(7, 9);
  2737. var scaleButtons = BUTTONS.slice(9);
  2738. if (!custom) {
  2739. addClass(toolbar, getResponsiveClass(options.toolbar));
  2740. }
  2741. forEach(custom ? options.toolbar : BUTTONS, function (value, index) {
  2742. var deep = custom && isPlainObject(value);
  2743. var name = custom ? hyphenate(index) : value;
  2744. var show = deep && !isUndefined(value.show) ? value.show : value;
  2745. if (!show || !options.zoomable && zoomButtons.indexOf(name) !== -1 || !options.rotatable && rotateButtons.indexOf(name) !== -1 || !options.scalable && scaleButtons.indexOf(name) !== -1) {
  2746. return;
  2747. }
  2748. var size = deep && !isUndefined(value.size) ? value.size : value;
  2749. var click = deep && !isUndefined(value.click) ? value.click : value;
  2750. var item = document.createElement('li');
  2751. item.setAttribute('role', 'button');
  2752. addClass(item, NAMESPACE + '-' + name);
  2753. if (!isFunction(click)) {
  2754. setData(item, 'action', name);
  2755. }
  2756. if (isNumber(show)) {
  2757. addClass(item, getResponsiveClass(show));
  2758. }
  2759. if (['small', 'large'].indexOf(size) !== -1) {
  2760. addClass(item, NAMESPACE + '-' + size);
  2761. } else if (name === 'play') {
  2762. addClass(item, NAMESPACE + '-large');
  2763. }
  2764. if (isFunction(click)) {
  2765. addListener(item, EVENT_CLICK, click);
  2766. }
  2767. list.appendChild(item);
  2768. });
  2769. toolbar.appendChild(list);
  2770. } else {
  2771. addClass(toolbar, CLASS_HIDE);
  2772. }
  2773. if (!options.rotatable) {
  2774. var rotates = toolbar.querySelectorAll('li[class*="rotate"]');
  2775. addClass(rotates, CLASS_INVISIBLE);
  2776. forEach(rotates, function (rotate) {
  2777. toolbar.appendChild(rotate);
  2778. });
  2779. }
  2780. if (options.inline) {
  2781. addClass(button, CLASS_FULLSCREEN);
  2782. setStyle(viewer, {
  2783. zIndex: options.zIndexInline
  2784. });
  2785. if (window.getComputedStyle(parent).position === 'static') {
  2786. setStyle(parent, {
  2787. position: 'relative'
  2788. });
  2789. }
  2790. parent.insertBefore(viewer, element.nextSibling);
  2791. } else {
  2792. addClass(button, CLASS_CLOSE);
  2793. addClass(viewer, CLASS_FIXED);
  2794. addClass(viewer, CLASS_FADE);
  2795. addClass(viewer, CLASS_HIDE);
  2796. setStyle(viewer, {
  2797. zIndex: options.zIndex
  2798. });
  2799. var container = options.container;
  2800. if (isString(container)) {
  2801. container = element.ownerDocument.querySelector(container);
  2802. }
  2803. if (!container) {
  2804. container = this.body;
  2805. }
  2806. container.appendChild(viewer);
  2807. }
  2808. if (options.inline) {
  2809. this.render();
  2810. this.bind();
  2811. this.isShown = true;
  2812. }
  2813. this.ready = true;
  2814. if (isFunction(options.ready)) {
  2815. addListener(element, EVENT_READY, options.ready, {
  2816. once: true
  2817. });
  2818. }
  2819. if (dispatchEvent(element, EVENT_READY) === false) {
  2820. this.ready = false;
  2821. return;
  2822. }
  2823. if (this.ready && options.inline) {
  2824. this.view();
  2825. }
  2826. }
  2827. /**
  2828. * Get the no conflict viewer class.
  2829. * @returns {Viewer} The viewer class.
  2830. */
  2831. }], [{
  2832. key: 'noConflict',
  2833. value: function noConflict() {
  2834. window.Viewer = AnotherViewer;
  2835. return Viewer;
  2836. }
  2837. /**
  2838. * Change the default options.
  2839. * @param {Object} options - The new default options.
  2840. */
  2841. }, {
  2842. key: 'setDefaults',
  2843. value: function setDefaults(options) {
  2844. assign(DEFAULTS, isPlainObject(options) && options);
  2845. }
  2846. }]);
  2847. return Viewer;
  2848. }();
  2849. assign(Viewer.prototype, render, events, handlers, methods, others);
  2850. if ($.fn) {
  2851. var AnotherViewer$1 = $.fn.viewer;
  2852. var NAMESPACE$1 = 'viewer';
  2853. $.fn.viewer = function jQueryViewer(option) {
  2854. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  2855. args[_key - 1] = arguments[_key];
  2856. }
  2857. var result = void 0;
  2858. this.each(function (i, element) {
  2859. var $element = $(element);
  2860. var isDestroy = option === 'destroy';
  2861. var viewer = $element.data(NAMESPACE$1);
  2862. if (!viewer) {
  2863. if (isDestroy) {
  2864. return;
  2865. }
  2866. var options = $.extend({}, $element.data(), $.isPlainObject(option) && option);
  2867. viewer = new Viewer(element, options);
  2868. $element.data(NAMESPACE$1, viewer);
  2869. }
  2870. if (typeof option === 'string') {
  2871. var fn = viewer[option];
  2872. if ($.isFunction(fn)) {
  2873. result = fn.apply(viewer, args);
  2874. if (result === viewer) {
  2875. result = undefined;
  2876. }
  2877. if (isDestroy) {
  2878. $element.removeData(NAMESPACE$1);
  2879. }
  2880. }
  2881. }
  2882. });
  2883. return result !== undefined ? result : this;
  2884. };
  2885. $.fn.viewer.Constructor = Viewer;
  2886. $.fn.viewer.setDefaults = Viewer.setDefaults;
  2887. $.fn.viewer.noConflict = function noConflict() {
  2888. $.fn.viewer = AnotherViewer$1;
  2889. return this;
  2890. };
  2891. }
  2892. })));