Splitbar can now be dragged to an arbitrary position
[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                         $.wiki.switchToTab(this);
54         });
55
56                 $('#tabs li > .tabclose').live('click', function(event, callback) {
57                         var $tab = $(this).parent();
58
59                         if($tab.is('.active'))
60                                 $.wiki.switchToTab(DEFAULT_PERSPECTIVE);
61
62                         var p = $.wiki.perspectiveForTab($tab);
63                         p.destroy();
64
65                         return false;
66         });
67
68
69         $(window).resize(function(){
70             $('iframe').height($(window).height() - $('#tabs').outerHeight() - $('#source-editor .toolbar').outerHeight());
71             splitterWidth = splitter.width();
72         });
73
74         $(window).resize();
75
76         vsplitbar.toggle(
77                         function() {
78                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = true;
79                                 setSplitbarAt(splitterWidth - (480 + vsplitbarWidth));
80                                 $('.vsplitbar').addClass('active');
81                                 $(window).resize();
82                                 $.wiki.perspectiveForTab('#tabs-right .active').onEnter();
83                         },
84                         function() {
85                             var active_right = $.wiki.perspectiveForTab('#tabs-right .active');
86                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = false;
87                                 $(".vsplitbar-title").html("↑ " + active_right.vsplitbar + " ↑");
88                                 setSplitbarAt(splitterWidth - vsplitbarWidth);
89                                 $('.vsplitbar').removeClass('active');
90                                 $(window).resize();
91                                 active_right.onExit();
92                         }
93                 );
94
95
96         /* Splitbar dragging support */
97         vsplitbar
98             .mousedown(function(e) {
99                 e.preventDefault();
100                 isHolding = true;
101             })
102             .mousemove(function(e) {
103                 if(isHolding) {
104                     dragLayer.show(); // We don't show it up until now so that we don't lose single click events on vsplitbar
105                 }
106             });
107         dragLayer.mousemove(function(e) {
108             setSplitbarAt(e.clientX - vsplitbarWidth/2);
109         });
110         $('body').mouseup(function(e) {
111             dragLayer.hide();
112             isHolding = false;
113         });
114
115
116                 if($.wiki.state.perspectives.ScanGalleryPerspective.show){
117             $('.vsplitbar').trigger('click');
118             $(".vsplitbar-title").html("↓ GALERIA ↓");
119         } else {
120             $(".vsplitbar-title").html("↑ GALERIA ↑");
121         }
122         window.onbeforeunload = function(e) {
123             if($.wiki.isDirty()) {
124                                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
125                                 return "Na stronie mogą być nie zapisane zmiany.";
126                         };
127         };
128
129                 console.log("Fetching document's text");
130
131                 $(document).bind('wlapi_document_changed', function(event, doc) {
132                         try {
133                                 $('#document-revision').text(doc.revision);
134                         } catch(e) {
135                                 console.log("Failed handler", e);
136                         }
137                 });
138
139                 CurrentDocument.fetch({
140                         success: function(){
141                                 console.log("Fetch success");
142                                 $('#loading-overlay').fadeOut();
143                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
144
145                                 if(active_tab == "#ScanGalleryPerspective")
146                                         active_tab = DEFAULT_PERSPECTIVE;
147
148                                 console.log("Initial tab is:", active_tab)
149                                 $.wiki.switchToTab(active_tab);
150
151                 /* every 5 minutes check for a newer version */
152                 var revTimer = setInterval(function() {
153                         CurrentDocument.checkRevision({outdated: function(){
154                             $('#header').addClass('out-of-date');
155                             clearInterval(revTimer);
156                         }});
157                     }, 300000);
158                         },
159                         failure: function() {
160                                 $('#loading-overlay').fadeOut();
161                                 alert("FAILURE");
162                         }
163                 });
164     }; /* end of initialize() */
165
166
167         /* Load configuration */
168         $.wiki.loadConfig();
169
170         var initAll = function(a, f) {
171                 if (a.length == 0) return f();
172
173                 $.wiki.initTab({
174                         tab: a.pop(),
175                         doc: CurrentDocument,
176                         callback: function(){
177                                 initAll(a, f);
178                         }
179                 });
180         };
181
182
183         /*
184          * Initialize all perspectives
185          */
186         initAll( $.makeArray($('.tabs li')), initialize);
187         console.log(location.hash);
188 });