lg-hash.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**!
  2. * lg-hash.js | 0.0.1 | August 1st 2016
  3. * http://sachinchoolur.github.io/lg-hash.js
  4. * Copyright (c) 2016 Sachin N;
  5. * @license Apache 2.0
  6. */(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.LgHash = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  7. (function (global, factory) {
  8. if (typeof define === "function" && define.amd) {
  9. define([], factory);
  10. } else if (typeof exports !== "undefined") {
  11. factory();
  12. } else {
  13. var mod = {
  14. exports: {}
  15. };
  16. factory();
  17. global.lgHash = mod.exports;
  18. }
  19. })(this, function () {
  20. 'use strict';
  21. var _extends = Object.assign || function (target) {
  22. for (var i = 1; i < arguments.length; i++) {
  23. var source = arguments[i];
  24. for (var key in source) {
  25. if (Object.prototype.hasOwnProperty.call(source, key)) {
  26. target[key] = source[key];
  27. }
  28. }
  29. }
  30. return target;
  31. };
  32. var hashDefaults = {
  33. hash: true
  34. };
  35. var Hash = function Hash(element) {
  36. this.el = element;
  37. this.core = window.lgData[this.el.getAttribute('lg-uid')];
  38. this.core.s = _extends({}, hashDefaults, this.core.s);
  39. if (this.core.s.hash) {
  40. this.oldHash = window.location.hash;
  41. this.init();
  42. }
  43. return this;
  44. };
  45. Hash.prototype.init = function () {
  46. var _this = this;
  47. var _hash;
  48. // Change hash value on after each slide transition
  49. utils.on(_this.core.el, 'onAfterSlide.lgtm', function (event) {
  50. window.location.hash = 'lg=' + _this.core.s.galleryId + '&slide=' + event.detail.index;
  51. });
  52. // Listen hash change and change the slide according to slide value
  53. utils.on(window, 'hashchange.lghash', function () {
  54. _hash = window.location.hash;
  55. var _idx = parseInt(_hash.split('&slide=')[1], 10);
  56. // it galleryId doesn't exist in the url close the gallery
  57. if (_hash.indexOf('lg=' + _this.core.s.galleryId) > -1) {
  58. _this.core.slide(_idx, false, false);
  59. } else if (_this.core.lGalleryOn) {
  60. _this.core.destroy();
  61. }
  62. });
  63. };
  64. Hash.prototype.destroy = function () {
  65. if (!this.core.s.hash) {
  66. return;
  67. }
  68. // Reset to old hash value
  69. if (this.oldHash && this.oldHash.indexOf('lg=' + this.core.s.galleryId) < 0) {
  70. window.location.hash = this.oldHash;
  71. } else {
  72. if (history.pushState) {
  73. history.pushState('', document.title, window.location.pathname + window.location.search);
  74. } else {
  75. window.location.hash = '';
  76. }
  77. }
  78. utils.off(this.core.el, '.lghash');
  79. };
  80. window.lgModules.hash = Hash;
  81. });
  82. },{}]},{},[1])(1)
  83. });