0acd2cd92c03a2c8fb660162257cf67de187bced
[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                                 initCallback: function(){
24
25                                         self.codemirror.grabKeys(function(event) {
26                                                 if (event.button) {
27                                                         $(event.button).trigger('click');
28                                                         event.button = null;
29                                                 }
30                                         }, function(event) {
31                                                 /* CM reports characters 2 times - as event and as code */
32                                                 if((typeof event) != "object")
33                                                         return false;
34
35                                                 if(!event.altKey)
36                                                         return false;
37
38                                                 var c = String.fromCharCode(event.keyCode).toLowerCase();
39                                                 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
40                                                 if(button.length == 0)
41                                                         return false;
42
43                                                 /* it doesn't matter which button we pick - all do the same */
44                                                 event.button = button[0];
45                                                 return true;
46                                         });
47
48                                         $('#source-editor .toolbar button').click(function(event){
49                                                 event.preventDefault();
50                                                 var params = eval("(" + $(this).attr('data-ui-action-params') + ")");
51                                                 scriptletCenter.scriptlets[$(this).attr('data-ui-action')](self.codemirror, params);
52                                         });
53
54                                         $('.toolbar select').change(function(event){
55                                                 var slug = $(this).val();
56
57                                                 $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
58                                                 $(window).resize();
59                                         });
60
61                                         $('.toolbar-buttons-container').hide();
62                                         $('.toolbar select').change();
63
64                                         console.log("Initialized CodeMirror");
65                                         
66                                         // textarea is no longer needed
67                                         $('codemirror_placeholder').remove();
68                                         
69                                         old_callback.call(self);
70                                 }
71                         });
72                 };
73
74                 $.wiki.Perspective.call(this, options);
75         };
76
77
78         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
79
80         CodeMirrorPerspective.prototype.freezeState = function() {
81
82         };
83         
84         
85
86         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
87                 $.wiki.Perspective.prototype.onEnter.call(this);
88
89                 console.log('Entering', this.doc);
90                 this.codemirror.setCode(this.doc.text);
91                 
92                 /* fix line numbers bar */
93                 var $nums = $('.CodeMirror-line-numbers');
94             var barWidth = $nums.width();
95                 
96                 $(this.codemirror.frame).css('margin-left', barWidth);
97                 $nums.css('left', -barWidth);
98                 
99                 $(this.codemirror.frame.contentWindow).trigger('resize');
100                 if(success) success();
101         }
102
103         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
104                 $.wiki.Perspective.prototype.onExit.call(this);
105
106                 console.log('Exiting', this.doc);
107                 this.doc.setText(this.codemirror.getCode());
108                 if(success) success();
109         }
110
111         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
112
113 })(jQuery);