lg-rotate.es5.js 10 KB

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