add prev button in tutorial mode + cleanup
[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
55
56         // tutorial mode
57         var tutorial, tutorial_no;
58         var start;
59
60         var first_reset = true;
61         function tutreset() {
62             if (start) $(start).popover('hide');
63             start = null;
64             tutorial_no = null;
65             var all_tutorial = $('[data-toggle="tutorial"]');
66
67             function sortKey(a) {
68                 return parseInt($(a).attr('data-tutorial'));
69             }
70             tutorial = $.makeArray(all_tutorial.sort(
71                 function(a, b) {return sortKey(a) < sortKey(b) ? -1 : 1}
72             ));
73
74             if (first_reset) {
75                 $.each(tutorial, function(i, e) {
76                     var but = (i < tutorial.length - 1) ? '>>' : 'OK',
77                         but_prev_html = i === 0 ? '' : '<a class="btn btn-default tutorial-prev" href="#-" id="pv'+i+'">&lt;&lt;</a></div></div>';
78                     $(e).popover({
79                         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',
80                         trigger: 'focus',
81                         html: 'true',
82                         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>'
83                     });
84                     $(e).popover('disable');
85                 });
86                 first_reset = false;
87             }
88         }
89         
90         function tuton() {
91             sessionStorage.setItem("tutorial", "on");
92             tutreset();
93             var $tutModal = $('#tutModal');
94             if($tutModal.length === 0) {
95                 tutnext();
96             } else {
97                 $tutModal.modal('show');
98             }
99             return false;
100         }
101         function tutoff() {
102             $(this).popover('hide');
103             if (start) $(start).popover('hide');
104             start = null;
105             sessionStorage.removeItem("tutorial");
106             $('[data-toggle="tutorial"]').popover('disable');
107             return false;
108         }
109         function tut(next) {
110             if (start) {
111                 $(start).popover('hide').popover('disable');
112             }
113             if (tutorial_no === null)
114                 tutorial_no = 0;
115             else if (next)
116                 tutorial_no++;
117             else
118                 tutorial_no--;
119             if (tutorial_no < tutorial.length && tutorial_no >= 0) {
120                 start = tutorial[tutorial_no];
121                 $(start).popover('enable').popover('show');
122             }
123             else {
124                 tutorial_no = null;
125                 start = null;
126             }
127             return false;
128         }
129         function tutnext() {
130             tut(true);
131         }
132         function tutprev() {
133             tut(false);
134         }
135         $('#tutModal').on('hidden.bs.modal', tutnext);
136
137         if (sessionStorage.getItem("tutorial") === "on" && $('#tuton').length === 0) {
138             tutreset();
139             tutnext();
140         }
141         $(document).on('click', '#tuton', tuton);
142         $(document).on('click', '.tutorial-off', tutoff);
143         $(document).on('click', '.tutorial-next', tutnext);
144         $(document).on('click', '.tutorial-prev', tutprev);
145     });
146 })(jQuery);
147