New covers.
[redakcja.git] / src / redakcja / static / js / wiki / loader.js
1 if (!window.console) {
2     window.console = {
3         log: function(){
4         }
5     }
6 }
7
8 var DEFAULT_PERSPECTIVE = "#VisualPerspective";
9
10 $(function()
11 {
12     var tabs = $('ol#tabs li');
13     var gallery = null;
14     var MIN_SIDEBAR_WIDTH = 50,
15         DEFAULT_SIDEBAR_WIDTH = 480;
16
17         CurrentDocument = new $.wikiapi.WikiDocument("document-meta");
18
19         $.blockUI.defaults.baseZ = 10000;
20
21     function initialize()
22         {
23         var splitter = $('#splitter'),
24             vsplitbar = $('#vsplitbar'),
25             sidebar = $('#sidebar'),
26             dragLayer = $('#drag-layer'),
27             vsplitbarWidth = vsplitbar.outerWidth(),
28             isHolding = false;
29
30             function setSidebarWidth(x) {
31                 if (x < MIN_SIDEBAR_WIDTH) {
32                     x = 0;
33                     vsplitbar.removeClass('active');
34                 } else {
35                     vsplitbar.addClass('active');
36                 }
37                 $.wiki.state.perspectives.ScanGalleryPerspective.width = x;
38                 sidebar[0].style.width = x + 'px';
39             };
40
41
42                 /* The save button */
43         $('#save-button').click(function(event){
44             event.preventDefault();
45                         $.wiki.showDialog('#save_dialog');
46         });
47
48                 $('.editor').hide();
49
50                 /*
51                  * TABS
52                  */
53             $(document).on('click', '.tabs li', function(event, callback) {
54             event.preventDefault();
55                         $.wiki.switchToTab(this);
56         });
57
58             $(document).on('click', '#tabs li .tabclose', function(event, callback) {
59                 var $tab = $(this).parent().parent();
60
61                         if($('a', $tab).is('.active'))
62                                 $.wiki.switchToTab(DEFAULT_PERSPECTIVE);
63
64                         var p = $.wiki.perspectiveForTab($tab);
65                         p.destroy();
66
67                         return false;
68         });
69
70
71         $(window).resize(function(){
72             splitterWidth = splitter.width();
73         });
74
75         $(window).resize();
76             $.wiki.perspectiveForTab($('#tabs-right .active').parent()).onEnter();
77
78             vsplitbar.on('click', function() {
79                 var $this = $(this);
80                 if ($this.hasClass('active')) {
81                     $.wiki.state.perspectives.ScanGalleryPerspective.lastWidth = sidebar.width();
82                     setSidebarWidth(0);
83                 } else {
84                     setSidebarWidth($.wiki.state.perspectives.ScanGalleryPerspective.lastWidth);
85                 }
86             });
87
88
89         /* Splitbar dragging support */
90         vsplitbar
91             .mousedown(function(e) {
92                 e.preventDefault();
93                 isHolding = true;
94                 if (sidebar.width() > MIN_SIDEBAR_WIDTH) {
95                     $.wiki.state.perspectives.ScanGalleryPerspective.lastWidth = sidebar.width();
96                 }
97             })
98             .mousemove(function(e) {
99                 if(isHolding) {
100                     dragLayer.show(); // We don't show it up until now so that we don't lose single click events on vsplitbar
101                 }
102             });
103             dragLayer.mousemove(function(e) {
104                 setSidebarWidth(splitterWidth - e.clientX - vsplitbarWidth / 2);
105             });
106             $('body').mouseup(function(e) {
107                 dragLayer.hide();
108                 isHolding = false;
109             });
110
111             setSidebarWidth($.wiki.state.perspectives.ScanGalleryPerspective.width);
112
113             window.onbeforeunload = function(e) {
114             if($.wiki.isDirty()) {
115                                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
116                                 return "Na stronie mogą być nie zapisane zmiany.";
117                         };
118
119
120
121             };
122
123             $('body').mousemove(function(e) {
124                 CurrentDocument.active = true;
125             });
126             $('body').keydown(function(e) {
127                 CurrentDocument.active = true;
128             });
129
130
131                 console.log("Fetching document's text");
132
133                 $(document).bind('wlapi_document_changed', function(event, doc) {
134                         try {
135                                 $('#document-revision').text(doc.revision);
136                         } catch(e) {
137                                 console.log("Failed handler", e);
138                         }
139                 });
140
141                 CurrentDocument.fetch({
142                         success: function(){
143                                 console.log("Fetch success");
144                                 $('#loading-overlay').fadeOut();
145                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
146
147                                 if(active_tab == "#ScanGalleryPerspective")
148                                         active_tab = DEFAULT_PERSPECTIVE;
149
150                                 console.log("Initial tab is:", active_tab)
151                                 $.wiki.switchToTab(active_tab);
152
153                 /* every 5 minutes check for a newer version */
154                 var revTimer = setInterval(function() {
155                         CurrentDocument.checkRevision({outdated: function(){
156                             $('#header').addClass('out-of-date');
157                             clearInterval(revTimer);
158                         }});
159                     }, 60 * 1000);
160                         },
161                         failure: function() {
162                                 $('#loading-overlay').fadeOut();
163                                 alert("FAILURE");
164                         }
165                 });
166     }; /* end of initialize() */
167
168
169         /* Load configuration */
170         $.wiki.loadConfig();
171
172         var initAll = function(a, f) {
173                 if (a.length == 0) return f();
174
175                 $.wiki.initTab({
176                         tab: a.pop(),
177                         doc: CurrentDocument,
178                         callback: function(){
179                                 initAll(a, f);
180                         }
181                 });
182         };
183
184
185         /*
186          * Initialize all perspectives
187          */
188         initAll( $.makeArray($('.tabs li')), initialize);
189         console.log(location.hash);
190 });