Fixes #4189: theme box fails when entering second theme.
[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("user", user_callback),
54         {sep: '----'},
55         get_items("other-user", user_callback)
56     ];
57     var current_user_items = user_items = {};
58     var i = 0;
59     var more_label = $("label[for=mass_edit_more_users]").text();
60     for (user_table in users) {
61         for (user in users[user_table]) {
62             if (i && i % 20 == 0) {
63                 var more_items = {};
64                 current_user_items['more'] = {
65                     name: more_label,
66                     items: more_items
67                 };
68                 current_user_items = more_items;
69             }
70             current_user_items[user] = users[user_table][user];
71             i += 1;
72         }
73     }
74     $.contextMenu({
75         selector: '#file-list',
76         items: {
77             stage: { 
78                 name: $("label[for=mass_edit_stage]").text(),
79                 items: get_items("stage", get_callback('stage')),
80                 icon: "clock",
81             },
82             user: { 
83                 name: $("label[for=mass_edit_user]").text(),
84                 items: user_items,
85                 icon: "user",
86             },
87             project: {
88                 name: $("label[for=mass_edit_project]").text(),
89                 items: get_items("project", get_callback('project')),
90             },
91         },
92     });
93
94
95     });
96 })(jQuery);