starting to refine the Replaceable type. Most types should be a replaceable.
This commit is contained in:
parent
2a644f2f0c
commit
fea6de3bf9
@ -1,158 +0,0 @@
|
||||
var text_ops_uninitialized,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
text_ops_uninitialized = require("./Text");
|
||||
|
||||
module.exports = function() {
|
||||
var ops, text_ops;
|
||||
text_ops = text_ops_uninitialized();
|
||||
ops = text_ops.operations;
|
||||
ops.Object = (function(_super) {
|
||||
__extends(Object, _super);
|
||||
|
||||
function Object() {
|
||||
return Object.__super__.constructor.apply(this, arguments);
|
||||
}
|
||||
|
||||
Object.prototype.type = "Object";
|
||||
|
||||
Object.prototype.applyDelete = function() {
|
||||
return Object.__super__.applyDelete.call(this);
|
||||
};
|
||||
|
||||
Object.prototype.cleanup = function() {
|
||||
return Object.__super__.cleanup.call(this);
|
||||
};
|
||||
|
||||
Object.prototype.toJson = function(transform_to_value) {
|
||||
var json, name, o, that, val;
|
||||
if (transform_to_value == null) {
|
||||
transform_to_value = false;
|
||||
}
|
||||
if ((this.bound_json == null) || (Object.observe == null) || true) {
|
||||
val = this.val();
|
||||
json = {};
|
||||
for (name in val) {
|
||||
o = val[name];
|
||||
if (o instanceof ops.Object) {
|
||||
json[name] = o.toJson(transform_to_value);
|
||||
} else if (o instanceof ops.ListManager) {
|
||||
json[name] = o.toJson(transform_to_value);
|
||||
} else if (transform_to_value && o instanceof ops.Operation) {
|
||||
json[name] = o.val();
|
||||
} else {
|
||||
json[name] = o;
|
||||
}
|
||||
}
|
||||
this.bound_json = json;
|
||||
if (Object.observe != null) {
|
||||
that = this;
|
||||
Object.observe(this.bound_json, function(events) {
|
||||
var event, _i, _len, _results;
|
||||
_results = [];
|
||||
for (_i = 0, _len = events.length; _i < _len; _i++) {
|
||||
event = events[_i];
|
||||
if ((event.changedBy == null) && (event.type === "add" || (event.type = "update"))) {
|
||||
_results.push(that.val(event.name, event.object[event.name]));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
});
|
||||
this.observe(function(events) {
|
||||
var event, notifier, oldVal, _i, _len, _results;
|
||||
_results = [];
|
||||
for (_i = 0, _len = events.length; _i < _len; _i++) {
|
||||
event = events[_i];
|
||||
if (event.created_ !== this.HB.getUserId()) {
|
||||
notifier = Object.getNotifier(that.bound_json);
|
||||
oldVal = that.bound_json[event.name];
|
||||
if (oldVal != null) {
|
||||
notifier.performChange('update', function() {
|
||||
return that.bound_json[event.name] = that.val(event.name);
|
||||
}, that.bound_json);
|
||||
_results.push(notifier.notify({
|
||||
object: that.bound_json,
|
||||
type: 'update',
|
||||
name: event.name,
|
||||
oldValue: oldVal,
|
||||
changedBy: event.changedBy
|
||||
}));
|
||||
} else {
|
||||
notifier.performChange('add', function() {
|
||||
return that.bound_json[event.name] = that.val(event.name);
|
||||
}, that.bound_json);
|
||||
_results.push(notifier.notify({
|
||||
object: that.bound_json,
|
||||
type: 'add',
|
||||
name: event.name,
|
||||
oldValue: oldVal,
|
||||
changedBy: event.changedBy
|
||||
}));
|
||||
}
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
});
|
||||
}
|
||||
}
|
||||
return this.bound_json;
|
||||
};
|
||||
|
||||
Object.prototype.val = function(name, content) {
|
||||
var args, i, o, type, _i, _ref;
|
||||
if ((name != null) && arguments.length > 1) {
|
||||
if ((content != null) && (content.constructor != null)) {
|
||||
type = ops[content.constructor.name];
|
||||
if ((type != null) && (type.create != null)) {
|
||||
args = [];
|
||||
for (i = _i = 1, _ref = arguments.length; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) {
|
||||
args.push(arguments[i]);
|
||||
}
|
||||
o = type.create.apply(null, args);
|
||||
return Object.__super__.val.call(this, name, o);
|
||||
} else {
|
||||
throw new Error("The " + content.constructor.name + "-type is not (yet) supported in Y.");
|
||||
}
|
||||
} else {
|
||||
return Object.__super__.val.call(this, name, content);
|
||||
}
|
||||
} else {
|
||||
return Object.__super__.val.call(this, name);
|
||||
}
|
||||
};
|
||||
|
||||
Object.prototype._encode = function() {
|
||||
return {
|
||||
'type': this.type,
|
||||
'uid': this.getUid()
|
||||
};
|
||||
};
|
||||
|
||||
return Object;
|
||||
|
||||
})(ops.MapManager);
|
||||
ops.Object.parse = function(json) {
|
||||
var uid;
|
||||
uid = json['uid'];
|
||||
return new this(uid);
|
||||
};
|
||||
ops.Object.create = function(content, mutable) {
|
||||
var json, n, o;
|
||||
json = new ops.Object().execute();
|
||||
for (n in content) {
|
||||
o = content[n];
|
||||
json.val(n, o, mutable);
|
||||
}
|
||||
return json;
|
||||
};
|
||||
ops.Number = {};
|
||||
ops.Number.create = function(content) {
|
||||
return content;
|
||||
};
|
||||
return text_ops;
|
||||
};
|
@ -1,321 +0,0 @@
|
||||
var structured_ops_uninitialized,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
structured_ops_uninitialized = require("./Structured");
|
||||
|
||||
module.exports = function() {
|
||||
var ops, structured_ops;
|
||||
structured_ops = structured_ops_uninitialized();
|
||||
ops = structured_ops.operations;
|
||||
ops.String = (function(_super) {
|
||||
__extends(String, _super);
|
||||
|
||||
function String(uid) {
|
||||
this.textfields = [];
|
||||
String.__super__.constructor.call(this, uid);
|
||||
}
|
||||
|
||||
String.prototype.type = "String";
|
||||
|
||||
String.prototype.val = function() {
|
||||
return this.fold("", function(left, o) {
|
||||
return left + o.val();
|
||||
});
|
||||
};
|
||||
|
||||
String.prototype.toString = function() {
|
||||
return this.val();
|
||||
};
|
||||
|
||||
String.prototype.insert = function(position, content, options) {
|
||||
var ith;
|
||||
ith = this.getOperationByPosition(position);
|
||||
return this.insertAfter(ith, content, options);
|
||||
};
|
||||
|
||||
String.prototype.bind = function(textfield, dom_root) {
|
||||
var createRange, creator_token, t, word, writeContent, writeRange, _i, _len, _ref;
|
||||
if (dom_root == null) {
|
||||
dom_root = window;
|
||||
}
|
||||
if (dom_root.getSelection == null) {
|
||||
dom_root = window;
|
||||
}
|
||||
_ref = this.textfields;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
t = _ref[_i];
|
||||
if (t === textfield) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
creator_token = false;
|
||||
word = this;
|
||||
textfield.value = this.val();
|
||||
this.textfields.push(textfield);
|
||||
if ((textfield.selectionStart != null) && (textfield.setSelectionRange != null)) {
|
||||
createRange = function(fix) {
|
||||
var left, right;
|
||||
left = textfield.selectionStart;
|
||||
right = textfield.selectionEnd;
|
||||
if (fix != null) {
|
||||
left = fix(left);
|
||||
right = fix(right);
|
||||
}
|
||||
return {
|
||||
left: left,
|
||||
right: right
|
||||
};
|
||||
};
|
||||
writeRange = function(range) {
|
||||
writeContent(word.val());
|
||||
return textfield.setSelectionRange(range.left, range.right);
|
||||
};
|
||||
writeContent = function(content) {
|
||||
return textfield.value = content;
|
||||
};
|
||||
} else {
|
||||
createRange = function(fix) {
|
||||
var clength, edited_element, range, s;
|
||||
range = {};
|
||||
s = dom_root.getSelection();
|
||||
clength = textfield.textContent.length;
|
||||
range.left = Math.min(s.anchorOffset, clength);
|
||||
range.right = Math.min(s.focusOffset, clength);
|
||||
if (fix != null) {
|
||||
range.left = fix(range.left);
|
||||
range.right = fix(range.right);
|
||||
}
|
||||
edited_element = s.focusNode;
|
||||
if (edited_element === textfield || edited_element === textfield.childNodes[0]) {
|
||||
range.isReal = true;
|
||||
} else {
|
||||
range.isReal = false;
|
||||
}
|
||||
return range;
|
||||
};
|
||||
writeRange = function(range) {
|
||||
var r, s, textnode;
|
||||
writeContent(word.val());
|
||||
textnode = textfield.childNodes[0];
|
||||
if (range.isReal && (textnode != null)) {
|
||||
if (range.left < 0) {
|
||||
range.left = 0;
|
||||
}
|
||||
range.right = Math.max(range.left, range.right);
|
||||
if (range.right > textnode.length) {
|
||||
range.right = textnode.length;
|
||||
}
|
||||
range.left = Math.min(range.left, range.right);
|
||||
r = document.createRange();
|
||||
r.setStart(textnode, range.left);
|
||||
r.setEnd(textnode, range.right);
|
||||
s = window.getSelection();
|
||||
s.removeAllRanges();
|
||||
return s.addRange(r);
|
||||
}
|
||||
};
|
||||
writeContent = function(content) {
|
||||
var c, content_array, i, _j, _len1, _results;
|
||||
content_array = content.replace(new RegExp("\n", 'g'), " ").split(" ");
|
||||
textfield.innerText = "";
|
||||
_results = [];
|
||||
for (i = _j = 0, _len1 = content_array.length; _j < _len1; i = ++_j) {
|
||||
c = content_array[i];
|
||||
textfield.innerText += c;
|
||||
if (i !== content_array.length - 1) {
|
||||
_results.push(textfield.innerHTML += ' ');
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
}
|
||||
writeContent(this.val());
|
||||
this.observe(function(events) {
|
||||
var event, fix, o_pos, r, _j, _len1, _results;
|
||||
_results = [];
|
||||
for (_j = 0, _len1 = events.length; _j < _len1; _j++) {
|
||||
event = events[_j];
|
||||
if (!creator_token) {
|
||||
if (event.type === "insert") {
|
||||
o_pos = event.position;
|
||||
fix = function(cursor) {
|
||||
if (cursor <= o_pos) {
|
||||
return cursor;
|
||||
} else {
|
||||
cursor += 1;
|
||||
return cursor;
|
||||
}
|
||||
};
|
||||
r = createRange(fix);
|
||||
_results.push(writeRange(r));
|
||||
} else if (event.type === "delete") {
|
||||
o_pos = event.position;
|
||||
fix = function(cursor) {
|
||||
if (cursor < o_pos) {
|
||||
return cursor;
|
||||
} else {
|
||||
cursor -= 1;
|
||||
return cursor;
|
||||
}
|
||||
};
|
||||
r = createRange(fix);
|
||||
_results.push(writeRange(r));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
});
|
||||
textfield.onkeypress = function(event) {
|
||||
var char, diff, pos, r;
|
||||
if (word.is_deleted) {
|
||||
textfield.onkeypress = null;
|
||||
return true;
|
||||
}
|
||||
creator_token = true;
|
||||
char = null;
|
||||
if (event.keyCode === 13) {
|
||||
char = '\n';
|
||||
} else if (event.key != null) {
|
||||
if (event.charCode === 32) {
|
||||
char = " ";
|
||||
} else {
|
||||
char = event.key;
|
||||
}
|
||||
} else {
|
||||
char = window.String.fromCharCode(event.keyCode);
|
||||
}
|
||||
if (char.length > 1) {
|
||||
return true;
|
||||
} else if (char.length > 0) {
|
||||
r = createRange();
|
||||
pos = Math.min(r.left, r.right);
|
||||
diff = Math.abs(r.right - r.left);
|
||||
word["delete"](pos, diff);
|
||||
word.insert(pos, char);
|
||||
r.left = pos + char.length;
|
||||
r.right = r.left;
|
||||
writeRange(r);
|
||||
}
|
||||
event.preventDefault();
|
||||
creator_token = false;
|
||||
return false;
|
||||
};
|
||||
textfield.onpaste = function(event) {
|
||||
if (word.is_deleted) {
|
||||
textfield.onpaste = null;
|
||||
return true;
|
||||
}
|
||||
return event.preventDefault();
|
||||
};
|
||||
textfield.oncut = function(event) {
|
||||
if (word.is_deleted) {
|
||||
textfield.oncut = null;
|
||||
return true;
|
||||
}
|
||||
return event.preventDefault();
|
||||
};
|
||||
return textfield.onkeydown = function(event) {
|
||||
var del_length, diff, new_pos, pos, r, val;
|
||||
creator_token = true;
|
||||
if (word.is_deleted) {
|
||||
textfield.onkeydown = null;
|
||||
return true;
|
||||
}
|
||||
r = createRange();
|
||||
pos = Math.min(r.left, r.right, word.val().length);
|
||||
diff = Math.abs(r.left - r.right);
|
||||
if ((event.keyCode != null) && event.keyCode === 8) {
|
||||
if (diff > 0) {
|
||||
word["delete"](pos, diff);
|
||||
r.left = pos;
|
||||
r.right = pos;
|
||||
writeRange(r);
|
||||
} else {
|
||||
if ((event.ctrlKey != null) && event.ctrlKey) {
|
||||
val = word.val();
|
||||
new_pos = pos;
|
||||
del_length = 0;
|
||||
if (pos > 0) {
|
||||
new_pos--;
|
||||
del_length++;
|
||||
}
|
||||
while (new_pos > 0 && val[new_pos] !== " " && val[new_pos] !== '\n') {
|
||||
new_pos--;
|
||||
del_length++;
|
||||
}
|
||||
word["delete"](new_pos, pos - new_pos);
|
||||
r.left = new_pos;
|
||||
r.right = new_pos;
|
||||
writeRange(r);
|
||||
} else {
|
||||
if (pos > 0) {
|
||||
word["delete"](pos - 1, 1);
|
||||
r.left = pos - 1;
|
||||
r.right = pos - 1;
|
||||
writeRange(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
event.preventDefault();
|
||||
creator_token = false;
|
||||
return false;
|
||||
} else if ((event.keyCode != null) && event.keyCode === 46) {
|
||||
if (diff > 0) {
|
||||
word["delete"](pos, diff);
|
||||
r.left = pos;
|
||||
r.right = pos;
|
||||
writeRange(r);
|
||||
} else {
|
||||
word["delete"](pos, 1);
|
||||
r.left = pos;
|
||||
r.right = pos;
|
||||
writeRange(r);
|
||||
}
|
||||
event.preventDefault();
|
||||
creator_token = false;
|
||||
return false;
|
||||
} else {
|
||||
creator_token = false;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
String.prototype._encode = function() {
|
||||
var json;
|
||||
json = {
|
||||
'type': this.type,
|
||||
'uid': this.getUid()
|
||||
};
|
||||
return json;
|
||||
};
|
||||
|
||||
return String;
|
||||
|
||||
})(ops.ListManager);
|
||||
ops.String.parse = function(json) {
|
||||
var uid;
|
||||
uid = json['uid'];
|
||||
return new this(uid);
|
||||
};
|
||||
ops.String.create = function(content, mutable) {
|
||||
var word;
|
||||
if (mutable === "mutable") {
|
||||
word = new ops.String().execute();
|
||||
word.insert(0, content);
|
||||
return word;
|
||||
} else if ((mutable == null) || (mutable === "immutable")) {
|
||||
return content;
|
||||
} else {
|
||||
throw new Error("Specify either \"mutable\" or \"immutable\"!!");
|
||||
}
|
||||
};
|
||||
return structured_ops;
|
||||
};
|
404
build/node/Types/DOM.js
Normal file
404
build/node/Types/DOM.js
Normal file
@ -0,0 +1,404 @@
|
||||
var dont_proxy, json_types_uninitialized, proxy_token, _proxy,
|
||||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
json_types_uninitialized = require("./JsonTypes");
|
||||
|
||||
proxy_token = false;
|
||||
|
||||
dont_proxy = function(f) {
|
||||
var e;
|
||||
proxy_token = true;
|
||||
try {
|
||||
f();
|
||||
} catch (_error) {
|
||||
e = _error;
|
||||
proxy_token = false;
|
||||
throw new Error(e);
|
||||
}
|
||||
return proxy_token = false;
|
||||
};
|
||||
|
||||
_proxy = function(f_name, f) {
|
||||
var old_f;
|
||||
old_f = this[f_name];
|
||||
if (old_f != null) {
|
||||
return this[f_name] = function() {
|
||||
var args, that, _ref;
|
||||
if (!proxy_token && !((_ref = this._y) != null ? _ref.isDeleted() : void 0)) {
|
||||
that = this;
|
||||
args = arguments;
|
||||
return dont_proxy(function() {
|
||||
f.apply(that, args);
|
||||
return old_f.apply(that, args);
|
||||
});
|
||||
} else {
|
||||
return old_f.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof Element !== "undefined" && Element !== null) {
|
||||
Element.prototype._proxy = _proxy;
|
||||
}
|
||||
|
||||
module.exports = function(HB) {
|
||||
var TextNodeType, XmlType, json_types, parser, types;
|
||||
json_types = json_types_uninitialized(HB);
|
||||
types = json_types.types;
|
||||
parser = json_types.parser;
|
||||
XmlType = (function(_super) {
|
||||
__extends(XmlType, _super);
|
||||
|
||||
function XmlType(uid, _at_tagname, attributes, elements, _at_xml) {
|
||||
var attr, d, element, i, n, word, _i, _j, _len, _ref, _ref1, _ref2;
|
||||
this.tagname = _at_tagname;
|
||||
this.xml = _at_xml;
|
||||
|
||||
/* In case you make this instanceof Insert again
|
||||
if prev? and (not next?) and prev.type?
|
||||
* adjust what you actually mean. you want to insert after prev, then
|
||||
* next is not defined. but we only insert after non-deleted elements.
|
||||
* This is also handled in TextInsert.
|
||||
while prev.isDeleted()
|
||||
prev = prev.prev_cl
|
||||
next = prev.next_cl
|
||||
*/
|
||||
XmlType.__super__.constructor.call(this, uid);
|
||||
if (((_ref = this.xml) != null ? _ref._y : void 0) != null) {
|
||||
d = new types.Delete(void 0, this.xml._y);
|
||||
HB.addOperation(d).execute();
|
||||
this.xml._y = null;
|
||||
}
|
||||
if ((attributes != null) && (elements != null)) {
|
||||
this.saveOperation('attributes', attributes);
|
||||
this.saveOperation('elements', elements);
|
||||
} else if ((attributes == null) && (elements == null)) {
|
||||
this.attributes = new types.JsonType();
|
||||
this.attributes.setMutableDefault('immutable');
|
||||
HB.addOperation(this.attributes).execute();
|
||||
this.elements = new types.WordType();
|
||||
this.elements.parent = this;
|
||||
HB.addOperation(this.elements).execute();
|
||||
} else {
|
||||
throw new Error("Either define attribute and elements both, or none of them");
|
||||
}
|
||||
if (this.xml != null) {
|
||||
this.tagname = this.xml.tagName;
|
||||
for (i = _i = 0, _ref1 = this.xml.attributes.length; 0 <= _ref1 ? _i < _ref1 : _i > _ref1; i = 0 <= _ref1 ? ++_i : --_i) {
|
||||
attr = xml.attributes[i];
|
||||
this.attributes.val(attr.name, attr.value);
|
||||
}
|
||||
_ref2 = this.xml.childNodes;
|
||||
for (_j = 0, _len = _ref2.length; _j < _len; _j++) {
|
||||
n = _ref2[_j];
|
||||
if (n.nodeType === n.TEXT_NODE) {
|
||||
word = new TextNodeType(void 0, n);
|
||||
HB.addOperation(word).execute();
|
||||
this.elements.push(word);
|
||||
} else if (n.nodeType === n.ELEMENT_NODE) {
|
||||
element = new XmlType(void 0, void 0, void 0, void 0, n);
|
||||
HB.addOperation(element).execute();
|
||||
this.elements.push(element);
|
||||
} else {
|
||||
throw new Error("I don't know Node-type " + n.nodeType + "!!");
|
||||
}
|
||||
}
|
||||
this.setXmlProxy();
|
||||
}
|
||||
void 0;
|
||||
}
|
||||
|
||||
XmlType.prototype.type = "XmlType";
|
||||
|
||||
XmlType.prototype.applyDelete = function(op) {
|
||||
if ((this.insert_parent != null) && !this.insert_parent.isDeleted()) {
|
||||
return this.insert_parent.applyDelete(op);
|
||||
} else {
|
||||
this.attributes.applyDelete();
|
||||
this.elements.applyDelete();
|
||||
return XmlType.__super__.applyDelete.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
XmlType.prototype.cleanup = function() {
|
||||
return XmlType.__super__.cleanup.call(this);
|
||||
};
|
||||
|
||||
XmlType.prototype.setXmlProxy = function() {
|
||||
var findNode, insertBefore, removeChild, renewClassList, that;
|
||||
this.xml._y = this;
|
||||
that = this;
|
||||
this.elements.on('insert', function(event, op) {
|
||||
var newNode, right, rightNode;
|
||||
if (op.creator !== HB.getUserId() && this === that.elements) {
|
||||
newNode = op.content.val();
|
||||
right = op.next_cl;
|
||||
while ((right != null) && right.isDeleted()) {
|
||||
right = right.next_cl;
|
||||
}
|
||||
rightNode = null;
|
||||
if (right.type !== 'Delimiter') {
|
||||
rightNode = right.val().val();
|
||||
}
|
||||
return dont_proxy(function() {
|
||||
return that.xml.insertBefore(newNode, rightNode);
|
||||
});
|
||||
}
|
||||
});
|
||||
this.elements.on('delete', function(event, op) {
|
||||
var del_op, deleted;
|
||||
del_op = op.deleted_by[0];
|
||||
if ((del_op != null) && del_op.creator !== HB.getUserId() && this === that.elements) {
|
||||
deleted = op.content.val();
|
||||
return dont_proxy(function() {
|
||||
return that.xml.removeChild(deleted);
|
||||
});
|
||||
}
|
||||
});
|
||||
this.attributes.on(['add', 'update'], function(event, property_name, op) {
|
||||
if (op.creator !== HB.getUserId() && this === that.attributes) {
|
||||
return dont_proxy(function() {
|
||||
var newval;
|
||||
newval = op.val().val();
|
||||
if (newval != null) {
|
||||
return that.xml.setAttribute(property_name, op.val().val());
|
||||
} else {
|
||||
return that.xml.removeAttribute(property_name);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
findNode = function(child) {
|
||||
var elem;
|
||||
if (child == null) {
|
||||
throw new Error("you must specify a parameter!");
|
||||
}
|
||||
child = child._y;
|
||||
elem = that.elements.beginning.next_cl;
|
||||
while (elem.type !== 'Delimiter' && elem.content !== child) {
|
||||
elem = elem.next_cl;
|
||||
}
|
||||
if (elem.type === 'Delimiter') {
|
||||
return false;
|
||||
} else {
|
||||
return elem;
|
||||
}
|
||||
};
|
||||
insertBefore = function(insertedNode_s, adjacentNode) {
|
||||
var child, element, inserted_nodes, next, prev, _results;
|
||||
next = null;
|
||||
if (adjacentNode != null) {
|
||||
next = findNode(adjacentNode);
|
||||
}
|
||||
prev = null;
|
||||
if (next) {
|
||||
prev = next.prev_cl;
|
||||
} else {
|
||||
prev = this._y.elements.end.prev_cl;
|
||||
while (prev.isDeleted()) {
|
||||
prev = prev.prev_cl;
|
||||
}
|
||||
}
|
||||
inserted_nodes = null;
|
||||
if (insertedNode_s.nodeType === insertedNode_s.DOCUMENT_FRAGMENT_NODE) {
|
||||
child = insertedNode_s.lastChild;
|
||||
_results = [];
|
||||
while (child != null) {
|
||||
element = new XmlType(void 0, void 0, void 0, void 0, child);
|
||||
HB.addOperation(element).execute();
|
||||
that.elements.insertAfter(prev, element);
|
||||
_results.push(child = child.previousSibling);
|
||||
}
|
||||
return _results;
|
||||
} else {
|
||||
element = new XmlType(void 0, void 0, void 0, void 0, insertedNode_s);
|
||||
HB.addOperation(element).execute();
|
||||
return that.elements.insertAfter(prev, element);
|
||||
}
|
||||
};
|
||||
this.xml._proxy('insertBefore', insertBefore);
|
||||
this.xml._proxy('appendChild', insertBefore);
|
||||
this.xml._proxy('removeAttribute', function(name) {
|
||||
return that.attributes.val(name, void 0);
|
||||
});
|
||||
this.xml._proxy('setAttribute', function(name, value) {
|
||||
return that.attributes.val(name, value);
|
||||
});
|
||||
renewClassList = function(newclass) {
|
||||
var dont_do_it, elem, value, _i, _len;
|
||||
dont_do_it = false;
|
||||
if (newclass != null) {
|
||||
for (_i = 0, _len = this.length; _i < _len; _i++) {
|
||||
elem = this[_i];
|
||||
if (newclass === elem) {
|
||||
dont_do_it = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
value = Array.prototype.join.call(this, " ");
|
||||
if ((newclass != null) && !dont_do_it) {
|
||||
value += " " + newclass;
|
||||
}
|
||||
return that.attributes.val('class', value);
|
||||
};
|
||||
_proxy.call(this.xml.classList, 'add', renewClassList);
|
||||
_proxy.call(this.xml.classList, 'remove', renewClassList);
|
||||
this.xml.__defineSetter__('className', function(val) {
|
||||
return this.setAttribute('class', val);
|
||||
});
|
||||
this.xml.__defineGetter__('className', function() {
|
||||
return that.attributes.val('class');
|
||||
});
|
||||
this.xml.__defineSetter__('textContent', function(val) {
|
||||
var elem, remove, text_node;
|
||||
elem = that.xml.firstChild;
|
||||
while (elem != null) {
|
||||
remove = elem;
|
||||
elem = elem.nextSibling;
|
||||
that.xml.removeChild(remove);
|
||||
}
|
||||
if (val !== "") {
|
||||
text_node = document.createTextNode(val);
|
||||
return that.xml.appendChild(text_node);
|
||||
}
|
||||
});
|
||||
removeChild = function(node) {
|
||||
var d, elem;
|
||||
elem = findNode(node);
|
||||
if (!elem) {
|
||||
throw new Error("You are only allowed to delete existing (direct) child elements!");
|
||||
}
|
||||
d = new types.Delete(void 0, elem);
|
||||
HB.addOperation(d).execute();
|
||||
return node._y = null;
|
||||
};
|
||||
this.xml._proxy('removeChild', removeChild);
|
||||
return this.xml._proxy('replaceChild', function(insertedNode, replacedNode) {
|
||||
insertBefore.call(this, insertedNode, replacedNode);
|
||||
return removeChild.call(this, replacedNode);
|
||||
});
|
||||
};
|
||||
|
||||
XmlType.prototype.val = function(enforce) {
|
||||
var a, attr, attr_name, e, n, text_node, value;
|
||||
if (enforce == null) {
|
||||
enforce = false;
|
||||
}
|
||||
if (typeof document !== "undefined" && document !== null) {
|
||||
if ((this.xml == null) || enforce) {
|
||||
this.xml = document.createElement(this.tagname);
|
||||
attr = this.attributes.val();
|
||||
for (attr_name in attr) {
|
||||
value = attr[attr_name];
|
||||
if (value != null) {
|
||||
a = document.createAttribute(attr_name);
|
||||
a.value = value;
|
||||
this.xml.setAttributeNode(a);
|
||||
}
|
||||
}
|
||||
e = this.elements.beginning.next_cl;
|
||||
while (e.type !== "Delimiter") {
|
||||
n = e.content;
|
||||
if (!e.isDeleted() && (e.content != null)) {
|
||||
if (n.type === "XmlType") {
|
||||
this.xml.appendChild(n.val(enforce));
|
||||
} else if (n.type === "TextNodeType") {
|
||||
text_node = n.val();
|
||||
this.xml.appendChild(text_node);
|
||||
} else {
|
||||
throw new Error("Internal structure cannot be transformed to dom");
|
||||
}
|
||||
}
|
||||
e = e.next_cl;
|
||||
}
|
||||
}
|
||||
this.setXmlProxy();
|
||||
return this.xml;
|
||||
}
|
||||
};
|
||||
|
||||
XmlType.prototype.execute = function() {
|
||||
return XmlType.__super__.execute.call(this);
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
if not @validateSavedOperations()
|
||||
return false
|
||||
else
|
||||
|
||||
return true
|
||||
*/
|
||||
|
||||
XmlType.prototype.getParent = function() {
|
||||
return this.parent;
|
||||
};
|
||||
|
||||
XmlType.prototype._encode = function() {
|
||||
var json;
|
||||
json = {
|
||||
'type': this.type,
|
||||
'attributes': this.attributes.getUid(),
|
||||
'elements': this.elements.getUid(),
|
||||
'tagname': this.tagname,
|
||||
'uid': this.getUid()
|
||||
};
|
||||
return json;
|
||||
};
|
||||
|
||||
return XmlType;
|
||||
|
||||
})(types.Insert);
|
||||
parser['XmlType'] = function(json) {
|
||||
var attributes, elements, tagname, uid;
|
||||
uid = json['uid'], attributes = json['attributes'], elements = json['elements'], tagname = json['tagname'];
|
||||
return new XmlType(uid, tagname, attributes, elements, void 0);
|
||||
};
|
||||
TextNodeType = (function(_super) {
|
||||
__extends(TextNodeType, _super);
|
||||
|
||||
function TextNodeType(uid, content) {
|
||||
var d;
|
||||
if (content._y != null) {
|
||||
d = new types.Delete(void 0, content._y);
|
||||
HB.addOperation(d).execute();
|
||||
content._y = null;
|
||||
}
|
||||
content._y = this;
|
||||
TextNodeType.__super__.constructor.call(this, uid, content);
|
||||
}
|
||||
|
||||
TextNodeType.prototype.applyDelete = function(op) {
|
||||
if ((this.insert_parent != null) && !this.insert_parent.isDeleted()) {
|
||||
return this.insert_parent.applyDelete(op);
|
||||
} else {
|
||||
return TextNodeType.__super__.applyDelete.apply(this, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
TextNodeType.prototype.type = "TextNodeType";
|
||||
|
||||
TextNodeType.prototype._encode = function() {
|
||||
var json;
|
||||
json = {
|
||||
'type': this.type,
|
||||
'uid': this.getUid(),
|
||||
'content': this.content.textContent
|
||||
};
|
||||
return json;
|
||||
};
|
||||
|
||||
return TextNodeType;
|
||||
|
||||
})(types.ImmutableObject);
|
||||
parser['TextNodeType'] = function(json) {
|
||||
var content, textnode, uid;
|
||||
uid = json['uid'], content = json['content'];
|
||||
textnode = document.createTextNode(content);
|
||||
return new TextNodeType(uid, textnode);
|
||||
};
|
||||
types['XmlType'] = XmlType;
|
||||
return json_types;
|
||||
};
|
91
build/node/Types/Xml.js
Normal file
91
build/node/Types/Xml.js
Normal file
@ -0,0 +1,91 @@
|
||||
var YXml;
|
||||
|
||||
YXml = (function() {
|
||||
function YXml(tagname, attributes, children, classes) {
|
||||
var a, a_name, c, c_name, _classes, _i, _len, _ref;
|
||||
if (attributes == null) {
|
||||
attributes = {};
|
||||
}
|
||||
if (children == null) {
|
||||
children = [];
|
||||
}
|
||||
if (classes == null) {
|
||||
classes = {};
|
||||
}
|
||||
this._xml = {};
|
||||
if (tagname == null) {
|
||||
throw new Error("You must specify a tagname");
|
||||
}
|
||||
this._xml.tagname = tagname;
|
||||
if (attributes.constructor !== Object) {
|
||||
throw new Error("The attributes must be specified as a Object");
|
||||
}
|
||||
for (a_name in attributes) {
|
||||
a = attributes[a_name];
|
||||
if (a.constructor !== String) {
|
||||
throw new Error("The attributes must be of type String!");
|
||||
}
|
||||
}
|
||||
this._xml.attributes = attributes;
|
||||
if (classes.constructor !== Object) {
|
||||
throw new Error("The classes must be specified as an Array");
|
||||
}
|
||||
this._xml.classes = classes;
|
||||
_classes = this._xml.attributes["class"];
|
||||
delete this._xml.attributes["class"];
|
||||
if (_classes != null) {
|
||||
_ref = _classes.split(" ");
|
||||
for (c = _i = 0, _len = _ref.length; _i < _len; c = ++_i) {
|
||||
c_name = _ref[c];
|
||||
if (c.length > 0) {
|
||||
this._xml.classes[c_name] = c;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (children.constructor !== Array) {
|
||||
throw new Error("You must specify the children as an Array that contains Strings and Y.Xml objects only");
|
||||
}
|
||||
}
|
||||
|
||||
YXml.prototype._name = "Xml";
|
||||
|
||||
YXml.prototype._getModel = function(types, ops) {
|
||||
if (this._model == null) {
|
||||
this._model = new ops.MapManager(this).execute();
|
||||
this._model.val("attributes", new Y.Object(this._xml.attributes)).val("classes", new Y.Object(this._xml.classes)).val("tagname", this._xml.tagname).val("children", this._xml.children);
|
||||
}
|
||||
delete this._xml;
|
||||
return this._model;
|
||||
};
|
||||
|
||||
YXml.prototype._setModel = function(_at__model) {
|
||||
this._model = _at__model;
|
||||
return delete this._xml;
|
||||
};
|
||||
|
||||
YXml.prototype.attr = function(name, value) {
|
||||
if (arguments.length > 1) {
|
||||
if (value.constructor !== Strings) {
|
||||
throw new Error("The attributes must be of type String!");
|
||||
}
|
||||
this._model.val("attributes").val(name, value);
|
||||
return this;
|
||||
} else if (arguments.length > 0) {
|
||||
return this._model.val("attributes").val(name);
|
||||
} else {
|
||||
return this._model.val("attributes").val();
|
||||
}
|
||||
};
|
||||
|
||||
YXml.prototype.addClass = function(name) {
|
||||
this._model.val("classes").val(name, true);
|
||||
return this;
|
||||
};
|
||||
|
||||
YXml.prototype.removeClass = function(name) {
|
||||
return this._model.val("classes")["delete"](name);
|
||||
};
|
||||
|
||||
return YXml;
|
||||
|
||||
})();
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
###
|
||||
|
||||
json_types_uninitialized = require "./JsonTypes"
|
||||
|
||||
# some dom implementations may call another dom.method that simulates the behavior of another.
|
||||
@ -364,4 +364,3 @@ module.exports = (HB)->
|
||||
types['XmlType'] = XmlType
|
||||
|
||||
json_types
|
||||
###
|
65
lib/Types/Xml.coffee
Normal file
65
lib/Types/Xml.coffee
Normal file
@ -0,0 +1,65 @@
|
||||
class YXml
|
||||
|
||||
constructor: (tagname, attributes = {}, children = [], classes = {})->
|
||||
@_xml = {}
|
||||
if not tagname?
|
||||
throw new Error "You must specify a tagname"
|
||||
@_xml.tagname = tagname
|
||||
if attributes.constructor isnt Object
|
||||
throw new Error "The attributes must be specified as a Object"
|
||||
for a_name, a of attributes
|
||||
if a.constructor isnt String
|
||||
throw new Error "The attributes must be of type String!"
|
||||
@_xml.attributes = attributes
|
||||
if classes.constructor isnt Object
|
||||
throw new Error "The classes must be specified as an Array"
|
||||
@_xml.classes = classes
|
||||
_classes = @_xml.attributes.class
|
||||
delete @_xml.attributes.class
|
||||
if _classes?
|
||||
for c_name, c in _classes.split(" ")
|
||||
if c.length > 0
|
||||
@_xml.classes[c_name] = c
|
||||
if children.constructor isnt Array
|
||||
throw new Error "You must specify the children as an Array that contains Strings and Y.Xml objects only"
|
||||
|
||||
_name: "Xml"
|
||||
|
||||
_getModel: (types, ops)->
|
||||
if not @_model?
|
||||
@_model = new ops.MapManager(@).execute()
|
||||
@_model.val("attributes", new Y.Object(@_xml.attributes))
|
||||
.val("classes", new Y.Object(@_xml.classes))
|
||||
.val("tagname", @_xml.tagname)
|
||||
.val("children", @_xml.children)
|
||||
|
||||
delete @_xml
|
||||
@_model
|
||||
|
||||
_setModel: (@_model)->
|
||||
delete @_xml
|
||||
|
||||
attr: (name, value)->
|
||||
if arguments.length > 1
|
||||
if value.constructor isnt Strings
|
||||
throw new Error "The attributes must be of type String!"
|
||||
@_model.val("attributes").val(name, value)
|
||||
@
|
||||
else if arguments.length > 0
|
||||
@_model.val("attributes").val(name)
|
||||
else
|
||||
@_model.val("attributes").val()
|
||||
|
||||
addClass: (name)->
|
||||
@_model.val("classes").val(name, true)
|
||||
@
|
||||
|
||||
removeClass: (name)->
|
||||
@_model.val("classes").delete(name)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -61,7 +61,6 @@ class JsonTest extends Test
|
||||
@getRandomRoot user_num, p
|
||||
|
||||
getGeneratingFunctions: (user_num)->
|
||||
types = @users[user_num]._model.operations
|
||||
super(user_num).concat [
|
||||
f : (y)=> # Delete Object Property
|
||||
list = for name, o of y.val()
|
||||
@ -78,29 +77,35 @@ class JsonTest extends Test
|
||||
f : (y)=> # SET PROPERTY TEXT
|
||||
y.val(@getRandomKey(), new Y.Text(@getRandomText()))
|
||||
types: [Y.Object]
|
||||
]
|
||||
###
|
||||
f : (y)=> # SET PROPERTY
|
||||
,
|
||||
f : (y)=> # SET PROPERTY (primitive)
|
||||
l = y.val().length
|
||||
y.val(_.random(0, l-1), @getRandomText())
|
||||
null
|
||||
types : [types.Array]
|
||||
, f : (y)=> # Delete Array Element
|
||||
types : [Y.List]
|
||||
,
|
||||
f : (y)=> # Delete Array Element
|
||||
list = y.val()
|
||||
if list.length > 0
|
||||
key = list[_random(0,list.length-1)]
|
||||
y.delete(key)
|
||||
types: [types.Array]
|
||||
, f : (y)=> # insert TEXT mutable
|
||||
i = _.random(0,list.length-1)
|
||||
y.delete(i)
|
||||
types: [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Object mutable
|
||||
l = y.val().length
|
||||
y.val(_.random(0, l-1), new Y.Object(@getRamdomObject()))
|
||||
types: [types.Array]
|
||||
, f : (y)=> # insert string
|
||||
types: [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Text mutable
|
||||
l = y.val().length
|
||||
y.val(_.random(0, l-1), @getRandomText())
|
||||
null
|
||||
types : [Y.Array]
|
||||
###
|
||||
y.val(_.random(0, l-1), new Y.Text(@getRandomText()))
|
||||
types : [Y.List]
|
||||
,
|
||||
f : (y)=> # insert Number (primitive object)
|
||||
l = y.val().length
|
||||
y.val(_.random(0,l-1), _.random(0,42))
|
||||
types : [Y.List]
|
||||
]
|
||||
|
||||
describe "JsonFramework", ->
|
||||
@timeout 500000
|
||||
|
@ -14,7 +14,7 @@ module.exports = class Test
|
||||
Y = Yjs
|
||||
@number_of_test_cases_multiplier = 1
|
||||
@repeat_this = 1 * @number_of_test_cases_multiplier
|
||||
@doSomething_amount = 123 * @number_of_test_cases_multiplier
|
||||
@doSomething_amount = 1230 * @number_of_test_cases_multiplier
|
||||
@number_of_engines = 5 + @number_of_test_cases_multiplier - 1
|
||||
|
||||
@time = 0 # denotes to the time when run was started
|
||||
@ -24,6 +24,9 @@ module.exports = class Test
|
||||
@debug = false
|
||||
|
||||
@reinitialize()
|
||||
for gf in @getGeneratingFunctions(0)
|
||||
if not (gf.types? and gf.f?)
|
||||
throw new Error "Generating Functions are not initialized properly!"
|
||||
|
||||
reinitialize: ()->
|
||||
@users = []
|
||||
|
Loading…
x
Reference in New Issue
Block a user