menu for mas editing stage and user
[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 kp = key.split('_');
23             var field = kp[0];
24             var idx = parseInt(kp[1]);
25
26             var fds = {}
27             fds.stage = "";
28             fds.user = "";
29             fds.status = "";
30             fds[field] = $("select[name="+field+"] option[value!=]").eq(idx).val();
31             $("#chunk_mass_edit [name=ids]").val(get_ids());
32             for (var fn in fds) {
33                 $("#chunk_mass_edit [name="+fn+"]").val(fds[fn]);
34             }
35
36             $.post($("#chunk_mass_edit").attr("action"),
37                    $("#chunk_mass_edit").serialize(),
38                    function(data, status) {
39                        location.reload(true);
40                    }
41                   );
42             return true;
43
44             
45         };
46
47         var get_items = function(field) {
48             var d = {};
49             $.each($("select[name="+field+"] option[value!=]"),
50                          function(idx, ele) {
51                              d[field+"_"+idx] = { name: $(ele).text() };
52                          });
53             return d;
54         };
55
56
57         $.contextMenu({
58             selector: '#file-list',
59             items: {
60                 "stage": { 
61                     name: "Set stage",
62                     items: get_items("stage"),
63                 },
64                 "user": { 
65                     name: "Set user",
66                     items: get_items("user"),
67                 },
68                 "status": {
69                     name: "set status",
70                     items: get_items("status"),
71                 },
72             },
73             callback: set_field,
74         });
75         
76     });
77 })(jQuery);