Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / foundation / foundation.reveal.js @ b3c1328b

History | View | Annotate | Download (7.5 kB)

1
/*jslint unparam: true, browser: true, indent: 2 */
2

    
3
;(function ($, window, document, undefined) {
4
  'use strict';
5

    
6
  Foundation.libs.reveal = {
7
    name: 'reveal',
8

    
9
    version : '4.0.9',
10

    
11
    locked : false,
12

    
13
    settings : {
14
      animation: 'fadeAndPop',
15
      animationSpeed: 250,
16
      closeOnBackgroundClick: true,
17
      dismissModalClass: 'close-reveal-modal',
18
      bgClass: 'reveal-modal-bg',
19
      open: function(){},
20
      opened: function(){},
21
      close: function(){},
22
      closed: function(){},
23
      bg : $('.reveal-modal-bg'),
24
      css : {
25
        open : {
26
          'opacity': 0,
27
          'visibility': 'visible',
28
          'display' : 'block'
29
        },
30
        close : {
31
          'opacity': 1,
32
          'visibility': 'hidden',
33
          'display': 'none'
34
        }
35
      }
36
    },
37

    
38
    init : function (scope, method, options) {
39
      this.scope = scope || this.scope;
40
      Foundation.inherit(this, 'data_options delay');
41

    
42
      if (typeof method === 'object') {
43
        $.extend(true, this.settings, method);
44
      }
45

    
46
      if (typeof method != 'string') {
47
        this.events();
48

    
49
        return this.settings.init;
50
      } else {
51
        return this[method].call(this, options);
52
      }
53
    },
54

    
55
    events : function () {
56
      var self = this;
57

    
58
      $(this.scope)
59
        .off('.fndtn.reveal')
60
        .on('click.fndtn.reveal', '[data-reveal-id]', function (e) {
61
          e.preventDefault();
62
          if (!self.locked) {
63
            self.locked = true;
64
            self.open.call(self, $(this));
65
          }
66
        })
67
        .on('click.fndtn.reveal touchend.click.fndtn.reveal', this.close_targets(), function (e) {
68
          e.preventDefault();
69
          if (!self.locked) {
70
            self.locked = true;
71
            self.close.call(self, $(this).closest('.reveal-modal'));
72
          }
73
        })
74
        .on('open.fndtn.reveal', '.reveal-modal', this.settings.open)
75
        .on('opened.fndtn.reveal', '.reveal-modal', this.settings.opened)
76
        .on('opened.fndtn.reveal', '.reveal-modal', this.open_video)
77
        .on('close.fndtn.reveal', '.reveal-modal', this.settings.close)
78
        .on('closed.fndtn.reveal', '.reveal-modal', this.settings.closed)
79
        .on('closed.fndtn.reveal', '.reveal-modal', this.close_video);
80

    
81
      return true;
82
    },
83

    
84
    open : function (target) {
85
      if (target) {
86
        var modal = $('#' + target.data('reveal-id'));
87
      } else {
88
        var modal = $(this.scope);
89
      }
90

    
91
      if (!modal.hasClass('open')) {
92
        var open_modal = $('.reveal-modal.open');
93

    
94
        if (typeof modal.data('css-top') === 'undefined') {
95
          modal.data('css-top', parseInt(modal.css('top'), 10))
96
            .data('offset', this.cache_offset(modal));
97
        }
98

    
99
        modal.trigger('open');
100

    
101
        if (open_modal.length < 1) {
102
          this.toggle_bg(modal);
103
        }
104
        this.hide(open_modal, this.settings.css.open);
105
        this.show(modal, this.settings.css.open);
106
      }
107
    },
108

    
109
    close : function (modal) {
110

    
111
      var modal = modal || $(this.scope),
112
          open_modals = $('.reveal-modal.open');
113

    
114
      if (open_modals.length > 0) {
115
        this.locked = true;
116
        modal.trigger('close');
117
        this.toggle_bg(modal);
118
        this.hide(open_modals, this.settings.css.close);
119
      }
120
    },
121

    
122
    close_targets : function () {
123
      var base = '.' + this.settings.dismissModalClass;
124

    
125
      if (this.settings.closeOnBackgroundClick) {
126
        return base + ', .' + this.settings.bgClass;
127
      }
128

    
129
      return base;
130
    },
131

    
132
    toggle_bg : function (modal) {
133
      if ($('.reveal-modal-bg').length === 0) {
134
        this.settings.bg = $('<div />', {'class': this.settings.bgClass})
135
          .insertAfter(modal);
136
      }
137

    
138
      if (this.settings.bg.filter(':visible').length > 0) {
139
        this.hide(this.settings.bg);
140
      } else {
141
        this.show(this.settings.bg);
142
      }
143
    },
144

    
145
    show : function (el, css) {
146
      // is modal
147
      if (css) {
148
        if (/pop/i.test(this.settings.animation)) {
149
          css.top = $(window).scrollTop() - el.data('offset') + 'px';
150
          var end_css = {
151
            top: $(window).scrollTop() + el.data('css-top') + 'px',
152
            opacity: 1
153
          }
154

    
155
          return this.delay(function () {
156
            return el
157
              .css(css)
158
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
159
                this.locked = false;
160
                el.trigger('opened');
161
              }.bind(this))
162
              .addClass('open');
163
          }.bind(this), this.settings.animationSpeed / 2);
164
        }
165

    
166
        if (/fade/i.test(this.settings.animation)) {
167
          var end_css = {opacity: 1};
168

    
169
          return this.delay(function () {
170
            return el
171
              .css(css)
172
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
173
                this.locked = false;
174
                el.trigger('opened');
175
              }.bind(this))
176
              .addClass('open');
177
          }.bind(this), this.settings.animationSpeed / 2);
178
        }
179

    
180
        return el.css(css).show().css({opacity: 1}).addClass('open').trigger('opened');
181
      }
