Preliminary catalogue.
[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                 console.log("Fetching document's text");
121
122                 $(document).bind('wlapi_document_changed', function(event, doc) {
123                         try {
124                                 $('#document-revision').text(doc.revision);
125                         } catch(e) {
126                                 console.log("Failed handler", e);
127                         }
128                 });
129
130                 CurrentDocument.fetch({
131                         success: function(){
132                                 console.log("Fetch success");
133                                 $('#loading-overlay').fadeOut();
134                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
135
136                                 if(active_tab == "#ScanGalleryPerspective")
137                                         active_tab = DEFAULT_PERSPECTIVE;
138
139                                 console.log("Initial tab is:", active_tab)
140                                 $.wiki.switchToTab(active_tab);
141
142                 /* every 5 minutes check for a newer version */
143                 var revTimer = setInterval(function() {
144                         CurrentDocument.checkRevision({outdated: function(){
145                             $('#header').addClass('out-of-date');
146                             clearInterval(revTimer);
147                         }});
148                     }, 300000);
149                         },
150                         failure: function() {
151                                 $('#loading-overlay').fadeOut();
152                                 alert("FAILURE");
153                         }
154                 });
155     }; /* end of initialize() */
156
157
158         /* Load configuration */
159         $.wiki.loadConfig();
160
161         var initAll = function(a, f) {
162                 if (a.length == 0) return f();
163
164                 $.wiki.initTab({
165                         tab: a.pop(),
166                         doc: CurrentDocument,
167                         callback: function(){
168                                 initAll(a, f);
169                         }
170                 });
171         };
172
173
174         /*
175          * Initialize all perspectives
176          */
177         initAll( $.makeArray($('.tabs li')), initialize);
178         console.log(location.hash);
179 });