Fixes #759: go back to history when closing diff
[redakcja.git] / redakcja / static / js / wiki / view_editor_source.js
1 /* COMMENT */
2 (function($) {
3
4         function CodeMirrorPerspective(options)
5         {
6                 var old_callback = options.callback;
7         options.callback = function(){
8                         var self = this;
9
10                         this.codemirror = CodeMirror.fromTextArea('codemirror_placeholder', {
11                                 parserfile: 'parsexml.js',
12                                 path: STATIC_URL + "js/lib/codemirror/",
13                                 stylesheet: STATIC_URL + "css/xmlcolors_15032010.css",
14                                 parserConfig: {
15                                         useHTMLKludges: false
16                                 },
17                                 iframeClass: 'xml-iframe',
18                                 textWrapping: true,
19                                 lineNumbers: true,
20                                 width: "100%",
21                                 tabMode: 'spaces',
22                                 indentUnit: 0,
23                                 readOnly: CurrentDocument.readonly || false,
24                                 initCallback: function(){
25
26                                         self.codemirror.grabKeys(function(event) {
27                                                 if (event.button) {
28                                                         $(event.button).trigger('click');
29                                                         event.button = null;
30                                                 }
31                                         }, function(event) {
32                                                 /* CM reports characters 2 times - as event and as code */
33                                                 if((typeof event) != "object")
34                                                         return false;
35
36                                                 if(!event.altKey)
37                                                         return false;
38
39                                                 var c = String.fromCharCode(event.keyCode).toLowerCase();
40                                                 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
41                                                 if(button.length == 0)
42                                                         return false;
43
44                                                 /* it doesn't matter which button we pick - all do the same */
45                                                 event.button = button[0];
46                                                 return true;
47                                         });
48
49                                         $('#source-editor .toolbar').toolbarize({
50                                             actionContext: self.codemirror
51                                         });
52
53                                         console.log("Initialized CodeMirror");
54
55                                         // textarea is no longer needed
56                                         $('codemirror_placeholder').remove();
57
58                                         old_callback.call(self);
59                                 }
60                         });
61                 };
62
63                 $.wiki.Perspective.call(this, options);
64         };
65
66
67         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
68
69         CodeMirrorPerspective.prototype.freezeState = function() {
70                 this.config().position = this.codemirror.win.scrollY || 0;
71         };
72
73         CodeMirrorPerspective.prototype.unfreezeState = function () {
74                 this.codemirror.win.scroll(0, this.config().position || 0);
75         };
76
77         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
78                 $.wiki.Perspective.prototype.onEnter.call(this);
79
80                 console.log('Entering', this.doc);
81                 this.codemirror.setCode(this.doc.text);
82
83                 /* fix line numbers bar */
84                 var $nums = $('.CodeMirror-line-numbers');
85             var barWidth = $nums.width();
86
87                 $(this.codemirror.frame.contentDocument.body).css('padding-left', barWidth);
88                 // $nums.css('left', -barWidth);
89
90                 $(window).resize();
91                 this.unfreezeState(this._uistate);
92
93                 if(success) success();
94         }
95
96         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
97                 this.freezeState();
98
99                 $.wiki.Perspective.prototype.onExit.call(this);
100                 console.log('Exiting', this.doc);
101                 this.doc.setText(this.codemirror.getCode());
102
103         if ($('.vsplitbar').hasClass('active') && $('#SearchPerspective').hasClass('active')) {
104             $.wiki.switchToTab('#ScanGalleryPerspective');
105         }
106
107                 if(success) success();
108         }
109
110         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
111
112 })(jQuery);