yet another fix for beta
[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: 'default',
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(keycode, event) {
33                                                 if(!event.altKey)
34                                                         return false;
35
36                                                 var c = String.fromCharCode(keycode).toLowerCase();
37                                                 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
38                                                 if(button.length == 0)
39                                                         return false;
40
41                                                 /* it doesn't matter which button we pick - all do the same */
42                                                 event.button = button[0];
43                                                 return true;
44                                         });
45
46                                         $('#source-editor .toolbar').toolbarize({
47                                             actionContext: self.codemirror
48                                         });
49
50                                         console.log("Initialized CodeMirror");
51
52                                         // textarea is no longer needed
53                                         $('codemirror_placeholder').remove();
54
55                                         old_callback.call(self);
56                                 }
57                         });
58                 };
59
60                 $.wiki.Perspective.call(this, options);
61         };
62
63
64         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
65
66         CodeMirrorPerspective.prototype.freezeState = function() {
67                 this.config().position = this.codemirror.win.scrollY || 0;
68         };
69
70         CodeMirrorPerspective.prototype.unfreezeState = function () {
71                 this.codemirror.win.scroll(0, this.config().position || 0);
72         };
73
74         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
75                 $.wiki.Perspective.prototype.onEnter.call(this);
76
77                 console.log('Entering', this.doc);
78                 this.codemirror.setCode(this.doc.text);
79
80                 /* fix line numbers bar */
81                 var $nums = $('.CodeMirror-line-numbers');
82             var barWidth = $nums.width();
83
84                 $(this.codemirror.frame.contentDocument.body).css('padding-left', barWidth);
85                 // $nums.css('left', -barWidth);
86
87                 $(window).resize();
88                 this.unfreezeState(this._uistate);
89
90                 if(success) success();
91         }
92
93         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
94                 this.freezeState();
95
96                 $.wiki.Perspective.prototype.onExit.call(this);
97                 console.log('Exiting', this.doc);
98                 this.doc.setText(this.codemirror.getCode());
99
100         if ($('.vsplitbar').hasClass('active') && $('#SearchPerspective').hasClass('active')) {
101             $.wiki.switchToTab('#ScanGalleryPerspective');
102         }
103
104                 if(success) success();
105         }
106
107         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
108
109 })(jQuery);