fix
[redakcja.git] / src / redakcja / static / js / documents / book_list.js
1 (function($) {
2     $(function() {
3
4     // clicking on book checks chunks, too
5     $("input[name=select_book]").change(function(ev) {
6         $book = $(this);
7         $book.closest("table").find("input[name=select_chunk][data-book-id=" + $book.val() + "]").attr("checked", $book.is(':checked'));
8     });
9
10     // initialize context menu
11
12    var get_ids = function() {
13        return $.map($("input[name=select_chunk]:checked"), function(ele, idx) {
14            return ele.value;
15            }).concat(
16                $.map($("input[name=select_book][data-chunk-id!=]:checked"), function(ele, idx) {
17                    return $(ele).attr("data-chunk-id");
18                    })).join();
19    };
20
21     var get_callback = function(form_field_name) {
22         var $form = $("#chunk_mass_edit");
23         var $field = $("[name=" + form_field_name + "]", $form);
24         var $ids_field = $("[name=ids]").val(get_ids());
25         var usable_callback = function(value) {
26             $field.val(value);
27             $ids_field.val(get_ids());
28             $.post($form.attr("action"),
29                $form.serialize(),
30                function(data, status) {
31                    location.reload(true);
32                }
33             );
34             return true;
35         };
36         return usable_callback;
37     };
38
39     var get_items = function(field, callback) {
40         var d = {};
41         $.each($("select[name="+field+"] option[value!=]"),
42             function(idx, ele) {
43                 d[field + "_" + idx] = {
44                     name: $(ele).text(), 
45                     callback: function() {callback($(ele).attr('value'));}
46                 };
47             });
48         return d;
49     };
50
51     var user_callback = get_callback('user');
52     var users = [
53         get_items("active-users", user_callback)
54     ];
55     var current_user_items = user_items = {};
56     var i = 0;
57     var more_label = $("label[for=mass_edit_more_users]").text();
58     for (user_table in users) {
59         for (user in users[user_table]) {
60             if (i && i % 20 == 0) {
61                 var more_items = {};
62                 current_user_items['more'] = {
63                     name: more_label,
64                     items: more_items
65                 };
66                 current_user_items = more_items;
67             }
68             current_user_items[user] = users[user_table][user];
69             i += 1;
70         }
71     }
72     $.contextMenu({
73         selector: '#file-list',
74         items: {
75             stage: { 
76                 name: $("label[for=mass_edit_stage]").text(),
77                 items: get_items("stage", get_callback('stage')),
78                 icon: "clock",
79             },
80             user: { 
81                 name: $("label[for=mass_edit_user]").text(),
82                 items: user_items,
83                 icon: "user",
84             },
85             project: {
86                 name: $("label[for=mass_edit_project]").text(),
87                 items: get_items("project", get_callback('project')),
88             },
89         },
90     });
91
92
93     });
94 })(jQuery);