1/*! lg-hash - v1.0.0 - 2016-09-20
2* http://sachinchoolur.github.io/lightGallery
3* Copyright (c) 2016 Sachin N; Licensed GPLv3 */
4
5(function (root, factory) {
6  if (typeof define === 'function' && define.amd) {
7    // AMD. Register as an anonymous module unless amdModuleId is set
8    define([], function () {
9      return (factory());
10    });
11  } else if (typeof exports === 'object') {
12    // Node. Does not work with strict CommonJS, but
13    // only CommonJS-like environments that support module.exports,
14    // like Node.
15    module.exports = factory();
16  } else {
17    factory();
18  }
19}(this, function () {
20
21(function($, window, document, undefined) {
22
23    'use strict';
24
25    var defaults = {
26        hash: true
27    };
28
29    var Hash = function(element) {
30
31        this.core = $(element).data('lightGallery');
32
33        this.core.s = $.extend({}, defaults, this.core.s);
34
35        if (this.core.s.hash) {
36            this.oldHash = window.location.hash;
37            this.init();
38        }
39
40        return this;
41    };
42
43    Hash.prototype.init = function() {
44        var _this = this;
45        var _hash;
46
47        // Change hash value on after each slide transition
48        _this.core.$el.on('onAfterSlide.lg.tm', function(event, prevIndex, index) {
49            window.location.hash = 'lg=' + _this.core.s.galleryId + '&slide=' + index;
50        });
51
52        // Listen hash change and change the slide according to slide value
53        $(window).on('hashchange.lg.hash', function() {
54            _hash = window.location.hash;
55            var _idx = parseInt(_hash.split('&slide=')[1], 10);
56
57            // it galleryId doesn't exist in the url close the gallery
58            if ((_hash.indexOf('lg=' + _this.core.s.galleryId) > -1)) {
59                _this.core.slide(_idx, false, false);
60            } else if (_this.core.lGalleryOn) {
61                _this.core.destroy();
62            }
63
64        });
65    };
66
67    Hash.prototype.destroy = function() {
68
69        if (!this.core.s.hash) {
70            return;
71        }
72
73        // Reset to old hash value
74        if (this.oldHash && this.oldHash.indexOf('lg=' + this.core.s.galleryId) < 0) {
75            window.location.hash = this.oldHash;
76        } else {
77            if (history.pushState) {
78                history.pushState('', document.title, window.location.pathname + window.location.search);
79            } else {
80                window.location.hash = '';
81            }
82        }
83
84        this.core.$el.off('.lg.hash');
85
86    };
87
88    $.fn.lightGallery.modules.hash = Hash;
89
90})(jQuery, window, document);
91
92
93}));
94