0f6711032a9d449c3e46599c3ee7140a76f820f5
[redakcja.git] / platforma / static / js / wiki / source_editor.js
1 /* COMMENT */
2 (function($) {
3         
4         function CodeMirrorPerspective(doc, callback) 
5         {
6                 this.perspective_id = 'CodeMirrorPerspective';
7                 this.doc = doc; // document model
8                 
9                 var self = this;
10                 
11                 this.codemirror = CodeMirror.fromTextArea('codemirror_placeholder', {                   
12                         parserfile: 'parsexml.js',
13                         path: STATIC_URL + "js/lib/codemirror/",
14                         stylesheet: STATIC_URL + "css/xmlcolors_15032010.css",
15                         parserConfig: {
16                                 useHTMLKludges: false
17                         },
18                         iframeClass: 'xml-iframe',
19                         textWrapping: true,
20                         lineNumbers: true,
21                         width: "100%",
22                         tabMode: 'spaces',
23                         indentUnit: 0,
24                         initCallback: function() {
25                                 $('#source-editor .toolbar button').click(function(event){
26                     event.preventDefault();
27                     var params = eval("(" + $(this).attr('data-ui-action-params') + ")");
28                         scriptletCenter.scriptlets[$(this).attr('data-ui-action')](self.codemirror, params);
29                         });
30                 
31                         $('.toolbar select').change(function(event){
32                             var slug = $(this).val();
33                             
34                             $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
35                             $(window).resize();
36                         });
37                 
38                         $('.toolbar-buttons-container').hide();
39                         $('.toolbar select').change();          
40                                                 
41                                 console.log("Initialized CodeMirror");
42                                 // textarea is no longer needed
43                                 $('codemirror_placeholder').remove();
44                                 callback.call(self);
45                         }       
46                 });
47         };
48         
49         
50         CodeMirrorPerspective.prototype = new $.wiki.Perspective();
51         
52         CodeMirrorPerspective.prototype.freezeState = function() {
53                 
54         };
55         
56         CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
57                 $.wiki.Perspective.prototype.onEnter.call(this);
58         
59                 console.log(this.doc);
60                 this.codemirror.setCode(this.doc.text);
61                 if(success) success();
62         }
63         
64         CodeMirrorPerspective.prototype.onExit = function(success, failure) {
65                 $.wiki.Perspective.prototype.onExit.call(this);
66         
67                 console.log(this.doc);
68                 this.doc.setText(this.codemirror.getCode());
69                 if(success) success();
70         }
71         
72         $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;
73                 
74 })(jQuery);