1/**
2 * Federated Login for DokuWiki - admin javascript
3 *
4 * @copyright  2012 Aoi Karasu
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @link       http://www.dokuwiki.org/plugin:fedauth
7 * @author     Aoi Karasu <aoikarasu@gmail.com>
8 */
9
10var fa_manager = {
11    /**
12     * Binds actions to the form buttons.
13     */
14    init : function() {
15        if (jQuery('#fedauth__manager').length === 0) {
16            return;
17        }
18        /* Bind action for each details button click */
19        fa_manager.bindaction('details', function(target, id) {
20            if (jQuery(target).attr('collapse') == 'yes') {
21                jQuery(target).removeAttr('collapse');
22                jQuery('#fa__det_'+id+' .details').toggle();
23                return;
24            }
25            fa_manager.loadinfo(target, id);
26        });
27
28        /* Bind action for each move and transfer buttons click */
29        fa_manager.bindaction('movedn', fa_manager.providerslidedown);
30        fa_manager.bindaction('moveup', fa_manager.providerslideup);
31        fa_manager.bindaction('uselarge', fa_manager.provideruselarge);
32        fa_manager.bindaction('usesmall', fa_manager.providerusesmall);
33
34        /* Bind action for each toggle button click */
35        fa_manager.bindaction('toggle', function(target) {
36             var did = jQuery(target).closest('div[id^="fa__"]').attr('id');
37             did = did.substring(did.lastIndexOf('_')+1);
38             fa_manager.toggleproviders(target, did);
39        });
40    },
41
42    /**
43     * Binds a click event handler to all buttons matching action name.
44     *
45     * @param string actname the action name
46     * @param function handler the click event handler function
47     * @param string id (optional) associated identifier
48     */
49    bindaction: function (actname, handler, id) {
50        jQuery('#fedauth__manager input[name^="fa['+actname+']"]')
51            .prop('type','button')
52            .unbind('click')
53            .click(function() {
54                var handle = typeof id == 'undefined' ? fa_manager._stripprovid(this) : id;
55                handler(this, handle);
56                return false;
57            });
58    },
59
60    /**
61     * Loads the current authorization service info.
62     */
63    loadinfo: function (sender, target) {
64        jQuery('#fa__det_'+target)
65            .html(fa_manager._throbber(10))
66            .load(
67                FEDAUTH_BASE + 'ajax.php',
68                jQuery('#fa__large form').serialize()+'&ajax=details&fa[details]['+target+']',
69                function() {
70                    jQuery(sender).attr('collapse', 'yes');
71                    jQuery('#fa__det_'+target+' .details').toggle();
72                }
73            );
74        return false;
75    },
76
77    /**
78     * Toggles the enabled state of providers from the target list.
79     */
80    toggleproviders: function (sender, target) {
81        var data = decodeURIComponent(jQuery('#fa__'+target+' form').serialize())+'&ajax=toggle&fa[toggle]';
82        jQuery('#axwrap__'+target)
83            .html(fa_manager._throbber())
84            .load(
85                FEDAUTH_BASE + 'ajax.php',
86                data,
87                fa_manager.init
88            );
89        return false;
90    },
91
92    /**
93     * Moves up a provider item in the list with the slide effect.
94     */
95    providerslideup: function (sender, target) {
96        fa_manager._prepareitem(sender);
97        jQuery
98            .ajax(FEDAUTH_BASE + 'ajax.php',
99                { data: jQuery('#fa__large form').serialize()+'&ajax=moveup&fa[moveup]['+target+']' })
100            .success(function(data) {
101                var ret = jQuery.parseJSON(data);
102                if (!ret['success']) return;
103                var cur = jQuery(sender).parent();
104                var prv = cur.prev();
105                prv.before(cur);
106                fa_manager._switchclass(prv);
107                fa_manager._switchclass(cur);
108                fa_manager._easeitem(sender);
109            })
110            .error(function() { alert("AJAX error!"); fa_manager._easeitem(sender); });
111    },
112
113    /**
114     * Moves down a provider item in the list with the slide effect.
115     */
116    providerslidedown: function (sender, target) {
117        fa_manager._prepareitem(sender);
118        jQuery
119            .ajax(FEDAUTH_BASE + 'ajax.php',
120                { data: jQuery('#fa__large form').serialize()+'&ajax=movedn&fa[movedn]['+target+']' })
121            .success(function(data) {
122                var ret = jQuery.parseJSON(data);
123                if (!ret['success']) return;
124                var cur = jQuery(sender).parent();
125                var prv = cur.next();
126                prv.after(cur);
127                fa_manager._switchclass(prv);
128                fa_manager._switchclass(cur);
129                fa_manager._easeitem(sender);
130            })
131            .error(function() { alert("AJAX error!"); fa_manager._easeitem(sender); });
132    },
133
134    /**
135     * Transfers a provider item to the large buttons list with scroll and fadein effect.
136     */
137    provideruselarge: function (sender, target) {
138        fa_manager._prepareitem(sender);
139        jQuery
140            .ajax(FEDAUTH_BASE + 'ajax.php',
141                { data: jQuery('#fa__large form').serialize()+'&ajax=uselarge&fa[uselarge]['+target+']' })
142            .success(function(data) {
143                var ret = jQuery.parseJSON(data);
144                if (!ret['success']) return;
145                var cur = jQuery(sender).parent();
146                cur.slideUp(function() {
147                    var owner = jQuery('#axwrap__large').last();
148                    owner.append(cur);
149                    fa_manager._updateitem(sender, target, ret, fa_manager.providerusesmall);
150                    cur.slideDown();
151                    fa_manager._easeitem(sender);
152                    fa_manager._easeitem(jQuery('#axwrap__small fieldset input[type="button"]').get(0));
153                });
154            })
155            .error(function() { alert("AJAX error!"); fa_manager._easeitem(sender); });
156    },
157
158    /**
159     * Transfers a provider item to the small buttons list with scroll and fadein effect.
160     */
161    providerusesmall: function (sender, target) {
162        fa_manager._prepareitem(sender);
163        jQuery
164            .ajax(FEDAUTH_BASE + 'ajax.php',
165                { data: jQuery('#fa__large form').serialize()+'&ajax=usesmall&fa[usesmall]['+target+']' })
166            .success(function(data) {
167                var ret = jQuery.parseJSON(data);
168                if (!ret['success']) return;
169                var cur = jQuery(sender).parent();
170                cur.slideUp(function() {
171                    var owner = jQuery('#axwrap__small').last();
172                    owner.append(cur);
173                    fa_manager._updateitem(sender, target, ret, fa_manager.provideruselarge);
174                    cur.slideDown();
175                    fa_manager._easeitem(sender);
176                    fa_manager._easeitem(jQuery('#axwrap__large fieldset input[type="button"]').get(0));
177                });
178            })
179            .error(function() { alert("AJAX error!"); fa_manager._easeitem(sender); });
180    },
181
182    /**
183     * Removes the throbber image and enables action buttons for all items in the sender's scope,
184     * and disables the move up and move down buttons for first and last item respectively.
185     */
186    _easeitem: function (sender) {
187       jQuery(sender).siblings('img').remove();
188       jQuery(sender).parent().parent().find('input[type="button"]').removeAttr('disabled');
189       /* ensure move up/dn buttons are disabled for first/last item */
190       var fsets = jQuery(sender).parent().parent().children('fieldset');
191       fsets.first().children('input[name^="fa[moveup"]').attr('disabled','disabled');
192       fsets.last().children('input[name^="fa[movedn"]').attr('disabled','disabled');
193    },
194
195    /**
196     * Displays the throbber image and disables action buttons for all items in the sender's scope.
197     */
198    _prepareitem: function (sender) {
199       jQuery(sender).siblings('div[class="legend"]').after(fa_manager._throbber());
200       jQuery(sender).parent().parent().find('input[type="button"]').attr('disabled','disabled');
201    },
202
203    /**
204     * Switches the position-in-list CSS class for a provider item to the opposite one.
205     */
206    _switchclass: function(src) {
207       if (src.hasClass('even')) {
208           src.removeClass('even');
209       } else if (src.hasClass('disabledeven')) {
210           src.removeClass('disabledeven').addClass('disabled');
211       } else if (src.hasClass('disabled')) {
212           src.removeClass('disabled').addClass('disabledeven');
213       } else {
214           src.addClass('even');
215       }
216    },
217
218    /**
219     * Updates the transfer button and the position-in-list CSS class for a provider item to its sibling opposite.
220     *
221     * @param object src buttom DOM object
222     * @param string target the provider identifier
223     * @param object data object with new button XHTML attributes
224     * @param function clickhandler new button click hander function
225     */
226    _updateitem: function (src, target, data, clickhandler) {
227        jQuery(src).attr('name', data.name).attr('title', data.title).attr('value', data.value)
228                   .unbind('click').click(function() { clickhandler(this, target); return false; });
229        fa_manager._updateeven('small');
230        fa_manager._updateeven('large');
231    },
232
233    _updateeven: function(listname) {
234        var even = true;
235        jQuery('#axwrap__'+listname).children('fieldset').each(function() {
236           var it = jQuery(this);
237           var disabled = it.is('.disabled, .disabledeven') ? 'disabled' : '';
238           even = !even;
239           it.removeClass('even disabled disabledeven').addClass(disabled+(even ? 'even' : ''));
240        });
241    },
242
243    /**
244     * Strips the provider id from a DOM element's name attribute.
245     */
246    _stripprovid: function (elem) {
247        var pid = jQuery(elem).attr('name');
248        return pid.substring(pid.lastIndexOf('[')+1, pid.lastIndexOf(']'));
249    },
250
251    /**
252     * Builds the throbber image XHTML tag.
253     */
254    _throbber: function (margin, valign) {
255        margin = typeof margin !== 'undefined' ? new String(margin) : '0';
256        valign = typeof valign !== 'undefined' ? valign : 'middle';
257        return '<img src="'+DOKU_BASE+'lib/images/throbber.gif" alt="..." style="margin: '+margin+'px; vertical-align: '+valign+';"/>';
258    }
259};
260
261jQuery(fa_manager.init);
262
263/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
264