librarian update
[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                 $(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             event.preventDefault();
37                         $.wiki.switchToTab(this);
38         });
39
40                 $('#tabs li > .tabclose').live('click', function(event, callback) {
41                         var $tab = $(this).parent();
42
43                         if($tab.is('.active'))
44                                 $.wiki.switchToTab(DEFAULT_PERSPECTIVE);
45
46                         var p = $.wiki.perspectiveForTab($tab);
47                         p.destroy();
48
49                         return false;
50         });
51
52
53         $(window).resize(function(){
54             $('iframe').height($(window).height() - $('#tabs').outerHeight() - $('#source-editor .toolbar').outerHeight());
55         });
56
57         $(window).resize();
58
59         $('.vsplitbar').toggle(
60                         function() {
61                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = true;
62                                 $('#sidebar').show();
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                             var active_right = $.wiki.perspectiveForTab('#tabs-right .active');
70                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = false;
71                                 $('#sidebar').hide();
72                                 $('.vsplitbar').css('right', 0).removeClass('active');
73                                 $(".vsplitbar-title").html("↑ " + active_right.vsplitbar + " ↑");
74                                 $('#editor .editor').css('right', 30);
75                                 $(window).resize();
76                                 active_right.onExit();
77                         }
78                 );
79
80                 if($.wiki.state.perspectives.ScanGalleryPerspective.show){
81             $('.vsplitbar').trigger('click');
82             $(".vsplitbar-title").html("↓ GALERIA ↓");
83         } else {
84             $(".vsplitbar-title").html("↑ GALERIA ↑");
85         }
86         window.onbeforeunload = function(e) {
87             if($.wiki.isDirty()) {
88                                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
89                                 return "Na stronie mogą być nie zapisane zmiany.";
90                         };
91         };
92
93                 console.log("Fetching document's text");
94
95                 $(document).bind('wlapi_document_changed', function(event, doc) {
96                         try {
97                                 $('#document-revision').text(doc.revision);
98                         } catch(e) {
99                                 console.log("Failed handler", e);
100                         }
101                 });
102
103                 CurrentDocument.fetch({
104                         success: function(){
105                                 console.log("Fetch success");
106                                 $('#loading-overlay').fadeOut();
107                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
108
109                                 if(active_tab == "#ScanGalleryPerspective")
110                                         active_tab = DEFAULT_PERSPECTIVE;
111
112                                 console.log("Initial tab is:", active_tab)
113                                 $.wiki.switchToTab(active_tab);
114
115                 /* every 5 minutes check for a newer version */
116                 var revTimer = setInterval(function() {
117                         CurrentDocument.checkRevision({outdated: function(){
118                             $('#header').addClass('out-of-date');
119                             clearInterval(revTimer);
120                         }});
121                     }, 300000);
122                         },
123                         failure: function() {
124                                 $('#loading-overlay').fadeOut();
125                                 alert("FAILURE");
126                         }
127                 });
128     }; /* end of initialize() */
129
130
131         /* Load configuration */
132         $.wiki.loadConfig();
133
134         var initAll = function(a, f) {
135                 if (a.length == 0) return f();
136
137                 $.wiki.initTab({
138                         tab: a.pop(),
139                         doc: CurrentDocument,
140                         callback: function(){
141                                 initAll(a, f);
142                         }
143                 });
144         };
145
146
147         /*
148          * Initialize all perspectives
149          */
150         initAll( $.makeArray($('.tabs li')), initialize);
151         console.log(location.hash);
152 });