forked from 6004x/jade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjade_workbook.js
143 lines (121 loc) · 5.31 KB
/
jade_workbook.js
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
// interface iframe or window containing jade to workbook machinery
jade_defs.services = function (jade) {
var host; // window target for state updates
var jade_instance; // jade instance whose state we'll save
jade.model.set_autosave_trigger(1); // save after every edit
jade.load_from_server = function (filename,shared,callback) {
};
jade.save_to_server = function (json,callback) {
};
jade.unsaved_changes = function(which) {
};
jade.request_zip_url = undefined; // not used here...
// set up editor inside of div's with class "jade"
jade.setup = function (div,setup_channel) {
if (window.parent !== window) {
// make iframe resizable if we can. This may fail if we don't have
// access to our parent...
try {
// look through all our parent's iframes
$('iframe',window.parent.document).each(function () {
// is this iframe us?
if (this.contentWindow == window) {
// yes! so add css to enable resizing
$(this).css({resize:'both', overflow:'auto'});
}
});
} catch (e) {
}
}
// skip if this div has already been configured
if (div.jade === undefined) {
var config = {};
// use text from jade.div, if any
var text = $(div).html();
// strip off <!--[CDATA[ ... ]]--> tag if it's there
if (text.lastIndexOf('<!--[CDATA[',0) === 0) {
text = text.substring(11,text.length-5);
}
$(div).empty(); // all done with innards
if (text)
try {
config = JSON.parse(text);
} catch(e) {
console.log('Error parsing configuration: '+e);
}
// standalone mode... change if message arrives
var saved_state = localStorage.getItem(window.location.pathname);
if (saved_state) {
try {
saved_state = JSON.parse(saved_state);
$.extend(config,saved_state);
} catch (e) {
console.log('Restore of local state failed');
console.log(e.stack);
}
}
// now create the editor, pass along initial configuration
var j = new jade.Jade(div);
j.initialize(config);
if (setup_channel) {
// accept initialization message from host, remember where
// to send update messages when local state changes
$(window).on('message',function (event) {
event = event.originalEvent;
if (event.origin != window.location.origin) return;
var host = event.source;
// {value: , check: , message: , id: }
var answer = JSON.parse(event.data);
// change save_to_server to communicate with host
if (answer.id) {
jade.save_to_server = function (json,callback) {
// update answer object
var state = j.get_state();
answer.value = JSON.stringify(state);
// if there are tests, see if they've been run
answer.message = undefined;
// make sure all required tests passed
answer.check = 'right';
$.each(state['required-tests'] || [],function (index1,rtest) {
var passed = false;
$.each(state['tests'], function (index2, test) {
// test results: error msg or "passed <md5sum> <mverify_md5sum> <benmark>"
if (test.indexOf(rtest) != -1 && test.indexOf('passed') == 0)
passed = true;
});
if (!passed) answer.check = 'wrong';
});
// send it to our host
host.postMessage(JSON.stringify(answer),window.location.origin);
// done...
if (callback) callback();
};
}
if (answer.value) {
var state = JSON.parse(answer.value);
j.initialize(state);
}
});
};
}
};
};
// set up editor inside of the div's with class "jade"
var jade = {};
$(document).ready(function () {
$('.jade').each(function(index, div) {
var j = new jade_defs.jade();
jade_defs.services(j);
// only the first Jade div can interact with host framework
j.setup(div,index == 0);
if (index == 0) {
jade.initialize = j.initialize;
}
});
});
// notify user of unsaved changes
$(window).bind('beforeunload',function () {
if ($('body').attr('data-dirty') !== undefined)
return 'You have unsaved changes on this page.';
return undefined;
});