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 readOnly: CurrentDocument.readonly || false,
24 initCallback: function(){
26 self.codemirror.grabKeys(function(event) {
28 $(event.button).trigger('click');
32 /* CM reports characters 2 times - as event and as code */
33 if((typeof event) != "object")
39 var c = String.fromCharCode(event.keyCode).toLowerCase();
40 var button = $("#source-editor button[data-ui-accesskey='"+c+"']");
41 if(button.length == 0)
44 /* it doesn't matter which button we pick - all do the same */
45 event.button = button[0];
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,
59 $('.toolbar select').change(function(event){
60 var slug = $(this).val();
62 $('.toolbar-buttons-container').hide().filter('[data-group=' + slug + ']').show();
66 $('.toolbar-buttons-container').hide();
67 $('.toolbar select').change();
69 console.log("Initialized CodeMirror");
71 // textarea is no longer needed
72 $('codemirror_placeholder').remove();
74 old_callback.call(self);
79 $.wiki.Perspective.call(this, options);
83 CodeMirrorPerspective.prototype = new $.wiki.Perspective();
85 CodeMirrorPerspective.prototype.freezeState = function() {
86 this.config().position = this.codemirror.win.scrollY || 0;
89 CodeMirrorPerspective.prototype.unfreezeState = function () {
90 this.codemirror.win.scroll(0, this.config().position || 0);
93 CodeMirrorPerspective.prototype.onEnter = function(success, failure) {
94 $.wiki.Perspective.prototype.onEnter.call(this);
96 console.log('Entering', this.doc);
97 this.codemirror.setCode(this.doc.text);
99 /* fix line numbers bar */
100 var $nums = $('.CodeMirror-line-numbers');
101 var barWidth = $nums.width();
103 $(this.codemirror.frame.contentDocument.body).css('padding-left', barWidth);
104 // $nums.css('left', -barWidth);
107 this.unfreezeState(this._uistate);
109 if(success) success();
112 CodeMirrorPerspective.prototype.onExit = function(success, failure) {
115 $.wiki.Perspective.prototype.onExit.call(this);
116 console.log('Exiting', this.doc);
117 this.doc.setText(this.codemirror.getCode());
119 if(success) success();
122 $.wiki.CodeMirrorPerspective = CodeMirrorPerspective;