Source

sage-iphone-app / template.html

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>
        <!--meta name="viewport" content="width=device-width"/ -->
        <meta name="viewport" content="width=device-width; minimum-scale=.1250; maximum-scale=10.0; initial-scale=1.0; user-scalable=yes">
        <!-- http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/UsingtheViewport/UsingtheViewport.html -->

        <title>Sage Cell Server</title>

        <script>
            /**
             *
             *  Base64 encode / decode
             *  http://www.webtoolkit.info/
             *  http://www.webtoolkit.info/javascript-base64.html
             **/

            var Base64 = {

                // private property
                _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

                // public method for encoding
                encode : function (input) {
                    var output = "";
                    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
                    var i = 0;

                    input = Base64._utf8_encode(input);

                    while (i < input.length) {

                        chr1 = input.charCodeAt(i++);
                        chr2 = input.charCodeAt(i++);
                        chr3 = input.charCodeAt(i++);

                        enc1 = chr1 >> 2;
                        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                        enc4 = chr3 & 63;

                        if (isNaN(chr2)) {
                            enc3 = enc4 = 64;
                        } else if (isNaN(chr3)) {
                            enc4 = 64;
                        }

                        output = output +
                        this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
                        this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

                    }

                    return output;
                },

                // public method for decoding
                decode : function (input) {
                    var output = "";
                    var chr1, chr2, chr3;
                    var enc1, enc2, enc3, enc4;
                    var i = 0;

                    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

                    while (i < input.length) {

                        enc1 = this._keyStr.indexOf(input.charAt(i++));
                        enc2 = this._keyStr.indexOf(input.charAt(i++));
                        enc3 = this._keyStr.indexOf(input.charAt(i++));
                        enc4 = this._keyStr.indexOf(input.charAt(i++));

                        chr1 = (enc1 << 2) | (enc2 >> 4);
                        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                        chr3 = ((enc3 & 3) << 6) | enc4;

                        output = output + String.fromCharCode(chr1);

                        if (enc3 != 64) {
                            output = output + String.fromCharCode(chr2);
                        }
                        if (enc4 != 64) {
                            output = output + String.fromCharCode(chr3);
                        }

                    }

                    output = Base64._utf8_decode(output);

                    return output;

                },

                // private method for UTF-8 encoding
                _utf8_encode : function (string) {
                    string = string.replace(/\r\n/g,"\n");
                    var utftext = "";

                    for (var n = 0; n < string.length; n++) {

                        var c = string.charCodeAt(n);

                        if (c < 128) {
                            utftext += String.fromCharCode(c);
                        }
                        else if((c > 127) && (c < 2048)) {
                            utftext += String.fromCharCode((c >> 6) | 192);
                            utftext += String.fromCharCode((c & 63) | 128);
                        }
                        else {
                            utftext += String.fromCharCode((c >> 12) | 224);
                            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                            utftext += String.fromCharCode((c & 63) | 128);
                        }

                    }

                    return utftext;
                },

                // private method for UTF-8 decoding
                _utf8_decode : function (utftext) {
                    var string = "";
                    var i = 0;
                    var c = c1 = c2 = 0;

                    while ( i < utftext.length ) {

                        c = utftext.charCodeAt(i);

                        if (c < 128) {
                            string += String.fromCharCode(c);
                            i++;
                        }
                        else if((c > 191) && (c < 224)) {
                            c2 = utftext.charCodeAt(i+1);
                            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                            i += 2;
                        }
                        else {
                            c2 = utftext.charCodeAt(i+1);
                            c3 = utftext.charCodeAt(i+2);
                            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                            i += 3;
                        }

                    }

                    return string;
                }

            }
        </script>

        <script>

            function getBase64Image(img) {
                // Create an empty canvas element
                var canvas = document.createElement("canvas");
                canvas.width = img.width;
                canvas.height = img.height;

                // Copy the image contents to the canvas
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0);

                // Get the data-URL formatted image
                // Firefox supports PNG and JPEG. You could check img.src to
                // guess the original format, but be aware the using "image/jpg"
                // will re-encode the image.
                var dataURL = canvas.toDataURL("image/png");
                return dataURL;

                return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
            }

            function encodedImages() {
                var images = document.images;
                for (var i=0; i<images.length; i++){
                    im = images[i];
                    // Encode for
                    if (im.src.match(/\/kernel\//) ) {
                        if (im.complete) {
                            im.src = getBase64Image(im);
                        }
                    }
                }
            }
        </script>

        <style>
            .sagecell_poweredBy, .sagecell_permalink, .sagecell_sessionFiles, .sagecell_messages {
                display:none;
            }
            .sagecell_stdout {
                background:LightGray;
            }
            .ansibold {font-weight: bold;}
            .ansiblack {color: black;}
            .ansired {color: red;}
            .ansiyellow {color: gold;}
            .ansiblue {color: blue;}
            .ansicyan {color: cyan;}
            .ansigrey {color: gray;}

        </style>
        <script src="static/jquery.min.js"></script>
        <script src="embedded_sagecell.js"></script>
        <script>
            $(function () {
                sagecell.makeSagecell({inputLocation:  '#isagein',
                                       // If I specify outputLocation then computation ID shows up.  :-(
                                       // outputLocation: '#isageout',
                                       // evalButtonText: 'Activate',
                                       replaceOutput: true,
                                       editor: "textarea",
                                       mode: "normal",
                                       // languages: ["sage"],
                                       callback: isageCallback,
                                       hide: [
                                           "editor",
                                           "editorToggle",
                                           "evalButton",
                                           "permalink",
                                           // "output",
                                           // "done",
                                           // "files",
                                           // "sessionFiles",
                                           "computationID",
                                           "messages",
                                           // "poweredBy",
                                       ]
                                      });
            });
        </script>
        <script>
            function isage_add_callbacks() {
                isage.Session = sagecell.Session;
                sagecell.Session = function(outputDiv) {

                    var ret = isage.Session(outputDiv);
                    // $([IPython.events]).on("status_busy.Kernel", function (e) {
                    //     if (e.kernel.kernel_id === that.kernel.kernel_id) {
                    //         that.spinner.style.display = "";
                    //     }
                    // });
                    // $([IPython.events]).on("status_idle.Kernel", function (e) {
                    //     if (e.kernel.kernel_id === that.kernel.kernel_id) {
                    //         that.spinner.style.display = "none";
                    //     }
                    // });
                    $([IPython.events]).on("status_dead.Kernel", function (e) {
                                           alert("I'm dead");
                                           });
                    alert("ret"+ret);
                    return ret;
                }
            }
            function isage_get_status() {
                // isage.status:

                // AlephCellStatusHasOutput
                // AlephCellStatusSyntaxError
                //// Intermediate conditions
                // AlephCellStatusHasInput,
                // AlephCellStatusHasAlephID -- work begun
                //// Shouldn't be here
                // AlephCellStatusUninitialized,
                //// Error conditions
                // AlephCellStatusJSONParseError,
                // AlephCellStatusNetworkError,
                spinners = $(".sagecell_spinner");
                newStatus = undefined;
                if (spinners.length > 1) {
                    newStatus = 'invalid';
                } else if (spinners.length == 0) {
                    newStatus = 'input';
                } else {
                    d = spinners[0].style.display;
                    if (d == '') {
                        newStatus = 'busy';
                    } else if (d == 'none') {
                        // TODO: check for syntax errors
                        if ($(".sagecell_pyerr").length > 0 ) {
                            newStatus = 'syntax_error';
                        } else {
                            // check that important images are loaded
                            newStatus = 'output';
                            images = document.images;
                            for (var i=0; i<images.length; i++){
                                im = images[i];
                                if (im.src.match(/\/kernel\//) ) {
                                    if (!im.complete) {
                                        newStatus = 'busy';
                                    }
                                }
                            }
                        }
                    } else {
                        newStatus = 'weird';
                    }
                }
                return newStatus;
            }
            function isage_monitor_status() {
                // isage.status:

                // AlephCellStatusHasOutput
                // AlephCellStatusSyntaxError
                //// Intermediate conditions
                // AlephCellStatusHasInput,
                // AlephCellStatusHasAlephID -- work begun
                //// Shouldn't be here
                // AlephCellStatusUninitialized,
                //// Error conditions
                // AlephCellStatusJSONParseError,
                // AlephCellStatusNetworkError,

                spinners = $(".sagecell_spinner");
                newStatus = isage_get_status();

                if (newStatus != isage.status) {
                    window.location = "isage://status/"+newStatus;
                    isage.status = newStatus;
                }
                if ( newStatus != "output" ) {
                    setTimeout(isage_monitor_status, 250);
                }
                return newStatus;
            }

            function isageSetAndEval(val) {
                // "Delete" any old output
                var output = document.getElementById("isageout");
                output.style = "display:none;";
                output.innerHTML = "";
                //alert("Hello, I deleted the output");
                //alert(output.style);

                var editor = document.getElementsByClassName("sagecell_commands")[0];
                editor.value = val;
                var button = document.getElementsByClassName("sagecell_evalButton")[0];
                button.click();
                setTimeout(isage_monitor_status, 1000);
                return true;
            }
        </script>
        <script>
            function isageGetOutputForStorage(textOnly) {
                var output = document.getElementById("isageout");
                if ( output.innerHTML != "" ) {
                    // We haven't evaluated this cell, so use what we had before
                    return null;
                }

                if (textOnly == undefined ) {
                    textOnly = false;
                }
                //var output = document.getElementsByClassName("sagecell_sessionOutput")[0];
                output = document.getElementById("isagein");
                if (textOnly) {
                    return output.innerText;
                } else {
                    encodedImages();
                    return output.innerHTML;
                }
            }
            function isageSetOutputFromStorage(content) {
                return "frack";
                alert("here");
                isage.output = content;
                alert(isage.output);
                return isageSetOutputFromStorageIfSetup();
            }
            function isageSetOutputFromStorageIfSetup() {
                //alert(isage.setup);
                //alert(isage.output);
                if (isage.setup && isage.output ) {
                    //var output = document.getElementsByClassName("sagecell_output")[0];
                    var output = document.getElementById("isageout");
                    output.innerHTML = isage.output;
                    //output.style = "display:block;";
                    alert("Hi, I set output");
                    return true;
                }
                return false;
            }
            var isage = {
                setup: false,
                output: null,
            }
            function isageCallback() {
                //alert("isageCallback");
                isage.setup = true;
                isageSetOutputFromStorageIfSetup();
                noinet = document.getElementById('isagenointernet');
                if (!navigator.onLine) {
                    noinet.innerHTML = "It appears you are offline.";
                } else {
                    noinet.style.display = 'none';
                    window.location = "isage://doneLoading/";
                }
                // setTimeout(isage_monitor_status, 1000);
            }
            function isageNoticeInternetDown() {
                noinet = document.getElementById('isagenointernet');
                if (!navigator.onLine) {
                    noinet.innerHTML = "You are offline, please check your settings and try again.";
                } else {
                    noinet.innerHTML = "It is taking a long time to load this page.  Please check your internect connection and try again.";
                }
            }
        </script>
        <script>
            function isageDoneLoading() {
                // TODO: http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/
                window.location = "isage://doneLoading/";
            }
        </script>
        <style>
            ui-button-text { visibility:hidden; };
        </style>
    </head>
    <body onload="setTimeout(isageNoticeInternetDown, 5000);">
        <div id="isagenointernet">Loading, please wait...</div>
        <div id="isagein"></div>
        <div id="isageout" style="">%@</div>
    </body>
</html>