icons for context menu
[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                 fds[field] = $("select[name="+field+"] option[value!=]").eq(idx).val();
35             }
36             /* fill in the form */
37             $("#chunk_mass_edit [name=ids]").val(get_ids());
38             for (var fn in fds) {
39                 $("#chunk_mass_edit [name="+fn+"]").val(fds[fn]);
40             }
41
42             $.post($("#chunk_mass_edit").attr("action"),
43                    $("#chunk_mass_edit").serialize(),
44                    function(data, status) {
45                        location.reload(true);
46                    }
47                   );
48             return true;
49
50             
51         };
52
53         var get_items = function(field) {
54             var d = {};
55             $.each($("select[name="+field+"] option[value!=]"),
56                          function(idx, ele) {
57                              d[field+"_"+idx] = { name: $(ele).text() };
58                          });
59             return d;
60         };
61
62
63         $.contextMenu({
64             selector: '#file-list',
65             items: {
66                 "stage": { 
67                     name: "Set stage",
68                     items: get_items("stage"),
69                     icon: "clock",
70                 },
71                 "user": { 
72                     name: "Set user",
73                     items: get_items("user"),
74                     icon: "user",
75                 },
76                 "publish": {
77                     name: "Mark publishable",
78                     icon: "ok",
79                 },
80                 "unpublish": {
81                     name: "Mark not publishable",
82                     icon: "stop",
83                 },
84             },
85             callback: set_field,
86         });
87         
88     });
89 })(jQuery);