4 function CodeMirrorPerspective(options)
6 var old_callback = options.callback;
7 options.callback = function(){
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",
17 iframeClass: 'xml-iframe',
23 initCallback: function(){
25 self.codemirror.grabKeys(function(event) {
27 $(event.button).trigger('click');
31 /* CM reports characters 2 times - as event and as code */
32 if((typeof event) != "object")
38 var c = String.fromCharCode(event.keyCode).toLowerCase();
39 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
40 if(button.length == 0)
43 /* it doesn't matter which button we pick - all do the same */
44 event.button = button[0];
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);
54 $('.toolbar select').change(function(event){
55 var slug = $(this).val();
57 $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
61 $('.toolbar-buttons-container').hide();
62 $('.toolbar select').change();
64 console.log("Initialized CodeMirror");
66 // textarea is no longer needed
67 $('codemirror_placeholder').remove();
69 old_callback.call(self);
74 $.wiki.Perspective.call(this, options);
78 CodeMirrorPerspective.prototype = new $.wiki.Perspective();
80 CodeMirrorPerspective.prototype.freezeState = function() {
86 CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
87 $.wiki.Perspective.prototype.onEnter.call(this);
89 console.log('Entering', this.doc);
90 this.codemirror.setCode(this.doc.text);
92 /* fix line numbers bar */
93 var $nums = $('.CodeMirror-line-numbers');
94 var barWidth = $nums.width();
96 $(this.codemirror.frame).css('margin-left', barWidth);
97 $nums.css('left', -barWidth);
99 $(this.codemirror.frame.contentWindow).trigger('resize');
100 if(success) success();
103 CodeMirrorPerspective.prototype.onExit = function(success, failure) {
104 $.wiki.Perspective.prototype.onExit.call(this);
106 console.log('Exiting', this.doc);
107 this.doc.setText(this.codemirror.getCode());
108 if(success) success();
111 $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;