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