Completed
Pull Request — develop (#179)
by Wachter
14:46
created

(ꞌanimationend webkitAnimationEnd oanimationend MSAnimationEndꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 1
cc 1
nc 1
nop 0
1
/*
2
 * This file is part of Sulu.
3
 *
4
 * (c) MASSIVE ART WebServices GmbH
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
define(['jquery', 'text!./grid.html'], function($, gridTemplate) {
11
12
    'use strict';
13
14
    var compare = function(id, $a, $b) {
15
            var aValue = parseInt($a.find('input').val()),
16
                bValue = parseInt($b.find('input').val()),
17
                aId = $a.data('id'),
18
                bId = $b.data('id');
19
20
            if (aValue < bValue) {
21
                return -1;
22
            } else if (aValue > bValue || aId === id || bId === id) {
23
                return 1;
24
            }
25
26
            return 0;
27
        },
28
29
        highlight = function($item) {
30
            $item.addClass('highlight-animation');
31
            $item.one('animationend webkitAnimationEnd oanimationend MSAnimationEnd', function() {
32
                $item.removeClass('highlight-animation');
33
            });
34
        },
35
36
        focus = function($item) {
37
            $item.find('input').focus();
38
        };
39
40
    return {
41
42
        defaults: {
43
            options: {
44
                pages: [],
45
                updateCallback: function(pages) {
0 ignored issues
show
Unused Code introduced by
The parameter pages is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
46
                }
47
            },
48
            templates: {
49
                grid: gridTemplate
50
            },
51
            translations: {
52
                title: 'public.title'
53
            }
54
        },
55
56
        initialize: function() {
57
            this.render();
58
59
            this.bindDomEvents();
60
        },
61
62
        render: function() {
63
            this.$el.append(this.templates.grid({translations: this.translations, pages: this.options.pages}));
64
        },
65
66
        bindDomEvents: function() {
67
            this.$el.find('input').on('change', function(e) {
68
                this.orderTable($(e.currentTarget).parents('tr'));
69
            }.bind(this));
70
        },
71
72
        orderTable: function($item) {
73
            var id = $item.data('id'),
74
                rows = this.$el.find('tbody tr').get(),
75
                pages = [];
76
77
            rows.sort(function(a, b) {
78
                return compare(id, $(a), $(b));
79
            });
80
81
            $.each(rows, function(index, row) {
82
                $(row).find('input').val(index + 1);
83
                pages.push($(row).data('id'));
84
85
                this.$el.find('table').children('tbody').append(row);
86
            }.bind(this));
87
88
            highlight($item);
89
            focus($item);
90
91
            this.options.updateCallback(pages);
92
        }
93
    }
94
});
95