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