forked from jsbin/jsbin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeta.js
More file actions
207 lines (171 loc) · 6 KB
/
beta.js
File metadata and controls
207 lines (171 loc) · 6 KB
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
// once these features are live, they come out of the jsbin beta box
(function () {
var $body = $('body'),
$document = $(document),
forbindDFD = $.Deferred(),
forbindPromise = forbindDFD.promise();
//= require "stream"
this.home = function (name, key) {
if (!key) {
console.log('A key is required to declare some sort of ownership.');
return false;
}
console.log('Checking for availability');
jsbin.settings.home = name; // will save later
$.ajax({
url: '/sethome',
data: { name: name, key: key },
type: 'post',
dataType: 'json',
success: function (data) {
// cookie is required to share with the server so we can do a redirect on new bin
if (data.ok) {
var date = new Date();
date.setTime(date.getTime()+(365*24*60*60*1000)); // set for a year
document.cookie = 'home=' + name + '; expires=' + date.toGMTString() + '; path=/';
// also store encoded key - this is used to authenticate on save
// this key doesn't provide security, but provides a way to declare
// ownership and cockblocking others from taking a home name.
document.cookie = 'key=' + data.key + '; expires=' + date.toGMTString() + '; path=/';
console.log('Successfully tied this browser to "' + name + '".');
} else {
console.log('"' + name + '" has already been taken. Please either double check the key, or choose another home.');
}
}
});
return '...';
};
this.nojumpkeys = function () {
};
this.enableAPI = function () {
navigator.registerProtocolHandler('web+jsbin', jsbin.root + '?api=%s', 'JS Bin API');
};
// popout live remoting
this.popout = function () {
var last = {};
forbindPromise.done(function () {
var key = sessionStorage.remotekey || (Math.abs(~~(Math.random()*+new Date))).toString(32);
// sessionStorage.remotekey = key;
function changes(lang, code) {
var msg = {},
diff,
patch,
result;
if (last[lang] === undefined) {
msg.text = code;
msg.diff = false;
} else {
diff = new diff_match_patch();
// 1. get diffs
patch = diff.patch_make(last[lang], code);
// 2. apply patch to old javascript
result = diff.patch_apply(patch, last[lang]);
// 3. if it matches, then send diff
if (result[0] == code) {
msg.text = diff.patch_toText(patch);
msg.diff = true;
// 4. otherwise, send entire code
} else {
msg.text = code;
msg.diff = false;
}
}
last[lang] = code;
return msg;
}
function capture() {
var javascript = editors.javascript.getCode(),
html = editors.html.getCode(),
changed = false,
cursor,
msg = {};
msg.javascript = changes('javascript', javascript);
msg.html = changes('html', html);
if (msg.html.text || msg.javascript.text) {
msg.panel = getFocusedPanel();
cursor = editors[msg.panel].getCursor();
msg.line = cursor.line;
msg.ch = cursor.ch;
forbind.send(msg);
}
}
if (typeof window.forbind !== 'undefined') {
$('a.popout').click(function () {
if (!this.search) {
// hide the real-time view now that we've popped out
$('#showlive').removeAttr('checked')[0].checked = false;
updatePanel('live', false);
forbind.on({
join: function (event) {
if (event.isme) {
console.log('forbind ready');
capture();
} else {
console.log('New remote view: ', event.user);
}
$document.bind('codeChange', throttle(capture, 250));
}
});
this.search = '?' + key;
forbind.debug = false;
forbind.create(key);
}
});
}
}).fail(function () {
console.log('Förbind is not available, therefore we can\'t start the popout. Sorry :(');
});
};
this.diff = function (revision) {
var url = window.location.pathname;
url = url.split('/');
var thisRev = url.pop();
if (thisRev == 'edit') thisRev = url.pop(); // should always happen
if (!revision) {
revision = thisRev;
revision--;
} else {
revision *= 1;
}
if (!isNaN(revision) && revision > 0) {
$.ajax({
url: url.join('/') + '/' + revision + '/source',
dataType: 'json',
success: function (data) {
var diff = new diff_match_patch(),
patch = diff.patch_make(data.javascript, editors.javascript.getCode()),
patchText = diff.patch_toText(patch);
if (patchText) {
console.log('--- javascript diff ---');
console.log(decodeURIComponent(patchText));
}
diff = new diff_match_patch();
patch = diff.patch_make(data.html, editors.html.getCode());
patchText = diff.patch_toText(patch);
if (patchText) {
console.log('--- html diff ---');
console.log(decodeURIComponent(patchText));
}
}
});
} else {
console.log('requires a revision number to test against');
}
};
this.on = function () {
localStorage.setItem('beta', 'true');
$body.addClass('beta');
this.popout();
};
this.off = function () {
localStorage.removeItem('beta');
$body.removeClass('beta');
};
this.active = localStorage.getItem('beta') == 'true' || false;
if (this.active) this.on();
// lazy cookie parsing.
try {
jsbin.settings.home = document.cookie.split('home=')[1].split(';')[0];
document.title = jsbin.settings.home + '@' + document.title;
} catch (e) {};
}).call(jsbin);