-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathjade_6004.js
111 lines (98 loc) · 4.24 KB
/
jade_6004.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
// sandbox can only load shared modules
jade_defs.services = function (jade) {
jade.load_from_server = function (filename,shared,callback) {
};
// sandbox doesn't save changes
jade.save_to_server = function (json,callback) {
};
jade.unsaved_changes = function(which) {
};
jade.request_zip_url = undefined; //'/jade-server?zip=1';
// return JSON representation of persistent state
jade_defs.getState = function() {
var div = $('.jade').get(0);
var state = {};
if (div.jade) state = div.jade.get_state();
return JSON.stringify(state);
};
jade.setup = function (div) {
// skip if this div has already been configured
if (div.jade === undefined) {
// if this Jade needs to save state, make sure user
// doesn't navigate away unintentionally
if ($(div).hasClass('jade-save-state'))
jade.unsaved_changes = function(which) {
if (which && $('body').attr('data-dirty') === undefined)
$('body').attr('data-dirty','true');
else if (!which && $('body').attr('data-dirty') !== undefined)
$('body').removeAttr('data-dirty');
};
// are we in an iframe?
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
var foo;
$('iframe',window.parent.document).each(function () {
// is this iframe us?
if (this.contentWindow == window) {
foo = this;
// yes! so add css to enable resizing
$(this).css({resize:'both', overflow:'auto'});
// initial state is JSON stored as text child of <iframe>
var state = JSON.parse($(this).text() || '{}');
// grab our server-side state from the appropriate input field
var id = $(this).attr('data-id');
if (id) {
var input = $("[name='"+id+"']",window.parent.document);
if (input) {
// overwrite with user's state from server
input = input.val();
if (input.length > 0) {
var args = JSON.parse(input);
args.student_id = window.parent.anonymous_student_id;
$.extend(state,args);
}
}
}
var j = new jade.Jade(div);
j.initialize(state);
}
});
} catch (e) {
alert(e.stack ? e.stack : e);
}
} else {
var text = $(div).text().trim();
// starting state is given by text from jade.div, if any
var config = {};
$(div).empty(); // all done with innards
if (text) {
try {
state = JSON.parse(text);
} catch(e) {
console.log('Error parsing configuration: '+e);
}
}
// now create the editor and pass along initial configuration
var j = new jade.Jade(div);
j.initialize(config);
}
}
};
};
// set up editor inside of div's with class "jade"
$(document).ready(function () {
$('.jade').each(function(index, div) {
var j = new jade_defs.jade();
jade_defs.services(j);
j.setup(div);
});
});
// notify user of unsaved changes
$(window).on('beforeunload',function () {
if ($('body').attr('data-dirty') !== undefined)
return 'You have unsaved changes on this page.';
return undefined;
});