Completed
Push — master ( 309357...7ad46f )
by Andreas
18:00
created

midcom.datamanager/privilege/jquery.privilege.js (3 issues)

1
(function($){
2
3
    /**
4
     * background mover - jQuery plugin
5
     */
6
7
    /**
8
     * Moves elements background depending on given parameters
9
     *
10
     * @example jQuery('a.image-link').moveBackground({startPos: 0, endPos: -16});
11
     * @cat plugin
12
     * @type jQuery
13
     */
14
    $.fn.extend({
15
        moveBackground: function(settings) {
16
            settings = $.extend({
17
                direction: "down",
18
                startPos: 0,
19
                endPos: 0,
20
                time: null,
21
                startLeft: null,
22
                startTop: null,
23
                callback: null,
24
                callbackArgs: []
25
            }, settings);
26
27
            var element = this[0],
28
                direction = settings.direction,
29
                startPos = settings.startPos,
30
                endPos = settings.endPos,
31
                time = settings.time;
32
33
            if (direction == "down" || direction == "d")
34
            {
35
                endPos -= 1;
36
            }
37
38
            var left = settings.startLeft != null ? settings.startLeft : 0,
39
                top = settings.startTop != null ? settings.startTop : 0;
40
41
            if (direction == "down" || direction == "d") {
42
                top = startPos;
43
            }
44
45
            function anim()
46
            {
47
                var leftPos = "px",
48
                    topPos = "px";
49
50
                if (direction == "down" || direction == "d")
51
                {
52
                    if (endPos < top) {
53
                        top -= 1;
54
                    }
55
56
                    if (top != endPos) {
57
                        setTimeout(anim, time);
58
                    } else {
59
                        if (settings.callback != null) {
60
                            element.style.backgroundPosition = "";
61
                            settings.callback.call(settings.callbackArgs[0], settings.callbackArgs);
62
                        }
63
                        return this;
64
                    }
65
                }
66
67
                leftPos = left + leftPos;
68
                topPos = top + topPos;
69
70
                var posStr = leftPos + " " + topPos;
71
72
                element.style.backgroundPosition = posStr;
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
73
            }
74
75
            anim();
76
        },
77
78
        /**
79
         * privilege renderer - jQuery plugin
80
         */
81
82
        /**
83
         * Create a multiface checkbox interface out of a simple form structure.
84
         *
85
         * @example jQuery('#select-holder').render_privilege();
86
         * @cat plugin
87
         * @type jQuery
88
         */
89
        render_privilege: function(settings) {
90
            settings = $.extend({
91
                imageWidth: 16,
92
                imageHeight: 16,
93
                maxIndex: 3,
94
                animate: false
95
            }, settings || {});
96
97
            return this.each(function() {
98
                if ($(this).hasClass('privilege_rendered')) {
99
                    return;
100
                }
101
                $(this).addClass('privilege_rendered');
102
103
                var div = $("<div/>").insertAfter( this ),
104
105
                    list_menu = $(this).find("select")[0],
106
                    nextValue = 0,
107
                    selected_index = 0;
108
109
                $(list_menu).each(function(){
110
                    $(this).on('change', function() {
111
                        div.find('div.privilege_val').trigger('click');
112
                    });
113
                });
114
115
                $(this).find("select option").each(function(){
116
                    var classes = [ null, 'allow', 'deny', 'inherited'],
117
                        block_style = this.selected ? "style='display: block;'" : "",
118
                        value;
119
                    if (this.value === null || this.value == 3) {
120
                        value = 'inherited-' + settings.effective_value;
121
                    }
122
                    else {
123
                        value = classes[this.value];
124
                        if (value !== settings.effective_value) {
125
                            value += ' ineffectual';
126
                        }
127
                    }
128
129
                    div.append( "<div class='privilege_val' " + block_style + "><a class='" + value + "' href='#" + this.value + "' title='" + this.innerHTML + "'>" + this.innerHTML + "</a></div>" );
130
                });
131
132
                var selects = div.find('div.privilege_val').click(function(){
133
                    selected_index = selects.index(this) + 1;
134
135
                    var href = $(this).find('a')[0].href,
136
                        currentValue = href.charAt(href.length-1),
137
                        startPos, nextHref;
138
139
                    if (prevValue == undefined) {
0 ignored issues
show
The variable prevValue seems to be never initialized.
Loading history...
140
                        var prevValue = currentValue;
141
                    } else {
142
                        prevValue = currentValue;
143
                    }
144
145
                    if (selected_index == settings.maxIndex) {
146
                        selected_index = 0;
147
                        startPos = 0;
148
                    } else {
149
                        startPos = 0 - (prevValue * settings.imageHeight);
150
                    }
151
152
153
                    var idx = selected_index;
154
                    if (selected_index >= settings.maxIndex) {
155
                        idx = settings.maxIndex;
156
                    }
157
158
                    nextHref = $(selects[idx]).find('a:eq(0)').attr('href');
159
                    nextValue = nextHref.charAt(nextHref.length - 1);
160
161
                    var endPos = 0 - (nextValue * settings.imageHeight);
162
163
                    if (settings.animate == true) {
164
                        $(this).find('a:eq(0)').moveBackground({
165
                            startPos: startPos,
166
                            endPos: endPos,
167
                            time: 25,
168
                            callback: showNext,
169
                            callbackArgs: [this,selects[selected_index]]
170
                        });
171
                    } else {
172
                        showNext([this]);
173
                    }
174
175
                    list_menu.selectedIndex = selected_index;
176
177
                    prevValue = currentValue;
0 ignored issues
show
The assignment to variable prevValue seems to be never used. Consider removing it.
Loading history...
178
179
                    return false;
180
                });
181
182
                function showNext(args) {
183
                    $(args[0]).hide();
184
                    $(selects[selected_index]).show();
185
                }
186
187
            }).hide();
188
        },
189
190
        privilege_actions: function(privilege_key) {
191
            return this.each(function() {
192
                if ($(this).find('.privilege_action').length > 0) {
193
                    return;
194
                }
195
196
                var row = this,
197
                    actions_holder = $('#privilege_row_actions_' + privilege_key, row),
198
                    clear_action = $('<div class="privilege_action" />').insertAfter( actions_holder );
199
200
                $('<img src="' + MIDCOM_STATIC_URL + '/stock-icons/16x16/trash.png" />').attr({
201
                        alt: "Clear privileges",
202
                        border: 0
203
                    }).appendTo(clear_action);
204
205
                clear_action.on('click', function() {
206
                    $('select', row).each(function(i, n) {
207
                        $(n).val(3).trigger('onchange', [0]);
208
                        $(row).hide();
209
                    });
210
                });
211
            });
212
        }
213
    });
214
215
})(jQuery);
216