lg-rotate.umd.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*!
  2. * lightgallery | 2.4.0-beta.0 | December 12th 2021
  3. * http://www.lightgalleryjs.com/
  4. * Copyright (c) 2020 Sachin Neravath;
  5. * @license GPLv3
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  9. typeof define === 'function' && define.amd ? define(factory) :
  10. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.lgRotate = factory());
  11. }(this, (function () { 'use strict';
  12. /*! *****************************************************************************
  13. Copyright (c) Microsoft Corporation.
  14. Permission to use, copy, modify, and/or distribute this software for any
  15. purpose with or without fee is hereby granted.
  16. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  17. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  19. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  20. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. PERFORMANCE OF THIS SOFTWARE.
  23. ***************************************************************************** */
  24. var __assign = function() {
  25. __assign = Object.assign || function __assign(t) {
  26. for (var s, i = 1, n = arguments.length; i < n; i++) {
  27. s = arguments[i];
  28. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  29. }
  30. return t;
  31. };
  32. return __assign.apply(this, arguments);
  33. };
  34. /**
  35. * List of lightGallery events
  36. * All events should be documented here
  37. * Below interfaces are used to build the website documentations
  38. * */
  39. var lGEvents = {
  40. afterAppendSlide: 'lgAfterAppendSlide',
  41. init: 'lgInit',
  42. hasVideo: 'lgHasVideo',
  43. containerResize: 'lgContainerResize',
  44. updateSlides: 'lgUpdateSlides',
  45. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  46. beforeOpen: 'lgBeforeOpen',
  47. afterOpen: 'lgAfterOpen',
  48. slideItemLoad: 'lgSlideItemLoad',
  49. beforeSlide: 'lgBeforeSlide',
  50. afterSlide: 'lgAfterSlide',
  51. posterClick: 'lgPosterClick',
  52. dragStart: 'lgDragStart',
  53. dragMove: 'lgDragMove',
  54. dragEnd: 'lgDragEnd',
  55. beforeNextSlide: 'lgBeforeNextSlide',
  56. beforePrevSlide: 'lgBeforePrevSlide',
  57. beforeClose: 'lgBeforeClose',
  58. afterClose: 'lgAfterClose',
  59. rotateLeft: 'lgRotateLeft',
  60. rotateRight: 'lgRotateRight',
  61. flipHorizontal: 'lgFlipHorizontal',
  62. flipVertical: 'lgFlipVertical',
  63. autoplay: 'lgAutoplay',
  64. autoplayStart: 'lgAutoplayStart',
  65. autoplayStop: 'lgAutoplayStop',
  66. };
  67. var rotateSettings = {
  68. rotate: true,
  69. rotateSpeed: 400,
  70. rotateLeft: true,
  71. rotateRight: true,
  72. flipHorizontal: true,
  73. flipVertical: true,
  74. rotatePluginStrings: {
  75. flipVertical: 'Flip vertical',
  76. flipHorizontal: 'Flip horizontal',
  77. rotateLeft: 'Rotate left',
  78. rotateRight: 'Rotate right',
  79. },
  80. };
  81. var Rotate = /** @class */ (function () {
  82. function Rotate(instance, $LG) {
  83. // get lightGallery core plugin instance
  84. this.core = instance;
  85. this.$LG = $LG;
  86. // extend module default settings with lightGallery core settings
  87. this.settings = __assign(__assign({}, rotateSettings), this.core.settings);
  88. return this;
  89. }
  90. Rotate.prototype.buildTemplates = function () {
  91. var rotateIcons = '';
  92. if (this.settings.flipVertical) {
  93. rotateIcons += "<button type=\"button\" id=\"lg-flip-ver\" aria-label=\"" + this.settings.rotatePluginStrings['flipVertical'] + "\" class=\"lg-flip-ver lg-icon\"></button>";
  94. }
  95. if (this.settings.flipHorizontal) {
  96. rotateIcons += "<button type=\"button\" id=\"lg-flip-hor\" aria-label=\"" + this.settings.rotatePluginStrings['flipHorizontal'] + "\" class=\"lg-flip-hor lg-icon\"></button>";
  97. }
  98. if (this.settings.rotateLeft) {
  99. rotateIcons += "<button type=\"button\" id=\"lg-rotate-left\" aria-label=\"" + this.settings.rotatePluginStrings['rotateLeft'] + "\" class=\"lg-rotate-left lg-icon\"></button>";
  100. }
  101. if (this.settings.rotateRight) {
  102. rotateIcons += "<button type=\"button\" id=\"lg-rotate-right\" aria-label=\"" + this.settings.rotatePluginStrings['rotateRight'] + "\" class=\"lg-rotate-right lg-icon\"></button>";
  103. }
  104. this.core.$toolbar.append(rotateIcons);
  105. };
  106. Rotate.prototype.init = function () {
  107. var _this = this;
  108. if (!this.settings.rotate) {
  109. return;
  110. }
  111. this.buildTemplates();
  112. // Save rotate config for each item to persist its rotate, flip values
  113. // even after navigating to diferent slides
  114. this.rotateValuesList = {};
  115. // event triggered after appending slide content
  116. this.core.LGel.on(lGEvents.afterAppendSlide + ".rotate", function (event) {
  117. var index = event.detail.index;
  118. var imageWrap = _this.core
  119. .getSlideItem(index)
  120. .find('.lg-img-wrap')
  121. .first();
  122. imageWrap.wrap('lg-img-rotate');
  123. _this.core
  124. .getSlideItem(_this.core.index)
  125. .find('.lg-img-rotate')
  126. .css('transition-duration', _this.settings.rotateSpeed + 'ms');
  127. });
  128. this.core.outer
  129. .find('#lg-rotate-left')
  130. .first()
  131. .on('click.lg', this.rotateLeft.bind(this));
  132. this.core.outer
  133. .find('#lg-rotate-right')
  134. .first()
  135. .on('click.lg', this.rotateRight.bind(this));
  136. this.core.outer
  137. .find('#lg-flip-hor')
  138. .first()
  139. .on('click.lg', this.flipHorizontal.bind(this));
  140. this.core.outer
  141. .find('#lg-flip-ver')
  142. .first()
  143. .on('click.lg', this.flipVertical.bind(this));
  144. // Reset rotate on slide change
  145. this.core.LGel.on(lGEvents.beforeSlide + ".rotate", function (event) {
  146. if (!_this.rotateValuesList[event.detail.index]) {
  147. _this.rotateValuesList[event.detail.index] = {
  148. rotate: 0,
  149. flipHorizontal: 1,
  150. flipVertical: 1,
  151. };
  152. }
  153. });
  154. };
  155. Rotate.prototype.applyStyles = function () {
  156. var $image = this.core
  157. .getSlideItem(this.core.index)
  158. .find('.lg-img-rotate')
  159. .first();
  160. $image.css('transform', 'rotate(' +
  161. this.rotateValuesList[this.core.index].rotate +
  162. 'deg)' +
  163. ' scale3d(' +
  164. this.rotateValuesList[this.core.index].flipHorizontal +
  165. ', ' +
  166. this.rotateValuesList[this.core.index].flipVertical +
  167. ', 1)');
  168. };
  169. Rotate.prototype.rotateLeft = function () {
  170. this.rotateValuesList[this.core.index].rotate -= 90;
  171. this.applyStyles();
  172. this.triggerEvents(lGEvents.rotateLeft, {
  173. rotate: this.rotateValuesList[this.core.index].rotate,
  174. });
  175. };
  176. Rotate.prototype.rotateRight = function () {
  177. this.rotateValuesList[this.core.index].rotate += 90;
  178. this.applyStyles();
  179. this.triggerEvents(lGEvents.rotateRight, {
  180. rotate: this.rotateValuesList[this.core.index].rotate,
  181. });
  182. };
  183. Rotate.prototype.getCurrentRotation = function (el) {
  184. if (!el) {
  185. return 0;
  186. }
  187. var st = this.$LG(el).style();
  188. var tm = st.getPropertyValue('-webkit-transform') ||
  189. st.getPropertyValue('-moz-transform') ||
  190. st.getPropertyValue('-ms-transform') ||
  191. st.getPropertyValue('-o-transform') ||
  192. st.getPropertyValue('transform') ||
  193. 'none';
  194. if (tm !== 'none') {
  195. var values = tm.split('(')[1].split(')')[0].split(',');
  196. if (values) {
  197. var angle = Math.round(Math.atan2(values[1], values[0]) * (180 / Math.PI));
  198. return angle < 0 ? angle + 360 : angle;
  199. }
  200. }
  201. return 0;
  202. };
  203. Rotate.prototype.flipHorizontal = function () {
  204. var rotateEl = this.core
  205. .getSlideItem(this.core.index)
  206. .find('.lg-img-rotate')
  207. .first()
  208. .get();
  209. var currentRotation = this.getCurrentRotation(rotateEl);
  210. var rotateAxis = 'flipHorizontal';
  211. if (currentRotation === 90 || currentRotation === 270) {
  212. rotateAxis = 'flipVertical';
  213. }
  214. this.rotateValuesList[this.core.index][rotateAxis] *= -1;
  215. this.applyStyles();
  216. this.triggerEvents(lGEvents.flipHorizontal, {
  217. flipHorizontal: this.rotateValuesList[this.core.index][rotateAxis],
  218. });
  219. };
  220. Rotate.prototype.flipVertical = function () {
  221. var rotateEl = this.core
  222. .getSlideItem(this.core.index)
  223. .find('.lg-img-rotate')
  224. .first()
  225. .get();
  226. var currentRotation = this.getCurrentRotation(rotateEl);
  227. var rotateAxis = 'flipVertical';
  228. if (currentRotation === 90 || currentRotation === 270) {
  229. rotateAxis = 'flipHorizontal';
  230. }
  231. this.rotateValuesList[this.core.index][rotateAxis] *= -1;
  232. this.applyStyles();
  233. this.triggerEvents(lGEvents.flipVertical, {
  234. flipVertical: this.rotateValuesList[this.core.index][rotateAxis],
  235. });
  236. };
  237. Rotate.prototype.triggerEvents = function (event, detail) {
  238. var _this = this;
  239. setTimeout(function () {
  240. _this.core.LGel.trigger(event, detail);
  241. }, this.settings.rotateSpeed + 10);
  242. };
  243. Rotate.prototype.isImageOrientationChanged = function () {
  244. var rotateValue = this.rotateValuesList[this.core.index];
  245. var isRotated = Math.abs(rotateValue.rotate) % 360 !== 0;
  246. var ifFlippedHor = rotateValue.flipHorizontal < 0;
  247. var ifFlippedVer = rotateValue.flipVertical < 0;
  248. return isRotated || ifFlippedHor || ifFlippedVer;
  249. };
  250. Rotate.prototype.closeGallery = function () {
  251. if (this.isImageOrientationChanged()) {
  252. this.core.getSlideItem(this.core.index).css('opacity', 0);
  253. }
  254. this.rotateValuesList = {};
  255. };
  256. Rotate.prototype.destroy = function () {
  257. // Unbind all events added by lightGallery rotate plugin
  258. this.core.LGel.off('.lg.rotate');
  259. this.core.LGel.off('.rotate');
  260. };
  261. return Rotate;
  262. }());
  263. return Rotate;
  264. })));
  265. //# sourceMappingURL=lg-rotate.umd.js.map