##// END OF EJS Templates
JS Configurablity Take 2...
Matthias BUSSONNIER -
Show More
@@ -32,30 +32,41 b' var IPython = (function (IPython) {'
32 */
32 */
33 var Cell = function (options) {
33 var Cell = function (options) {
34
34
35 options = options || {};
35 options = this.mergeopt(Cell, options)
36 // superclass default overwrite our default
36 // superclass default overwrite our default
37 this.cm_config = $.extend({},Cell.cm_default,options.cm_config);
38
37
39 this.placeholder = this.placeholder || '';
38 this.placeholder = options.placeholder || '';
40 this.read_only = false;
39 this.read_only = options.cm_config.readOnly;
41 this.selected = false;
40 this.selected = false;
42 this.element = null;
41 this.element = null;
43 this.metadata = {};
42 this.metadata = {};
44 // load this from metadata later ?
43 // load this from metadata later ?
45 this.user_highlight = 'auto';
44 this.user_highlight = 'auto';
45 this.cm_config = options.cm_config;
46 this.create_element();
46 this.create_element();
47 if (this.element !== null) {
47 if (this.element !== null) {
48 this.element.data("cell", this);
48 this.element.data("cell", this);
49 this.bind_events();
49 this.bind_events();
50 }
50 }
51 this.cell_id = utils.uuid();
51 this.cell_id = utils.uuid();
52 this._options = options;
52 };
53 };
53
54
54 Cell.cm_default = {
55 Cell.options_default = {
56 cm_config : {
55 indentUnit : 4,
57 indentUnit : 4,
56 readOnly: this.read_only,
58 readOnly: false,
59 theme: "default"
60 }
57 };
61 };
58
62
63 Cell.prototype.mergeopt = function(_class, options, overwrite){
64 overwrite = overwrite ||Β {};
65 return $.extend(true, {}, _class.options_default, options, overwrite)
66
67 }
68
69
59
70
60 /**
71 /**
61 * Empty. Subclasses must implement create_element.
72 * Empty. Subclasses must implement create_element.
@@ -95,7 +106,7 b' var IPython = (function (IPython) {'
95 Cell.prototype.typeset = function () {
106 Cell.prototype.typeset = function () {
96 if (window.MathJax){
107 if (window.MathJax){
97 var cell_math = this.element.get(0);
108 var cell_math = this.element.get(0);
98 MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]);
109 MathJax.Hub.Queue(["Typeset", MathJax.Hub, cell_math]);
99 }
110 }
100 };
111 };
101
112
@@ -196,7 +207,7 b' var IPython = (function (IPython) {'
196 **/
207 **/
197 Cell.prototype.get_pre_cursor = function () {
208 Cell.prototype.get_pre_cursor = function () {
198 var cursor = this.code_mirror.getCursor();
209 var cursor = this.code_mirror.getCursor();
199 var text = this.code_mirror.getRange({line:0,ch:0}, cursor);
210 var text = this.code_mirror.getRange({line:0, ch:0}, cursor);
200 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
211 text = text.replace(/^\n+/, '').replace(/\n+$/, '');
201 return text;
212 return text;
202 }
213 }
@@ -293,7 +304,7 b' var IPython = (function (IPython) {'
293 // here we handle non magic_modes
304 // here we handle non magic_modes
294 if(first_line.match(regs[reg]) != null) {
305 if(first_line.match(regs[reg]) != null) {
295 if (mode.search('magic_') != 0) {
306 if (mode.search('magic_') != 0) {
296 this.code_mirror.setOption('mode',mode);
307 this.code_mirror.setOption('mode', mode);
297 CodeMirror.autoLoadMode(this.code_mirror, mode);
308 CodeMirror.autoLoadMode(this.code_mirror, mode);
298 return;
309 return;
299 }
310 }
@@ -62,7 +62,6 b' var IPython = (function (IPython) {'
62 * @param [options.cm_config] {object} config to pass to CodeMirror
62 * @param [options.cm_config] {object} config to pass to CodeMirror
63 */
63 */
64 var CodeCell = function (kernel, options) {
64 var CodeCell = function (kernel, options) {
65 var options = options || {}
66 this.kernel = kernel || null;
65 this.kernel = kernel || null;
67 this.code_mirror = null;
66 this.code_mirror = null;
68 this.input_prompt_number = null;
67 this.input_prompt_number = null;
@@ -71,15 +70,10 b' var IPython = (function (IPython) {'
71
70
72
71
73 var cm_overwrite_options = {
72 var cm_overwrite_options = {
74 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
75 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
73 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
76 };
74 };
77
75
78 var arg_cm_options = options.cm_options || {};
76 options = this.mergeopt(CodeCell, options, {cm_config:cm_overwrite_options});
79 var cm_config = $.extend({},CodeCell.cm_default, arg_cm_options, cm_overwrite_options);
80
81 var options = {};
82 options.cm_config = cm_config;
83
77
84 IPython.Cell.apply(this,[options]);
78 IPython.Cell.apply(this,[options]);
85
79
@@ -89,10 +83,13 b' var IPython = (function (IPython) {'
89 );
83 );
90 };
84 };
91
85
92 CodeCell.cm_default = {
86 CodeCell.options_default = {
87 cm_config : {
88 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess",'Backspace':"delSpaceToPrevTabStop"},
93 mode: 'python',
89 mode: 'python',
94 theme: 'ipython',
90 theme: 'ipython',
95 matchBrackets: true
91 matchBrackets: true
92 }
96 };
93 };
97
94
98
95
@@ -9,6 +9,8 b''
9 // TextCell
9 // TextCell
10 //============================================================================
10 //============================================================================
11
11
12
13
12 /**
14 /**
13 A module that allow to create different type of Text Cell
15 A module that allow to create different type of Text Cell
14 @module IPython
16 @module IPython
@@ -28,37 +30,39 b' var IPython = (function (IPython) {'
28 * @extend Ipython.Cell
30 * @extend Ipython.Cell
29 * @param {object|undefined} [options]
31 * @param {object|undefined} [options]
30 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
32 * @param [options.cm_config] {object} config to pass to CodeMirror, will extend/overwrite default config
33 * @param [options.placeholder] {string} default string to use when souce in empty for rendering (only use in some TextCell subclass)
31 */
34 */
32 var TextCell = function (options) {
35 var TextCell = function (options) {
33 this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
36 // in all TextCell/Cell subclasses
34 var options = options || {};
37 // do not assign most of members here, just pass it down
38 // in the options dict potentially overwriting what you wish.
39 // they will be assigned in the base class.
35
40
41 // we cannot put this as a class key as it has handle to "this".
36 var cm_overwrite_options = {
42 var cm_overwrite_options = {
37 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
38 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
43 onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
39 };
44 };
40
45
41 var arg_cm_options = options.cm_options || {};
46 options = this.mergeopt(TextCell,options,{cm_config:cm_overwrite_options});
42 var cm_config = $.extend({},TextCell.cm_default, arg_cm_options, cm_overwrite_options);
43
47
44 var options = {};
48 IPython.Cell.apply(this, [options]);
45 options.cm_config = cm_config;
46
49
47
50
48 IPython.Cell.apply(this, [options]);
49 this.rendered = false;
51 this.rendered = false;
50 this.cell_type = this.cell_type || 'text';
52 this.cell_type = this.cell_type || 'text';
51 };
53 };
52
54
53 TextCell.cm_default = {
55 TextCell.prototype = new IPython.Cell();
54 mode: this.code_mirror_mode,
56
55 theme: 'default',
57 TextCell.options_default = {
56 value: this.placeholder,
58 cm_config : {
59 extraKeys: {"Tab": "indentMore","Shift-Tab" : "indentLess"},
60 mode: 'htmlmixed',
57 lineWrapping : true,
61 lineWrapping : true,
58 }
62 }
63 };
59
64
60
65
61 TextCell.prototype = new IPython.Cell();
62
66
63 /**
67 /**
64 * Create the DOM element of the TextCell
68 * Create the DOM element of the TextCell
@@ -75,6 +79,7 b' var IPython = (function (IPython) {'
75
79
76 var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
80 var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
77 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
81 this.code_mirror = CodeMirror(input_area.get(0), this.cm_config);
82
78 // The tabindex=-1 makes this div focusable.
83 // The tabindex=-1 makes this div focusable.
79 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
84 var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
80 addClass('rendered_html').attr('tabindex','-1');
85 addClass('rendered_html').attr('tabindex','-1');
@@ -282,12 +287,21 b' var IPython = (function (IPython) {'
282 * @class HtmlCell
287 * @class HtmlCell
283 * @extends Ipython.TextCell
288 * @extends Ipython.TextCell
284 */
289 */
285 var HTMLCell = function () {
290 var HTMLCell = function (options) {
286 this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$";
291
287 IPython.TextCell.apply(this, arguments);
292 options = this.mergeopt(HTMLCell,options);
293 TextCell.apply(this, [options]);
294
288 this.cell_type = 'html';
295 this.cell_type = 'html';
289 };
296 };
290
297
298 HTMLCell.options_default = {
299 cm_config : {
300 mode: 'htmlmixed',
301 },
302 placeholder: "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$"
303 };
304
291
305
292 HTMLCell.prototype = new TextCell();
306 HTMLCell.prototype = new TextCell();
293
307
@@ -312,12 +326,24 b' var IPython = (function (IPython) {'
312 * @constructor MarkdownCell
326 * @constructor MarkdownCell
313 * @extends Ipython.HtmlCell
327 * @extends Ipython.HtmlCell
314 */
328 */
315 var MarkdownCell = function () {
329 var MarkdownCell = function (options) {
316 this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$";
330 var options = options || {};
317 IPython.TextCell.apply(this, arguments);
331
332 options = this.mergeopt(MarkdownCell,options);
333 TextCell.apply(this, [options]);
334
318 this.cell_type = 'markdown';
335 this.cell_type = 'markdown';
319 };
336 };
320
337
338 MarkdownCell.options_default = {
339 cm_config: {
340 mode: 'markdown'
341 },
342 placeholder: "Type *Markdown* and LaTeX: $\\alpha^2$"
343 }
344
345
346
321
347
322 MarkdownCell.prototype = new TextCell();
348 MarkdownCell.prototype = new TextCell();
323
349
@@ -367,18 +393,24 b' var IPython = (function (IPython) {'
367 * @constructor RawCell
393 * @constructor RawCell
368 * @extends Ipython.TextCell
394 * @extends Ipython.TextCell
369 */
395 */
370 var RawCell = function () {
396 var RawCell = function (options) {
371 this.placeholder = "Type plain text and LaTeX: $\\alpha^2$";
397
372 this.code_mirror_mode = 'rst';
398 options = this.mergeopt(RawCell,options)
373 IPython.TextCell.apply(this, arguments);
399 TextCell.apply(this, [options]);
400
374 this.cell_type = 'raw';
401 this.cell_type = 'raw';
375 var that = this
376
402
403 var that = this
377 this.element.focusout(
404 this.element.focusout(
378 function() { that.auto_highlight(); }
405 function() { that.auto_highlight(); }
379 );
406 );
380 };
407 };
381
408
409 RawCell.options_default = {
410 placeholder : "Type plain text and LaTeX: $\\alpha^2$"
411 };
412
413
382
414
383 RawCell.prototype = new TextCell();
415 RawCell.prototype = new TextCell();
384
416
@@ -461,9 +493,11 b' var IPython = (function (IPython) {'
461 * @constructor HeadingCell
493 * @constructor HeadingCell
462 * @extends Ipython.TextCell
494 * @extends Ipython.TextCell
463 */
495 */
464 var HeadingCell = function () {
496 var HeadingCell = function (options) {
465 this.placeholder = "Type Heading Here";
497
466 IPython.TextCell.apply(this, arguments);
498 options = this.mergeopt(HeadingCell,options)
499 TextCell.apply(this, [options]);
500
467 /**
501 /**
468 * heading level of the cell, use getter and setter to access
502 * heading level of the cell, use getter and setter to access
469 * @property level
503 * @property level
@@ -472,6 +506,9 b' var IPython = (function (IPython) {'
472 this.cell_type = 'heading';
506 this.cell_type = 'heading';
473 };
507 };
474
508
509 HeadingCell.options_default = {
510 placeholder: "Type Heading Here"
511 };
475
512
476 HeadingCell.prototype = new TextCell();
513 HeadingCell.prototype = new TextCell();
477
514
@@ -480,13 +517,13 b' var IPython = (function (IPython) {'
480 if (data.level != undefined){
517 if (data.level != undefined){
481 this.level = data.level;
518 this.level = data.level;
482 }
519 }
483 IPython.TextCell.prototype.fromJSON.apply(this, arguments);
520 TextCell.prototype.fromJSON.apply(this, arguments);
484 };
521 };
485
522
486
523
487 /** @method toJSON */
524 /** @method toJSON */
488 HeadingCell.prototype.toJSON = function () {
525 HeadingCell.prototype.toJSON = function () {
489 var data = IPython.TextCell.prototype.toJSON.apply(this);
526 var data = TextCell.prototype.toJSON.apply(this);
490 data.level = this.get_level();
527 data.level = this.get_level();
491 return data;
528 return data;
492 };
529 };
General Comments 0
You need to be logged in to leave comments. Login now