this._editor().editable().setHtml(msg.value);
},
init: function (update, msg) {
this.update(update);
},
makeInit: function () {
return {
element: this.element,
tracker: this.trackerName,
value: this.getContent()
};
},
_change: function (e) {
if (inRemoteUpdate) {
return;
}
sendData({
tracker: this.trackerName,
element: this.element,
value: this.getContent()
});
},
_editor: function () {
return CKEDITOR.dom.element.get(this.element).getEditor();
},
getContent: function () {
return this._editor().getData();
}
});
CKEditor.scan = function () {
var result = [];
if (typeof CKEDITOR == "undefined") {
return;
}
var editorInstance;
for (var instanceIdentifier in CKEDITOR.instances) {
editorInstance = document.getElementById(instanceIdentifier) || document.getElementsByName(instanceIdentifier)[0];
if (editorInstance) {
result.push(editorInstance);
}
}
return $(result);
};
CKEditor.tracked = function (el) {
if (typeof CKEDITOR == "undefined") {
return false;
}
el = $(el)[0];
return !! (CKEDITOR.dom.element.get(el) && CKEDITOR.dom.element.get(el).getEditor());
};
TogetherJS.addTracker(CKEditor, true );
function buildTrackers() {
assert(! liveTrackers.length);
util.forEachAttr(editTrackers, function (TrackerClass) {
var els = TrackerClass.scan();
if (els) {
$.each(els, function () {
var tracker = new TrackerClass(this);
$(this).data("togetherjsHistory", ot.SimpleHistory(session.clientId, tracker.getContent(), 1));
liveTrackers.push(tracker);
});
}
});
}
function destroyTrackers() {
liveTrackers.forEach(function (tracker) {
tracker.destroy();
});
liveTrackers = [];
}
function elementTracked(el) {
var result = false;
util.forEachAttr(editTrackers, function (TrackerClass) {
if (TrackerClass.tracked(el)) {
result = true;
}
});
return result;
}
function getTracker(el, name) {
el = $(el)[0];
for (var i=0; i<liveTrackers.length; i++) {
var tracker = liveTrackers[i];
if (tracker.tracked(el)) {
assert((! name) || name == tracker.trackerName, "Expected to map to a tracker type", name, "but got", tracker.trackerName);
return tracker;
}
}
return null;
}
var TEXT_TYPES = (
"color date datetime datetime-local email " +
"tel text time week").split(/ /g);
function isText(el) {
el = $(el);
var tag = el.prop("tagName");
var type = (el.prop("type") || "text").toLowerCase();
if (tag == "TEXTAREA") {
return true;
}
if (tag == "INPUT" && TEXT_TYPES.indexOf(type) != -1) {
return true;
}
return false;
}
function getValue(el) {
el = $(el);
if (isCheckable(el)) {
return el.prop("checked");
} else {
return el.val();
}
}
function getElementType(el) {
el = $(el)[0];
if (el.tagName == "TEXTAREA") {
return "textarea";
}
if (el.tagName == "SELECT") {
return "select";
}
if (el.tagName == "INPUT") {
return (el.getAttribute("type") || "text").toLowerCase();
}
return "?";
}
function setValue(el, value) {
el = $(el);
var changed = false;
if (isCheckable(el)) {
var checked = !! el.prop("checked");
value = !! value;
if (checked != value) {
changed = true;
el.prop("checked", value);
}
} else {
if (el.val() != value) {
changed = true;
el.val(value);
}
}
if (changed) {
eventMaker.fireChange(el);
}
}
function maybeSendUpdate(element, history, tracker) {
var change = history.getNextToSend();
if (! change) {
return;
}
var msg = {
type: "form-update",
element: element,
"server-echo": true,
replace: {
id: change.id,
basis: change.basis,
delta: {
start: change.delta.start,
del: change.delta.del,
text: change.delta.text
}
}
};
if (tracker) {
msg.tracker = tracker;
}
session.send(msg);
}
session.hub.on("form-update", function (msg) {
if (! msg.sameUrl) {
return;
}
var el = $(elementFinder.findElement(msg.element));
var tracker;
if (msg.tracker) {
tracker = getTracker(el, msg.tracker);
assert(tracker);
}
var focusedEl = el[0].ownerDocument.activeElement;
var focusedElSelection;
if (isText(focusedEl)) {
focusedElSelection = [focusedEl.selectionStart, focusedEl.selectionEnd];
}
var selection;
if (isText(el)) {
selection = [el[0].selectionStart, el[0].selectionEnd];
}
var value;
if (msg.replace) {
var history = el.data("togetherjsHistory");
if (!history) {
console.warn("form update received for uninitialized form element");
return;
}
history.setSelection(selection);