9cfa510af7a248424a6f963271ef366f5ce7af6b
[redakcja.git] / platforma / static / js / wiki / source_editor.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: false,
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                                         // textarea is no longer needed
66                                         $('codemirror_placeholder').remove();
67                                         old_callback.call(self);
68                                 }
69                         });     
70                 };
71                 
72                 $.wiki.Perspective.call(this, options);
73         };
74         
75         
76         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
77         
78         CodeMirrorPerspective.prototype.freezeState = function() {
79                 
80         };
81         
82         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
83                 $.wiki.Perspective.prototype.onEnter.call(this);
84         
85                 console.log('Entering', this.doc);
86                 this.codemirror.setCode(this.doc.text);
87                 if(success) success();
88         }
89         
90         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
91                 $.wiki.Perspective.prototype.onExit.call(this);
92         
93                 console.log('Exiting', this.doc);
94                 this.doc.setText(this.codemirror.getCode());
95                 if(success) success();
96         }
97         
98         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
99                 
100 })(jQuery);