eb535a165dce5a097c38fe898b3b76693407e7c0
[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 = "#SummaryPerspective";
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                 $(document).keydown(function(event) {
21                         console.log("Received key:", event);
22                 });
23
24                 /* The save button */
25         $('#save-button').click(function(event){
26             event.preventDefault();
27                         $.wiki.showDialog('#save_dialog');
28         });
29
30                 $('.editor').hide();
31
32                 /*
33                  * TABS
34                  */
35         $('.tabs li').live('click', function(event, callback) {
36                         $.wiki.switchToTab(this);
37         });
38
39                 $('#tabs li > .tabclose').live('click', function(event, callback) {
40                         var $tab = $(this).parent();
41
42                         if($tab.is('.active'))
43                                 $.wiki.switchToTab(DEFAULT_PERSPECTIVE);
44
45                         var p = $.wiki.perspectiveForTab($tab);
46                         p.destroy();
47
48                         return false;
49         });
50
51
52         $(window).resize(function(){
53             $('iframe').height($(window).height() - $('#tabs').outerHeight() - $('#source-editor .toolbar').outerHeight());
54         });
55
56         $(window).resize();
57
58         $('.vsplitbar').toggle(
59                         function() {
60                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = true;
61                                 $('#sidebar').show();
62                                 $(".vsplitbar-title").html("↓ GALLERY ↓");
63                                 $('.vsplitbar').css('right', 480).addClass('active');
64                                 $('#editor .editor').css('right', 510);
65                                 $(window).resize();
66                                 $.wiki.perspectiveForTab('#tabs-right .active').onEnter();
67                         },
68                         function() {
69                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = false;
70                                 $('#sidebar').hide();
71                                 $('.vsplitbar').css('right', 0).removeClass('active');
72                                 if($(".vsplitbar-title").html() == "↓ ZNAJDŹ I ZAMIEŃ ↓"){
73                                         $(".vsplitbar-title").html("↑ ZNAJDŹ I ZAMIEŃ ↑");        
74                                 } else if($(".vsplitbar-title").html() == "↓ PRZYPISY ↓"){
75                         $(".vsplitbar-title").html("↑ PRZYPISY ↑");        
76                 } else {
77                                     $(".vsplitbar-title").html("↑ GALERIA ↑");
78                                 }
79                                 $('#editor .editor').css('right', 30);
80                                 $(window).resize();
81                                 $.wiki.perspectiveForTab('#tabs-right .active').onExit();
82                         }
83                 );
84
85                 if($.wiki.state.perspectives.ScanGalleryPerspective.show){
86             $('.vsplitbar').trigger('click');
87             $(".vsplitbar-title").html("↓ GALLERY ↓");
88         } else {
89             $(".vsplitbar-title").html("↑ GALLERY ↑");
90         }
91         window.onbeforeunload = function(e) {
92             if($.wiki.isDirty()) {
93                                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
94                                 return "Na stronie mogą być nie zapisane zmiany.";
95                         };
96         };
97
98                 console.log("Fetching document's text");
99
100                 $(document).bind('wlapi_document_changed', function(event, doc) {
101                         try {
102                                 $('#document-revision').text(doc.revision);
103                         } catch(e) {
104                                 console.log("Failed handler", e);
105                         }
106                 });
107
108                 CurrentDocument.fetch({
109                         success: function(){
110                                 console.log("Fetch success");
111                                 $('#loading-overlay').fadeOut();
112                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
113
114                                 if(active_tab == "#ScanGalleryPerspective")
115                                         active_tab = DEFAULT_PERSPECTIVE;
116
117                                 console.log("Initial tab is:", active_tab)
118                                 $.wiki.switchToTab(active_tab);
119                         },
120                         failure: function() {
121                                 $('#loading-overlay').fadeOut();
122                                 alert("FAILURE");
123                         }
124                 });
125     }; /* end of initialize() */
126
127
128         /* Load configuration */
129         $.wiki.loadConfig();
130
131         var initAll = function(a, f) {
132                 if (a.length == 0) return f();
133
134                 $.wiki.initTab({
135                         tab: a.pop(),
136                         doc: CurrentDocument,
137                         callback: function(){
138                                 initAll(a, f);
139                         }
140                 });
141         };
142
143
144         /*
145          * Initialize all perspectives
146          */
147         initAll( $.makeArray($('.tabs li')), initialize);
148         console.log(location.hash);
149 });