ptrad
[redakcja.git] / src / 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     var tabs = $('ol#tabs li');
12     var gallery = null;
13     var MIN_SIDEBAR_WIDTH = 50,
14         DEFAULT_SIDEBAR_WIDTH = 480;
15
16     CurrentDocument = new $.wikiapi.WikiDocument("document-meta");
17
18     $.blockUI.defaults.baseZ = 10000;
19
20     function initialize() {
21         var splitter = $('#splitter'),
22             vsplitbar = $('#vsplitbar'),
23             sidebar = $('#sidebar'),
24             dragLayer = $('#drag-layer'),
25             vsplitbarWidth = vsplitbar.outerWidth(),
26             isHolding = false;
27
28         function setSidebarWidth(x) {
29             if (x < MIN_SIDEBAR_WIDTH) {
30                 x = 0;
31                 vsplitbar.removeClass('active');
32             } else {
33                 vsplitbar.addClass('active');
34             }
35             $.wiki.state.perspectives.ScanGalleryPerspective.width = x;
36             sidebar[0].style.width = x + 'px';
37         };
38
39         /* The save button */
40         $('#save-button').click(function(event){
41             event.preventDefault();
42             $.wiki.showDialog('#save_dialog');
43         });
44
45         $('.editor').hide();
46
47         /*
48          * TABS
49          */
50         $(document).on('click', '.tabs li', function(event, callback) {
51             event.preventDefault();
52             $.wiki.switchToTab(this);
53         });
54
55         $(document).on('click', '#tabs li .tabclose', function(event, callback) {
56             var $tab = $(this).parent().parent();
57
58             if($('a', $tab).is('.active'))
59                 $.wiki.switchToTab(DEFAULT_PERSPECTIVE);
60
61             var p = $.wiki.perspectiveForTab($tab);
62             p.destroy();
63
64             return false;
65         });
66
67         $(window).resize(function(){
68             splitterWidth = splitter.width();
69         });
70
71         $(window).resize();
72         $.wiki.perspectiveForTab($('#tabs-right .active').parent()).onEnter();
73
74         vsplitbar.on('click', function() {
75             var $this = $(this);
76             if ($this.hasClass('active')) {
77                 $.wiki.state.perspectives.ScanGalleryPerspective.lastWidth = sidebar.width();
78                 setSidebarWidth(0);
79             } else {
80                 setSidebarWidth($.wiki.state.perspectives.ScanGalleryPerspective.lastWidth);
81             }
82         });
83
84         /* Splitbar dragging support */
85         vsplitbar
86             .mousedown(function(e) {
87                 e.preventDefault();
88                 isHolding = true;
89                 if (sidebar.width() > MIN_SIDEBAR_WIDTH) {
90                     $.wiki.state.perspectives.ScanGalleryPerspective.lastWidth = sidebar.width();
91                 }
92             })
93             .mousemove(function(e) {
94                 if(isHolding) {
95                     dragLayer.show(); // We don't show it up until now so that we don't lose single click events on vsplitbar
96                 }
97             });
98         dragLayer.mousemove(function(e) {
99             setSidebarWidth(splitterWidth - e.clientX - vsplitbarWidth / 2);
100         });
101         $('body').mouseup(function(e) {
102             dragLayer.hide();
103             isHolding = false;
104         });
105
106         setSidebarWidth($.wiki.state.perspectives.ScanGalleryPerspective.width);
107
108         window.onbeforeunload = function(e) {
109             if($.wiki.isDirty()) {
110                 e.returnValue = "Na stronie mogą być nie zapisane zmiany.";
111                 return "Na stronie mogą być nie zapisane zmiany.";
112             };
113         };
114
115         $('body').mousemove(function(e) {
116             CurrentDocument.active = new Date();
117         });
118         $('body').keydown(function(e) {
119             CurrentDocument.active = new Date();
120         });
121
122         console.log("Fetching document's text");
123
124         $(document).bind('wlapi_document_changed', function(event, doc) {
125             try {
126                 $('#document-revision').text(doc.revision);
127             } catch(e) {
128                 console.log("Failed handler", e);
129             }
130         });
131
132         CurrentDocument.fetch({
133             success: function(){
134                 console.log("Fetch success");
135                 $('#loading-overlay').fadeOut();
136                 var active_tab = document.location.hash || DEFAULT_PERSPECTIVE;
137
138                 if(active_tab == "#ScanGalleryPerspective")
139                     active_tab = DEFAULT_PERSPECTIVE;
140
141                 console.log("Initial tab is:", active_tab)
142                 $.wiki.switchToTab(active_tab);
143
144                 /* check for a newer version */
145                 CurrentDocument.checkRevision({outdated: function(){
146                     $('#header').addClass('out-of-date');
147                 }});
148                 var revTimer = setInterval(function() {
149                     CurrentDocument.checkRevision({outdated: function(){
150                         $('#header').addClass('out-of-date');
151                     }});
152                 }, 5 * 1000);
153             },
154             failure: function() {
155                 $('#loading-overlay').fadeOut();
156                 alert("FAILURE");
157             }
158         });
159     }; /* end of initialize() */
160
161
162     /* Load configuration */
163     $.wiki.loadConfig();
164
165     /*
166      * Initialize all perspectives
167      */
168     $('.tabs li').each((i, e) => {
169         $.wiki.initTab({tab: e, doc: CurrentDocument});
170     });
171     initialize();
172 });