Fixes #4191: backward search.
[redakcja.git] / src / redakcja / static / js / wiki / view_editor_source.js
1 (function($) {
2
3     function CodeMirrorPerspective(options) {
4         var old_callback = options.callback;
5         options.callback = function(){
6             var self = this;
7
8             this.codemirror = CodeMirror.fromTextArea($(
9                 '#codemirror_placeholder').get(0), {
10                     mode: 'xml',
11                     lineWrapping: true,
12                     lineNumbers: true,
13                     readOnly: CurrentDocument.readonly || false,
14                     identUnit: 0,
15                 });
16
17
18             $('#source-editor').keydown(function(event) {
19                 if(!event.altKey)
20                     return;
21
22                 var c = event.key;
23                 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
24                 if(button.length == 0)
25                     return;
26                 button.get(0).click();
27                 event.preventDefault();
28             });
29
30             $('#source-editor .toolbar').toolbarize({
31                 actionContext: self.codemirror
32             });
33
34             // textarea is no longer needed
35             $('#codemirror_placeholder').remove();
36             old_callback.call(self);
37         }
38
39         $.wiki.Perspective.call(this, options);
40     };
41
42
43     CodeMirrorPerspective.prototype = new $.wiki.Perspective();
44
45     CodeMirrorPerspective.prototype.freezeState = function() {
46         this.config().position =  this.codemirror.getScrollInfo().top;
47     };
48
49     CodeMirrorPerspective.prototype.unfreezeState = function () {
50         this.codemirror.scrollTo(0, this.config().position || 0);
51     };
52
53         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
54                 $.wiki.Perspective.prototype.onEnter.call(this);
55
56                 this.codemirror.setValue(this.doc.text);
57
58                 this.unfreezeState(this._uistate);
59
60                 if(success) success();
61         }
62
63         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
64                 this.freezeState();
65
66                 $.wiki.Perspective.prototype.onExit.call(this);
67             this.doc.setText(this.codemirror.getValue());
68
69             if ($('.vsplitbar').hasClass('active') && $('#SearchPerspective').hasClass('active')) {
70                 $.wiki.switchToTab('#ScanGalleryPerspective');
71             }
72
73             if(success) success();
74         }
75
76         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
77
78 })(jQuery);