182

    
183
      // should we animate the background?
184
      if (/fade/i.test(this.settings.animation)) {
185
        return el.fadeIn(this.settings.animationSpeed / 2);
186
      }
187

    
188
      return el.show();
189
    },
190

    
191
    hide : function (el, css) {
192
      // is modal
193
      if (css) {
194
        if (/pop/i.test(this.settings.animation)) {
195
          var end_css = {
196
            top: - $(window).scrollTop() - el.data('offset') + 'px',
197
            opacity: 0
198
          };
199

    
200
          return this.delay(function () {
201
            return el
202
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
203
                this.locked = false;
204
                el.css(css).trigger('closed');
205
              }.bind(this))
206
              .removeClass('open');
207
          }.bind(this), this.settings.animationSpeed / 2);
208
        }
209

    
210
        if (/fade/i.test(this.settings.animation)) {
211
          var end_css = {opacity: 0};
212

    
213
          return this.delay(function () {
214
            return el
215
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
216
                this.locked = false;
217
                el.css(css).trigger('closed');
218
              }.bind(this))
219
              .removeClass('open');
220
          }.bind(this), this.settings.animationSpeed / 2);
221
        }
222

    
223
        return el.hide().css(css).removeClass('open').trigger('closed');
224
      }
225

    
226
      // should we animate the background?
227
      if (/fade/i.test(this.settings.animation)) {
228
        return el.fadeOut(this.settings.animationSpeed / 2);
229
      }
230

    
231
      return el.hide();
232
    },
233

    
234
    close_video : function (e) {
235
      var video = $(this).find('.flex-video'),
236
          iframe = video.find('iframe');
237

    
238
      if (iframe.length > 0) {
239
        iframe.attr('data-src', iframe[0].src);
240
        iframe.attr('src', 'about:blank');
241
        video.fadeOut(100).hide();
242
      }
243
    },
244

    
245
    open_video : function (e) {
246
      var video = $(this).find('.flex-video'),
247
          iframe = video.find('iframe');
248

    
249
      if (iframe.length > 0) {
250
        var data_src = iframe.attr('data-src');
251
        if (typeof data_src === 'string') {
252
          iframe[0].src = iframe.attr('data-src');
253
        }
254
        video.show().fadeIn(100);
255
      }
256
    },
257

    
258
    cache_offset : function (modal) {
259
      var offset = modal.show().height() + parseInt(modal.css('top'), 10);
260

    
261
      modal.hide();
262

    
263
      return offset;
264
    },
265

    
266
    off : function () {
267
      $(this.scope).off('.fndtn.reveal');
268
    }
269
  };
270
}(Foundation.zj, this, this.document));