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