pretty tag fields in new resource form
[redakcja.git] / redakcja / static / js / catalogue / catalogue.js
1 (function($) {
2     $(function() {
3
4
5         $('.filter').change(function() {
6             document.filter[this.name].value = this.value;
7             document.filter.submit();
8         });
9
10         $('.check-filter').change(function() {
11             document.filter[this.name].value = this.checked ? '1' : '';
12             document.filter.submit();
13         });
14
15         $('.text-filter').each(function() {
16             var inp = this;
17             $(inp).parent().submit(function() {
18                 document.filter[inp.name].value = inp.value;
19                 document.filter.submit();
20                 return false;
21             });
22         });
23
24
25         $('.autoslug-source').change(function() {
26             $('.autoslug').attr('value', slugify(this.value));
27         });
28
29
30         var nowTemp = new Date();
31         var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
32
33         $('.datepicker-field').each(function() {
34             var checkout = $(this).datepicker({
35                 format: 'yyyy-mm-dd',
36                 weekStart: 1,
37                 onRender: function(date) {
38                     return date.valueOf() < now.valueOf() ? 'disabled' : '';
39                 }
40             }).on('changeDate', function() {
41                 checkout.hide();
42             }).data('datepicker');
43         });
44
45
46         $("select").change(function() {
47             var helpdiv = $(this).next();
48             if (helpdiv.hasClass('help-text')) {
49                 var helptext = $("option:selected", this).attr('data-help');
50                 helpdiv.html(helptext || '');
51             }
52         });
53
54         $('.chosen-select').chosen();
55
56
57
58         // tutorial mode
59         var tutorial, tutorial_no;
60         var start;
61
62         var first_reset = true;
63         function tutreset() {
64             if (start) $(start).popover('hide');
65             start = null;
66             tutorial_no = null;
67             var all_tutorial = $('[data-toggle="tutorial"]');
68
69             function sortKey(a) {
70                 return parseInt($(a).attr('data-tutorial'));
71             }
72             tutorial = $.makeArray(all_tutorial.sort(
73                 function(a, b) {return sortKey(a) < sortKey(b) ? -1 : 1}
74             ));
75
76             if (first_reset) {
77                 $.each(tutorial, function(i, e) {
78                     var but = (i < tutorial.length - 1) ? '>>' : 'OK',
79                         but_prev_html = i === 0 ? '' : '<a class="btn btn-default tutorial-prev" href="#-" id="pv'+i+'">&lt;&lt;</a></div></div>';
80                     $(e).popover({
81                         title: '<a class="btn btn-default tutorial-off" href="#-" id="tutoff'+i+'" style="float:right; padding:0 8px 4px 8px; position:relative; top:-6px; right:-10px;">&times;</a>Tutorial',
82                         trigger: 'focus',
83                         html: 'true',
84                         template: '<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div><div><a style="float:right" class="btn btn-default tutorial-next" href="#-" id="nt'+i+'">' + but + '</a>' + but_prev_html + '</div></div>'
85                     });
86                     $(e).popover('disable');
87                 });
88                 first_reset = false;
89             }
90         }
91         
92         function tuton() {
93             sessionStorage.setItem("tutorial", "on");
94             tutreset();
95             var $tutModal = $('#tutModal');
96             if($tutModal.length === 0) {
97                 tutnext();
98             } else {
99                 $tutModal.modal('show');
100             }
101             return false;
102         }
103         function tutoff() {
104             $(this).popover('hide');
105             if (start) $(start).popover('hide');
106             start = null;
107             sessionStorage.removeItem("tutorial");
108             $('[data-toggle="tutorial"]').popover('disable');
109             return false;
110         }
111         function tut(next) {
112             if (start) {
113                 $(start).popover('hide').popover('disable');
114             }
115             if (tutorial_no === null)
116                 tutorial_no = 0;
117             else if (next)
118                 tutorial_no++;
119             else
120                 tutorial_no--;
121             if (tutorial_no < tutorial.length && tutorial_no >= 0) {
122                 start = tutorial[tutorial_no];
123                 $(start).popover('enable').popover('show');
124             }
125             else {
126                 tutorial_no = null;
127                 start = null;
128             }
129             return false;
130         }
131         function tutnext() {
132             tut(true);
133         }
134         function tutprev() {
135             tut(false);
136         }
137         $('#tutModal').on('hidden.bs.modal', tutnext);
138
139         if (sessionStorage.getItem("tutorial") === "on" && $('#tuton').length === 0) {
140             tutreset();
141             tutnext();
142         }
143         $(document).on('click', '#tuton', tuton);
144         $(document).on('click', '.tutorial-off', tutoff);
145         $(document).on('click', '.tutorial-next', tutnext);
146         $(document).on('click', '.tutorial-prev', tutprev);
147     });
148 })(jQuery);
149