~ubuntu-branches/ubuntu/trusty/qiime/trusty

« back to all changes in this revision

Viewing changes to web/home_static/nih-cloud-apr2012/NIHCloudDemo_Complete_files/textcell.js

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2013-06-17 18:28:26 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20130617182826-376az5ad080a0sfe
Tags: 1.7.0+dfsg-1
Upload preparations done for BioLinux to Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//----------------------------------------------------------------------------
2
 
//  Copyright (C) 2008-2011  The IPython Development Team
3
 
//
4
 
//  Distributed under the terms of the BSD License.  The full license is in
5
 
//  the file COPYING, distributed as part of this software.
6
 
//----------------------------------------------------------------------------
7
 
 
8
 
//============================================================================
9
 
// TextCell
10
 
//============================================================================
11
 
 
12
 
var IPython = (function (IPython) {
13
 
 
14
 
    // TextCell base class
15
 
 
16
 
    var TextCell = function (notebook) {
17
 
        this.code_mirror_mode = this.code_mirror_mode || 'htmlmixed';
18
 
        IPython.Cell.apply(this, arguments);
19
 
        this.rendered = false;
20
 
        this.cell_type = this.cell_type || 'text';
21
 
    };
22
 
 
23
 
 
24
 
    TextCell.prototype = new IPython.Cell();
25
 
 
26
 
 
27
 
    TextCell.prototype.create_element = function () {
28
 
        var cell = $("<div>").addClass('cell text_cell border-box-sizing');
29
 
        cell.attr('tabindex','2');
30
 
        var input_area = $('<div/>').addClass('text_cell_input border-box-sizing');
31
 
        this.code_mirror = CodeMirror(input_area.get(0), {
32
 
            indentUnit : 4,
33
 
            mode: this.code_mirror_mode,
34
 
            theme: 'default',
35
 
            value: this.placeholder,
36
 
            readOnly: this.read_only,
37
 
            lineWrapping : true,
38
 
            onKeyEvent: $.proxy(this.handle_codemirror_keyevent,this)
39
 
        });
40
 
        // The tabindex=-1 makes this div focusable.
41
 
        var render_area = $('<div/>').addClass('text_cell_render border-box-sizing').
42
 
            addClass('rendered_html').attr('tabindex','-1');
43
 
        cell.append(input_area).append(render_area);
44
 
        this.element = cell;
45
 
    };
46
 
 
47
 
 
48
 
    TextCell.prototype.bind_events = function () {
49
 
        IPython.Cell.prototype.bind_events.apply(this);
50
 
        var that = this;
51
 
        this.element.keydown(function (event) {
52
 
            if (event.which === 13 && !event.shiftKey) {
53
 
                if (that.rendered) {
54
 
                    that.edit();
55
 
                    return false;
56
 
                };
57
 
            };
58
 
        });
59
 
        this.element.dblclick(function () {
60
 
            that.edit();
61
 
        });
62
 
    };
63
 
 
64
 
 
65
 
    TextCell.prototype.handle_codemirror_keyevent = function (editor, event) {
66
 
        // This method gets called in CodeMirror's onKeyDown/onKeyPress
67
 
        // handlers and is used to provide custom key handling. Its return
68
 
        // value is used to determine if CodeMirror should ignore the event:
69
 
        // true = ignore, false = don't ignore.
70
 
        
71
 
        if (event.keyCode === 13 && (event.shiftKey || event.ctrlKey)) {
72
 
            // Always ignore shift-enter in CodeMirror as we handle it.
73
 
            return true;
74
 
        }
75
 
        return false;
76
 
    };
77
 
 
78
 
 
79
 
    TextCell.prototype.select = function () {
80
 
        IPython.Cell.prototype.select.apply(this);
81
 
        var output = this.element.find("div.text_cell_render");
82
 
        output.trigger('focus');
83
 
    };
84
 
 
85
 
 
86
 
    TextCell.prototype.unselect = function() {
87
 
        // render on selection of another cell
88
 
        this.render();
89
 
        IPython.Cell.prototype.unselect.apply(this);
90
 
    };
91
 
 
92
 
 
93
 
    TextCell.prototype.edit = function () {
94
 
        if ( this.read_only ) return;
95
 
        if (this.rendered === true) {
96
 
            var text_cell = this.element;
97
 
            var output = text_cell.find("div.text_cell_render");  
98
 
            output.hide();
99
 
            text_cell.find('div.text_cell_input').show();
100
 
            this.code_mirror.refresh();
101
 
            this.code_mirror.focus();
102
 
            // We used to need an additional refresh() after the focus, but
103
 
            // it appears that this has been fixed in CM. This bug would show
104
 
            // up on FF when a newly loaded markdown cell was edited.
105
 
            this.rendered = false;
106
 
            if (this.get_text() === this.placeholder) {
107
 
                this.set_text('');
108
 
                this.refresh();
109
 
            }
110
 
        }
111
 
    };
112
 
 
113
 
 
114
 
    // Subclasses must define render.
115
 
    TextCell.prototype.render = function () {};
116
 
 
117
 
 
118
 
    TextCell.prototype.get_text = function() {
119
 
        return this.code_mirror.getValue();
120
 
    };
121
 
 
122
 
 
123
 
    TextCell.prototype.set_text = function(text) {
124
 
        this.code_mirror.setValue(text);
125
 
        this.code_mirror.refresh();
126
 
    };
127
 
 
128
 
 
129
 
    TextCell.prototype.get_rendered = function() {
130
 
        return this.element.find('div.text_cell_render').html();
131
 
    };
132
 
 
133
 
 
134
 
    TextCell.prototype.set_rendered = function(text) {
135
 
        this.element.find('div.text_cell_render').html(text);
136
 
    };
137
 
 
138
 
 
139
 
    TextCell.prototype.at_top = function () {
140
 
        if (this.rendered) {
141
 
            return true;
142
 
        } else {
143
 
            return false;
144
 
        }
145
 
    };
146
 
 
147
 
 
148
 
    TextCell.prototype.at_bottom = function () {
149
 
        if (this.rendered) {
150
 
            return true;
151
 
        } else {
152
 
            return false;
153
 
        }
154
 
    };
155
 
 
156
 
 
157
 
    TextCell.prototype.fromJSON = function (data) {
158
 
        if (data.cell_type === this.cell_type) {
159
 
            if (data.source !== undefined) {
160
 
                this.set_text(data.source);
161
 
                this.set_rendered(data.rendered || '');
162
 
                this.rendered = false;
163
 
                this.render();
164
 
            }
165
 
        }
166
 
    };
167
 
 
168
 
 
169
 
    TextCell.prototype.toJSON = function () {
170
 
        var data = {};
171
 
        data.cell_type = this.cell_type;
172
 
        data.source = this.get_text();
173
 
        return data;
174
 
    };
175
 
 
176
 
 
177
 
    // HTMLCell
178
 
 
179
 
    var HTMLCell = function (notebook) {
180
 
        this.placeholder = "Type <strong>HTML</strong> and LaTeX: $\\alpha^2$";
181
 
        IPython.TextCell.apply(this, arguments);
182
 
        this.cell_type = 'html';
183
 
    };
184
 
 
185
 
 
186
 
    HTMLCell.prototype = new TextCell();
187
 
 
188
 
 
189
 
    HTMLCell.prototype.render = function () {
190
 
        if (this.rendered === false) {
191
 
            var text = this.get_text();
192
 
            if (text === "") { text = this.placeholder; }
193
 
            this.set_rendered(text);
194
 
            this.typeset();
195
 
            this.element.find('div.text_cell_input').hide();
196
 
            this.element.find("div.text_cell_render").show();
197
 
            this.rendered = true;
198
 
        }
199
 
    };
200
 
 
201
 
 
202
 
    // MarkdownCell
203
 
 
204
 
    var MarkdownCell = function (notebook) {
205
 
        this.placeholder = "Type *Markdown* and LaTeX: $\\alpha^2$";
206
 
        IPython.TextCell.apply(this, arguments);
207
 
        this.cell_type = 'markdown';
208
 
    };
209
 
 
210
 
 
211
 
    MarkdownCell.prototype = new TextCell();
212
 
 
213
 
 
214
 
    MarkdownCell.prototype.render = function () {
215
 
        if (this.rendered === false) {
216
 
            var text = this.get_text();
217
 
            if (text === "") { text = this.placeholder; }
218
 
            var html = IPython.markdown_converter.makeHtml(text);
219
 
            this.set_rendered(html);
220
 
            this.typeset()
221
 
            this.element.find('div.text_cell_input').hide();
222
 
            this.element.find("div.text_cell_render").show();
223
 
            var code_snippets = this.element.find("pre > code");
224
 
            code_snippets.replaceWith(function () {
225
 
                var code = $(this).html();
226
 
                /* Substitute br for newlines and &nbsp; for spaces
227
 
                   before highlighting, since prettify doesn't
228
 
                   preserve those on all browsers */
229
 
                code = code.replace(/(\r\n|\n|\r)/gm, "<br/>");
230
 
                code = code.replace(/ /gm, '&nbsp;');
231
 
                code = prettyPrintOne(code);
232
 
 
233
 
                return '<code class="prettyprint">' + code + '</code>';
234
 
            });
235
 
            this.rendered = true;
236
 
        }
237
 
    };
238
 
 
239
 
 
240
 
    // PlaintextCell
241
 
 
242
 
    var PlaintextCell = function (notebook) {
243
 
        this.placeholder = "Type plain text and LaTeX: $\\alpha^2$";
244
 
        this.code_mirror_mode = 'rst';
245
 
        IPython.TextCell.apply(this, arguments);
246
 
        this.cell_type = 'plaintext';
247
 
    };
248
 
 
249
 
 
250
 
    PlaintextCell.prototype = new TextCell();
251
 
 
252
 
 
253
 
    PlaintextCell.prototype.render = function () {
254
 
        this.rendered = true;
255
 
        this.edit();
256
 
    };
257
 
 
258
 
 
259
 
    PlaintextCell.prototype.select = function () {
260
 
        IPython.Cell.prototype.select.apply(this);
261
 
        this.code_mirror.refresh();
262
 
        this.code_mirror.focus();
263
 
    };
264
 
 
265
 
 
266
 
    PlaintextCell.prototype.at_top = function () {
267
 
        var cursor = this.code_mirror.getCursor();
268
 
        if (cursor.line === 0) {
269
 
            return true;
270
 
        } else {
271
 
            return false;
272
 
        }
273
 
    };
274
 
 
275
 
 
276
 
    PlaintextCell.prototype.at_bottom = function () {
277
 
        var cursor = this.code_mirror.getCursor();
278
 
        if (cursor.line === (this.code_mirror.lineCount()-1)) {
279
 
            return true;
280
 
        } else {
281
 
            return false;
282
 
        }
283
 
    };
284
 
 
285
 
 
286
 
    // HTMLCell
287
 
 
288
 
    var HeadingCell = function (notebook) {
289
 
        this.placeholder = "Type Heading Here";
290
 
        IPython.TextCell.apply(this, arguments);
291
 
        this.cell_type = 'heading';
292
 
        this.level = 1;
293
 
    };
294
 
 
295
 
 
296
 
    HeadingCell.prototype = new TextCell();
297
 
 
298
 
 
299
 
    HeadingCell.prototype.fromJSON = function (data) {
300
 
        if (data.level != undefined){
301
 
            this.level = data.level;
302
 
        }
303
 
        IPython.TextCell.prototype.fromJSON.apply(this, arguments);
304
 
    };
305
 
 
306
 
 
307
 
    HeadingCell.prototype.toJSON = function () {
308
 
        var data = IPython.TextCell.prototype.toJSON.apply(this);
309
 
        data.level = this.get_level();
310
 
        return data;
311
 
    };
312
 
 
313
 
 
314
 
    HeadingCell.prototype.set_level = function (level) {
315
 
        this.level = level;
316
 
        if (this.rendered) {
317
 
            this.rendered = false;
318
 
            this.render();
319
 
        };
320
 
    };
321
 
 
322
 
 
323
 
    HeadingCell.prototype.get_level = function () {
324
 
        return this.level;
325
 
    };
326
 
 
327
 
 
328
 
    HeadingCell.prototype.set_rendered = function (text) {
329
 
        var r = this.element.find("div.text_cell_render");
330
 
        r.empty();
331
 
        r.append($('<h'+this.level+'/>').html(text));
332
 
    };
333
 
 
334
 
 
335
 
    HeadingCell.prototype.get_rendered = function () {
336
 
        var r = this.element.find("div.text_cell_render");
337
 
        return r.children().first().html();
338
 
    };
339
 
 
340
 
 
341
 
    HeadingCell.prototype.render = function () {
342
 
        if (this.rendered === false) {
343
 
            var text = this.get_text();
344
 
            if (text === "") { text = this.placeholder; }
345
 
            this.set_rendered(text);
346
 
            this.typeset();
347
 
            this.element.find('div.text_cell_input').hide();
348
 
            this.element.find("div.text_cell_render").show();
349
 
            this.rendered = true;
350
 
        };
351
 
    };
352
 
 
353
 
    IPython.TextCell = TextCell;
354
 
    IPython.HTMLCell = HTMLCell;
355
 
    IPython.MarkdownCell = MarkdownCell;
356
 
    IPython.PlaintextCell = PlaintextCell;
357
 
    IPython.HeadingCell = HeadingCell;
358
 
 
359
 
 
360
 
    return IPython;
361
 
 
362
 
}(IPython));
363