summaryrefslogtreecommitdiff
path: root/frontend/gamma/js/JQTouch/extensions/jqt.actionsheet.js
blob: 2a5f8d1dfca9def1209f09bad38fa19809aed96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*

            _/    _/_/    _/_/_/_/_/                              _/
               _/    _/      _/      _/_/    _/    _/    _/_/_/  _/_/_/
          _/  _/  _/_/      _/    _/    _/  _/    _/  _/        _/    _/
         _/  _/    _/      _/    _/    _/  _/    _/  _/        _/    _/
        _/    _/_/  _/    _/      _/_/      _/_/_/    _/_/_/  _/    _/
       _/
    _/

    Documentation and issue tracking on Google Code <http://code.google.com/p/jqtouch/>

    (c) 2012 by jQTouch project members.
    See LICENSE.txt for license.

    Author: Thomas Yip
*/

/*

            _/    _/_/    _/_/_/_/_/                              _/
               _/    _/      _/      _/_/    _/    _/    _/_/_/  _/_/_/
          _/  _/  _/_/      _/    _/    _/  _/    _/  _/        _/    _/
         _/  _/    _/      _/    _/    _/  _/    _/  _/        _/    _/
        _/    _/_/  _/    _/      _/_/      _/_/_/    _/_/_/  _/    _/
       _/
    _/

    Documentation and issue tracking on Google Code <http://code.google.com/p/jqtouch/>

    (c) 2012 by jQTouch project members.
    See LICENSE.txt for license.

    Author: Thomas Yip
*/

(function($) {
    var src = $("head script").last().attr("src") || '';
    var scriptpath = src.split('?')[0].split('/').slice(0, -1).join('/')+'/';
    var csspath = scriptpath + 'jqt.actionsheet.css';
    var link = $('<link href="' + csspath + '" rel="stylesheet">');
    $('head').append($(link));

    function hide(callback) {
        var $target = $(this);
        var data = $(this).data('actionsheet');
        var $source = data.source;

        var timeout;

        function cleanup() {
          clearTimeout(timeout);

          $source.removeClass('transition');
          $target.removeClass('inmotion transition');
          !callback || callback.apply(this, arguments);
        };
        timeout = setTimeout(cleanup, 500);

        if (data.shown) {
            $(this).data('actionsheet', {});
            $target.one('webkitTransitionEnd', cleanup);
    
            $source.addClass('transition');
            $target.removeClass('current').addClass('inmotion transition');
            $('#jqt').removeClass('actionopened');
        }
        return $target;
    }
      
    function show(callback) {
        var $target = $(this);
        var data = $(this).data('actionsheet') || {};
        if (!data.shown) {
            var $source = $('#jqt .current:not(.actionsheet)');
    
            $target.one('webkitTransitionEnd', function() {
                $source.removeClass('transition');
                $target.removeClass('inmotion transition');
                !callback || callback.apply(this, arguments);
            });
    
            data.shown = true;
            data.source = $source;
            $(this).data('actionsheet', data);

            $source.addClass('transition');
            $target.addClass('inmotion transition');
            $('#jqt').addClass('actionopened');
            setTimeout(function() {
                $target.addClass('current');
            }, 50);
        }
        return $target;
    }
    
    var methods = {
        init: function(options) {
            $(this).addClass('actionsheet');
            $(this).data({shown: false});
        },
        show: show,
        hide: hide
    };
    
    $.fn.actionsheet = function(method) {
      if (methods[method]) {
          if ($(this).is('.actionsheet')) {
              return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
          } else {
              var msg = 'Target is not a `actionsheet`. Action `' + method + '` is ignored.';
              console.warn(msg);
          }
      } else if ( typeof method === 'object' || ! method ) {
          return methods.init.apply(this, arguments);
      } else {
          $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
      }        
    };

    if ($.jQTouch) {
        $.jQTouch.addTapHandler({
            name: 'open-actionsheet',
            isSupported: function(e, params) {
                return params.$el.is('.action');
            },
            fn: function(e, params) {
                params.$el.removeClass('active');

                var $target = $(params.hash);
                $target.actionsheet('show');

                return false;
            }
        });
        $.jQTouch.addTapHandler({
            name: 'follow-actionlink',
            isSupported: function(e, params) {
                if ($('#jqt').hasClass('actionopened')) {
                    return params.$el.is('.actionsheet a');
                }
                return false;
            },
            fn: function(e, params) {
                params.$el.removeClass('active');

                var $target = params.$el.closest('.actionsheet');
                $target.actionsheet('hide', function() {
                    if (!params.$el.is('.dismiss')) {
                      params.$el.trigger('tap');
                    }
                });
                return false;
            }
        });
    } else {
        console.error('Extension `jqt.actionsheet` failed to load. jQT not found');
    }
})($);