Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / new_ui / ui / vendor / foundation / foundation.reveal.js @ faad3c72

History | View | Annotate | Download (7.8 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
      closeOnEsc: true,
18
      dismissModalClass: 'close-reveal-modal',
19
      bgClass: 'reveal-modal-bg',
20
      open: function(){},
21
      opened: function(){},
22
      close: function(){},
23
      closed: function(){},
24
      bg : $('.reveal-modal-bg'),
25
      css : {
26
        open : {
27
          'opacity': 0,
28
          'visibility': 'visible',
29
          'display' : 'block'
30
        },
31
        close : {
32
          'opacity': 1,
33
          'visibility': 'hidden',
34
          'display': 'none'
35
        }
36
      }
37
    },
38

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

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

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

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

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

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

    
82
        $( 'body' ).bind( 'keyup.reveal', function ( event ) {
83
          var open_modal = $('.reveal-modal.open'),
84
              settings = $.extend({}, self.settings, self.data_options(open_modal));
85
          if ( event.which === 27  && settings.closeOnEsc) { // 27 is the keycode for the Escape key
86
            open_modal.foundation('reveal', 'close');
87
          }
88
        });
89

    
90
      return true;
91
    },
92

    
93
    open : function (target) {
94
      if (target) {
95
        var modal = $('#' + target.data('reveal-id'));
96
      } else {
97
        var modal = $(this.scope);
98
      }
99

    
100
      if (!modal.hasClass('open')) {
101
        var open_modal = $('.reveal-modal.open');
102

    
103
        if (typeof modal.data('css-top') === 'undefined') {
104
          modal.data('css-top', parseInt(modal.css('top'), 10))
105
            .data('offset', this.cache_offset(modal));
106
        }
107

    
108
        modal.trigger('open');
109

    
110
        if (open_modal.length < 1) {
111
          this.toggle_bg(modal);
112
        }
113
        this.hide(open_modal, this.settings.css.open);
114
        this.show(modal, this.settings.css.open);
115
      }
116
    },
117

    
118
    close : function (modal) {
119

    
120
      var modal = modal || $(this.scope),
121
          open_modals = $('.reveal-modal.open');
122

    
123
      if (open_modals.length > 0) {
124
        this.locked = true;
125
        modal.trigger('close');
126
        this.toggle_bg(modal);
127
        this.hide(open_modals, this.settings.css.close);
128
      }
129
    },
130

    
131
    close_targets : function () {
132
      var base = '.' + this.settings.dismissModalClass;
133

    
134
      if (this.settings.closeOnBackgroundClick) {
135
        return base + ', .' + this.settings.bgClass;
136
      }
137

    
138
      return base;
139
    },
140

    
141
    toggle_bg : function (modal) {
142
      if ($('.reveal-modal-bg').length === 0) {
143
        this.settings.bg = $('<div />', {'class': this.settings.bgClass})
144
          .insertAfter(modal);
145
      }
146

    
147
      if (this.settings.bg.filter(':visible').length > 0) {
148
        this.hide(this.settings.bg);
149
      } else {
150
        this.show(this.settings.bg);
151
      }
152
    },
153

    
154
    show : function (el, css) {
155
      // is modal
156
      if (css) {
157
        if (/pop/i.test(this.settings.animation)) {
158
          css.top = $(window).scrollTop() - el.data('offset') + 'px';
159
          var end_css = {
160
            top: $(window).scrollTop() + el.data('css-top') + 'px',
161
            opacity: 1
162
          }
163

    
164
          return this.delay(function () {
165
            return el
166
              .css(css)
167
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
168
                this.locked = false;
169
                el.trigger('opened');
170
              }.bind(this))
171
              .addClass('open');
172
          }.bind(this), this.settings.animationSpeed / 2);
173
        }
174

    
175
        if (/fade/i.test(this.settings.animation)) {
176
          var end_css = {opacity: 1};
177

    
178
          return this.delay(function () {
179
            return el
180
              .css(css)
181
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
182
                this.locked = false;
183
                el.trigger('opened');
184
              }.bind(this))
185
              .addClass('open');
186
          }.bind(this), this.settings.animationSpeed / 2);
187
        }
188

    
189
        return el.css(css).show().css({opacity: 1}).addClass('open').trigger('opened');
190
      }
191

    
192
      // should we animate the background?
193
      if (/fade/i.test(this.settings.animation)) {
194
        return el.fadeIn(this.settings.animationSpeed / 2);
195
      }
196

    
197
      return el.show();
198
    },
199

    
200
    hide : function (el, css) {
201
      // is modal
202
      if (css) {
203
        if (/pop/i.test(this.settings.animation)) {
204
          var end_css = {
205
            top: - $(window).scrollTop() - el.data('offset') + 'px',
206
            opacity: 0
207
          };
208

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

    
219
        if (/fade/i.test(this.settings.animation)) {
220
          var end_css = {opacity: 0};
221

    
222
          return this.delay(function () {
223
            return el
224
              .animate(end_css, this.settings.animationSpeed, 'linear', function () {
225
                this.locked = false;
226
                el.css(css).trigger('closed');
227
              }.bind(this))
228
              .removeClass('open');
229
          }.bind(this), this.settings.animationSpeed / 2);
230
        }
231

    
232
        return el.hide().css(css).removeClass('open').trigger('closed');
233
      }
234

    
235
      // should we animate the background?
236
      if (/fade/i.test(this.settings.animation)) {
237
        return el.fadeOut(this.settings.animationSpeed / 2);
238
      }
239

    
240
      return el.hide();
241
    },
242

    
243
    close_video : function (e) {
244
      var video = $(this).find('.flex-video'),
245
          iframe = video.find('iframe');
246

    
247
      if (iframe.length > 0) {
248
        iframe.attr('data-src', iframe[0].src);
249
        iframe.attr('src', 'about:blank');
250
        video.fadeOut(100).hide();
251
      }
252
    },
253

    
254
    open_video : function (e) {
255
      var video = $(this).find('.flex-video'),
256
          iframe = video.find('iframe');
257

    
258
      if (iframe.length > 0) {
259
        var data_src = iframe.attr('data-src');
260
        if (typeof data_src === 'string') {
261
          iframe[0].src = iframe.attr('data-src');
262
        }
263
        video.show().fadeIn(100);
264
      }
265
    },
266

    
267
    cache_offset : function (modal) {
268
      var offset = modal.show().height() + parseInt(modal.css('top'), 10);
269

    
270
      modal.hide();
271

    
272
      return offset;
273
    },
274

    
275
    off : function () {
276
      $(this.scope).off('.fndtn.reveal');
277
    }
278
  };
279
}(Foundation.zj, this, this.document));