a0a2e69a74feeaa6d538c6c8a6046b36e8972e70
[redakcja.git] / platforma / 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 button').click(function(event){
50                                                 event.preventDefault();
51                                                 var params = eval("(" + $(this).attr('data-ui-action-params') + ")");
52                                                 scriptletCenter.callInteractive({
53                                                         action: $(this).attr('data-ui-action'),
54                                                         context: self.codemirror,
55                                                         extra: params
56                                                 });
57                                         });
58
59                                         $('.toolbar select').change(function(event){
60                                                 var slug = $(this).val();
61
62                                                 $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
63                                                 $(window).resize();
64                                         });
65
66                                         $('.toolbar-buttons-container').hide();
67                                         $('.toolbar select').change();
68
69                                         console.log("Initialized CodeMirror");
70
71                                         // textarea is no longer needed
72                                         $('codemirror_placeholder').remove();
73
74                                         old_callback.call(self);
75                                 }
76                         });
77                 };
78
79                 $.wiki.Perspective.call(this, options);
80         };
81
82
83         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
84
85         CodeMirrorPerspective.prototype.freezeState = function() {
86                 this.config().position = this.codemirror.win.scrollY || 0;
87         };
88
89         CodeMirrorPerspective.prototype.unfreezeState = function () {
90                 this.codemirror.win.scroll(0, this.config().position || 0);
91         };
92
93         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
94                 $.wiki.Perspective.prototype.onEnter.call(this);
95
96                 console.log('Entering', this.doc);
97                 this.codemirror.setCode(this.doc.text);
98
99                 /* fix line numbers bar */
100                 var $nums = $('.CodeMirror-line-numbers');
101             var barWidth = $nums.width();
102
103                 $(this.codemirror.frame.contentDocument.body).css('padding-left', barWidth);
104                 // $nums.css('left', -barWidth);
105
106                 $(window).resize();
107                 this.unfreezeState(this._uistate);
108
109                 if(success) success();
110         }
111
112         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
113                 this.freezeState();
114
115                 $.wiki.Perspective.prototype.onExit.call(this);
116                 console.log('Exiting', this.doc);
117                 this.doc.setText(this.codemirror.getCode());
118
119                 if(success) success();
120         }
121
122         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
123
124 })(jQuery);