Merge branch 'mass_edit'
[redakcja.git] / redakcja / static / js / catalogue / book_list.js
1 (function($) {
2     $(function() {
3         // clicking on book checks chunks, too
4         $("input[name=select_book]").change(function(ev) {
5             $book = $(this);
6             $book.closest("table").find("input[name=select_chunk][data-book-id=" + $book.val() + "]").attr("checked", $book.is(':checked'));
7             });
8
9         // initialize context menu
10
11         var get_ids = function() {
12             return $.map($("input[name=select_chunk]:checked"), function(ele, idx) {
13                 return ele.value;
14                 }).concat(
15                     $.map($("input[name=select_book][data-chunk-id!=]:checked"), function(ele, idx) {
16                         return $(ele).attr("data-chunk-id");
17                         })).join();
18         };
19     
20
21         var set_field = function(key, ops) {
22             var fds = {}
23             fds.stage = "";
24             fds.user = "";
25             fds.status = "";
26
27             if (key == "publish" || key == "unpublish") {
28                 fds["status"] = key;
29             } else {
30                 var kp = key.split('_');
31                 var field = kp[0];
32                 var idx = parseInt(kp[1]);
33
34                 var target_field = field;
35                 if (field == 'other-user')
36                     target_field = 'user';
37
38                 fds[target_field] = $("select[name="+field+"] option[value!=]").eq(idx).val();
39             }
40             /* fill in the form */
41             $("#chunk_mass_edit [name=ids]").val(get_ids());
42             for (var fn in fds) {
43                 $("#chunk_mass_edit [name="+fn+"]").val(fds[fn]);
44             }
45
46             $.post($("#chunk_mass_edit").attr("action"),
47                    $("#chunk_mass_edit").serialize(),
48                    function(data, status) {
49                        location.reload(true);
50                    }
51                   );
52             return true;
53
54             
55         };
56
57         var get_items = function(field) {
58             var d = {};
59             $.each($("select[name="+field+"] option[value!=]"),
60                          function(idx, ele) {
61                              d[field+"_"+idx] = { name: $(ele).text() };
62                          });
63             return d;
64         };
65
66
67         $.contextMenu({
68             selector: '#file-list',
69             items: {
70                 "stage": { 
71                     name: $("label[for=mass_edit_stage]").text(),
72                     items: get_items("stage"),
73                     icon: "clock",
74                 },
75                 "user": { 
76                     name: $("label[for=mass_edit_user]").text(),
77                     items: (function() {
78                         var active_users = get_items("user");
79                         active_users['other'] = {
80                             name: $("label[for=mass_edit_other]").text(),
81                             items: get_items("other-user"),
82                         };
83                         return active_users;
84                         })(),
85                     icon: "user",
86                 },
87                 "publish": {
88                     name:  $("label[for=mass_edit_publish]").text(),
89                     icon: "ok",
90                 },
91                 "unpublish": {
92                     name:  $("label[for=mass_edit_unpublish]").text(),
93                     icon: "stop",
94                 },
95             },
96             callback: set_field,
97         });
98         
99     });
100 })(jQuery);