Merge branch 'master' into production
[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').css('right', 480).addClass('active');
63                                 $('#editor .editor').css('right', 510);
64                                 $(window).resize();
65                                 $.wiki.perspectiveForTab('#tabs-right .active').onEnter();
66                         },
67                         function() {
68                                 $.wiki.state.perspectives.ScanGalleryPerspective.show = false;
69                                 $('#sidebar').hide();
70                                 $('.vsplitbar').css('right', 0).removeClass('active');
71                                 $('#editor .editor').css('right', 30);
72                                 $(window).resize();
73                                 $.wiki.perspectiveForTab('#tabs-right .active').onExit();
74                         }
75                 );
76
77                 if($.wiki.state.perspectives.ScanGalleryPerspective.show)
78                         $('.vsplitbar').trigger('click');
79
80         window.onbeforeunload = function(e) {
81             if($.wiki.isDirty()) {
82                                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
83                                 return "Na stronie mogą być nie zapisane zmiany.";
84                         };
85         };
86
87                 console.log("Fetching document's text");
88
89                 $(document).bind('wlapi_document_changed', function(event, doc) {
90                         try {
91                                 $('#document-revision').text(doc.revision);
92                         } catch(e) {
93                                 console.log("Failed handler", e);
94                         }
95                 });
96
97                 CurrentDocument.fetch({
98                         success: function(){
99                                 console.log("Fetch success");
100                                 $('#loading-overlay').fadeOut();
101                                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
102
103                                 if(active_tab == "#ScanGalleryPerspective")
104                                         active_tab = DEFAULT_PERSPECTIVE;
105
106                                 console.log("Initial tab is:", active_tab)
107                                 $.wiki.switchToTab(active_tab);
108                         },
109                         failure: function() {
110                                 $('#loading-overlay').fadeOut();
111                                 alert("FAILURE");
112                         }
113                 });
114     }; /* end of initialize() */
115
116
117         /* Load configuration */
118         $.wiki.loadConfig();
119
120         var initAll = function(a, f) {
121                 if (a.length == 0) return f();
122
123                 $.wiki.initTab({
124                         tab: a.pop(),
125                         doc: CurrentDocument,
126                         callback: function(){
127                                 initAll(a, f);
128                         }
129                 });
130         };
131
132
133         /*
134          * Initialize all perspectives
135          */
136         initAll( $.makeArray($('.tabs li')), initialize);
137         console.log(location.hash);
138 });