refs, complex saveOperation' and validateSavedOperations`
This commit is contained in:
@@ -154,32 +154,67 @@ module.exports = function() {
|
||||
return json;
|
||||
};
|
||||
|
||||
Operation.prototype.saveOperation = function(name, op) {
|
||||
Operation.prototype.saveOperation = function(name, op, base) {
|
||||
var base1, dest, j, last_path, len, path, paths;
|
||||
if (base == null) {
|
||||
base = "this";
|
||||
}
|
||||
if (op == null) {
|
||||
|
||||
} else if ((op.execute != null) || !((op.op_number != null) && (op.creator != null))) {
|
||||
return this[name] = op;
|
||||
if (base === "this") {
|
||||
return this[name] = op;
|
||||
} else {
|
||||
dest = this[base];
|
||||
paths = name.split("/");
|
||||
last_path = paths.pop();
|
||||
for (j = 0, len = paths.length; j < len; j++) {
|
||||
path = paths[j];
|
||||
dest = dest[path];
|
||||
}
|
||||
return dest[last_path] = op;
|
||||
}
|
||||
} else {
|
||||
if (this.unchecked == null) {
|
||||
this.unchecked = {};
|
||||
}
|
||||
return this.unchecked[name] = op;
|
||||
if ((base1 = this.unchecked)[base] == null) {
|
||||
base1[base] = {};
|
||||
}
|
||||
return this.unchecked[base][name] = op;
|
||||
}
|
||||
};
|
||||
|
||||
Operation.prototype.validateSavedOperations = function() {
|
||||
var name, op, op_uid, ref, success, uninstantiated;
|
||||
var base, base_name, dest, j, last_path, len, name, op, op_uid, path, paths, ref, success, uninstantiated;
|
||||
uninstantiated = {};
|
||||
success = this;
|
||||
ref = this.unchecked;
|
||||
for (name in ref) {
|
||||
op_uid = ref[name];
|
||||
op = this.HB.getOperation(op_uid);
|
||||
if (op) {
|
||||
this[name] = op;
|
||||
} else {
|
||||
uninstantiated[name] = op_uid;
|
||||
success = false;
|
||||
for (base_name in ref) {
|
||||
base = ref[base_name];
|
||||
for (name in base) {
|
||||
op_uid = base[name];
|
||||
op = this.HB.getOperation(op_uid);
|
||||
if (op) {
|
||||
if (base_name === "this") {
|
||||
this[name] = op;
|
||||
} else {
|
||||
dest = this[base_name];
|
||||
paths = name.split("/");
|
||||
last_path = paths.pop();
|
||||
for (j = 0, len = paths.length; j < len; j++) {
|
||||
path = paths[j];
|
||||
dest = dest[path];
|
||||
}
|
||||
dest[last_path] = op;
|
||||
}
|
||||
} else {
|
||||
if (uninstantiated[base_name] == null) {
|
||||
uninstantiated[base_name] = {};
|
||||
}
|
||||
uninstantiated[base_name][name] = op_uid;
|
||||
success = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete this.unchecked;
|
||||
@@ -253,7 +288,8 @@ module.exports = function() {
|
||||
ops.Insert = (function(superClass) {
|
||||
extend(Insert, superClass);
|
||||
|
||||
function Insert(custom_type, content, parent, uid, prev_cl, next_cl, origin) {
|
||||
function Insert(custom_type, content, content_operations, parent, uid, prev_cl, next_cl, origin) {
|
||||
var name, op;
|
||||
if (content === void 0) {
|
||||
|
||||
} else if ((content != null) && (content.creator != null)) {
|
||||
@@ -261,6 +297,13 @@ module.exports = function() {
|
||||
} else {
|
||||
this.content = content;
|
||||
}
|
||||
if (content_operations != null) {
|
||||
this.content_operations = {};
|
||||
for (name in content_operations) {
|
||||
op = content_operations[name];
|
||||
this.saveOperation(name, op, 'content_operations');
|
||||
}
|
||||
}
|
||||
this.saveOperation('parent', parent);
|
||||
this.saveOperation('prev_cl', prev_cl);
|
||||
this.saveOperation('next_cl', next_cl);
|
||||
@@ -275,8 +318,28 @@ module.exports = function() {
|
||||
Insert.prototype.type = "Insert";
|
||||
|
||||
Insert.prototype.val = function() {
|
||||
if ((this.content != null) && (this.content.getCustomType != null)) {
|
||||
return this.content.getCustomType();
|
||||
var content, n, ref, ref1, v;
|
||||
if (this.content != null) {
|
||||
if (this.content.getCustomType != null) {
|
||||
return this.content.getCustomType();
|
||||
} else if (this.content.constructor === Object) {
|
||||
content = {};
|
||||
ref = this.content;
|
||||
for (n in ref) {
|
||||
v = ref[n];
|
||||
content[n] = v;
|
||||
}
|
||||
if (this.content_operations != null) {
|
||||
ref1 = this.content_operations;
|
||||
for (n in ref1) {
|
||||
v = ref1[n];
|
||||
content[n] = v;
|
||||
}
|
||||
}
|
||||
return content;
|
||||
} else {
|
||||
return this.content;
|
||||
}
|
||||
} else {
|
||||
return this.content;
|
||||
}
|
||||
@@ -380,14 +443,14 @@ module.exports = function() {
|
||||
};
|
||||
|
||||
Insert.prototype.execute = function() {
|
||||
var base, distance_to_origin, i, o;
|
||||
var base1, distance_to_origin, i, o;
|
||||
if (!this.validateSavedOperations()) {
|
||||
return false;
|
||||
} else {
|
||||
if (this.content instanceof ops.Operation) {
|
||||
this.content.insert_parent = this;
|
||||
if ((base = this.content).referenced_by == null) {
|
||||
base.referenced_by = 0;
|
||||
if ((base1 = this.content).referenced_by == null) {
|
||||
base1.referenced_by = 0;
|
||||
}
|
||||
this.content.referenced_by++;
|
||||
}
|
||||
@@ -461,7 +524,7 @@ module.exports = function() {
|
||||
};
|
||||
|
||||
Insert.prototype._encode = function(json) {
|
||||
var ref;
|
||||
var n, o, operations, ref, ref1;
|
||||
if (json == null) {
|
||||
json = {};
|
||||
}
|
||||
@@ -474,9 +537,18 @@ module.exports = function() {
|
||||
}
|
||||
json.parent = this.parent.getUid();
|
||||
if (((ref = this.content) != null ? ref.getUid : void 0) != null) {
|
||||
json['content'] = this.content.getUid();
|
||||
json.content = this.content.getUid();
|
||||
} else {
|
||||
json['content'] = JSON.stringify(this.content);
|
||||
json.content = JSON.stringify(this.content);
|
||||
}
|
||||
if (this.content_operations != null) {
|
||||
operations = {};
|
||||
ref1 = this.content_operations;
|
||||
for (n in ref1) {
|
||||
o = ref1[n];
|
||||
operations[n] = o.getUid();
|
||||
}
|
||||
json.content_operations = operations;
|
||||
}
|
||||
return Insert.__super__._encode.call(this, json);
|
||||
};
|
||||
@@ -485,12 +557,12 @@ module.exports = function() {
|
||||
|
||||
})(ops.Operation);
|
||||
ops.Insert.parse = function(json) {
|
||||
var content, next, origin, parent, prev, uid;
|
||||
content = json['content'], uid = json['uid'], prev = json['prev'], next = json['next'], origin = json['origin'], parent = json['parent'];
|
||||
var content, content_operations, next, origin, parent, prev, uid;
|
||||
content = json['content'], content_operations = json['content_operations'], uid = json['uid'], prev = json['prev'], next = json['next'], origin = json['origin'], parent = json['parent'];
|
||||
if (typeof content === "string") {
|
||||
content = JSON.parse(content);
|
||||
}
|
||||
return new this(null, content, parent, uid, prev, next, origin);
|
||||
return new this(null, content, content_operations, parent, uid, prev, next, origin);
|
||||
};
|
||||
ops.Delimiter = (function(superClass) {
|
||||
extend(Delimiter, superClass);
|
||||
|
||||
@@ -281,14 +281,14 @@ module.exports = function() {
|
||||
}
|
||||
left = right.prev_cl;
|
||||
if (contents instanceof ops.Operation) {
|
||||
(new ops.Insert(null, content, void 0, void 0, left, right)).execute();
|
||||
(new ops.Insert(null, content, null, void 0, void 0, left, right)).execute();
|
||||
} else {
|
||||
for (j = 0, len = contents.length; j < len; j++) {
|
||||
c = contents[j];
|
||||
if ((c != null) && (c._name != null) && (c._getModel != null)) {
|
||||
c = c._getModel(this.custom_types, this.operations);
|
||||
}
|
||||
tmp = (new ops.Insert(null, c, void 0, void 0, left, right)).execute();
|
||||
tmp = (new ops.Insert(null, c, null, void 0, void 0, left, right)).execute();
|
||||
left = tmp;
|
||||
}
|
||||
}
|
||||
@@ -337,7 +337,7 @@ module.exports = function() {
|
||||
position: op.getPosition(),
|
||||
object: this.getCustomType(),
|
||||
changedBy: op.uid.creator,
|
||||
value: getContentType(op.content)
|
||||
value: getContentType(op.val())
|
||||
}
|
||||
]);
|
||||
};
|
||||
@@ -366,17 +366,38 @@ module.exports = function() {
|
||||
ops.Composition = (function(superClass) {
|
||||
extend(Composition, superClass);
|
||||
|
||||
function Composition(custom_type, composition_value, uid, tmp_composition_ref) {
|
||||
function Composition(custom_type, _composition_value, composition_value_operations, uid, tmp_composition_ref) {
|
||||
var n, o;
|
||||
this._composition_value = _composition_value;
|
||||
console.log("delete this ...");
|
||||
this.constructed_with = [custom_type, this._composition_value, composition_value_operations, uid, tmp_composition_ref];
|
||||
Composition.__super__.constructor.call(this, custom_type, uid);
|
||||
if (tmp_composition_ref != null) {
|
||||
this.tmp_composition_ref = tmp_composition_ref;
|
||||
} else {
|
||||
this.composition_ref = this.end.prev_cl;
|
||||
}
|
||||
if (composition_value_operations != null) {
|
||||
this.composition_value_operations = {};
|
||||
for (n in composition_value_operations) {
|
||||
o = composition_value_operations[n];
|
||||
this.saveOperation(n, o, '_composition_value');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Composition.prototype.type = "Composition";
|
||||
|
||||
Composition.prototype.execute = function() {
|
||||
if (this.validateSavedOperations()) {
|
||||
this.getCustomType()._setCompositionValue(this._composition_value);
|
||||
delete this._composition_value;
|
||||
return Composition.__super__.execute.apply(this, arguments);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Composition.prototype.callOperationSpecificInsertEvents = function(op) {
|
||||
var o;
|
||||
if (this.tmp_composition_ref != null) {
|
||||
@@ -394,7 +415,7 @@ module.exports = function() {
|
||||
return;
|
||||
}
|
||||
if (this.composition_ref.next_cl === op) {
|
||||
op.undo_delta = this.getCustomType()._apply(op.content);
|
||||
op.undo_delta = this.getCustomType()._apply(op.val());
|
||||
} else {
|
||||
o = this.end.prev_cl;
|
||||
while (o !== op) {
|
||||
@@ -402,7 +423,7 @@ module.exports = function() {
|
||||
o = o.prev_cl;
|
||||
}
|
||||
while (o !== this.end) {
|
||||
o.undo_delta = this.getCustomType()._apply(o.content);
|
||||
o.undo_delta = this.getCustomType()._apply(o.val());
|
||||
o = o.next_cl;
|
||||
}
|
||||
}
|
||||
@@ -418,16 +439,26 @@ module.exports = function() {
|
||||
|
||||
Composition.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {};
|
||||
|
||||
Composition.prototype.applyDelta = function(delta) {
|
||||
(new ops.Insert(null, delta, this, null, this.end.prev_cl, this.end)).execute();
|
||||
Composition.prototype.applyDelta = function(delta, operations) {
|
||||
(new ops.Insert(null, delta, operations, this, null, this.end.prev_cl, this.end)).execute();
|
||||
return void 0;
|
||||
};
|
||||
|
||||
Composition.prototype._encode = function(json) {
|
||||
var custom, n, o, ref;
|
||||
if (json == null) {
|
||||
json = {};
|
||||
}
|
||||
json.composition_value = JSON.stringify(this.getCustomType()._getCompositionValue());
|
||||
custom = this.getCustomType()._getCompositionValue();
|
||||
json.composition_value = custom.composition_value;
|
||||
if (custom.composition_value_operations != null) {
|
||||
json.composition_value_operations = {};
|
||||
ref = custom.composition_value_operations;
|
||||
for (n in ref) {
|
||||
o = ref[n];
|
||||
json.composition_value_operations[n] = o.getUid();
|
||||
}
|
||||
}
|
||||
if (this.composition_ref != null) {
|
||||
json.composition_ref = this.composition_ref.getUid();
|
||||
} else {
|
||||
@@ -440,9 +471,9 @@ module.exports = function() {
|
||||
|
||||
})(ops.ListManager);
|
||||
ops.Composition.parse = function(json) {
|
||||
var composition_ref, composition_value, custom_type, uid;
|
||||
uid = json['uid'], custom_type = json['custom_type'], composition_value = json['composition_value'], composition_ref = json['composition_ref'];
|
||||
return new this(custom_type, JSON.parse(composition_value), uid, composition_ref);
|
||||
var composition_ref, composition_value, composition_value_operations, custom_type, uid;
|
||||
uid = json['uid'], custom_type = json['custom_type'], composition_value = json['composition_value'], composition_value_operations = json['composition_value_operations'], composition_ref = json['composition_ref'];
|
||||
return new this(custom_type, composition_value, composition_value_operations, uid, composition_ref);
|
||||
};
|
||||
ops.ReplaceManager = (function(superClass) {
|
||||
extend(ReplaceManager, superClass);
|
||||
@@ -516,7 +547,7 @@ module.exports = function() {
|
||||
ReplaceManager.prototype.replace = function(content, replaceable_uid) {
|
||||
var o, relp;
|
||||
o = this.getLastOperation();
|
||||
relp = (new ops.Insert(null, content, this, replaceable_uid, o, o.next_cl)).execute();
|
||||
relp = (new ops.Insert(null, content, null, this, replaceable_uid, o, o.next_cl)).execute();
|
||||
return void 0;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user