574 lines
17 KiB
JavaScript
574 lines
17 KiB
JavaScript
var basic_ops_uninitialized,
|
|
extend = 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;
|
|
|
|
basic_ops_uninitialized = require("./Basic");
|
|
|
|
module.exports = function() {
|
|
var basic_ops, ops;
|
|
basic_ops = basic_ops_uninitialized();
|
|
ops = basic_ops.operations;
|
|
ops.MapManager = (function(superClass) {
|
|
extend(MapManager, superClass);
|
|
|
|
function MapManager(custom_type, uid, content, content_operations) {
|
|
this._map = {};
|
|
console.log("delete this ...");
|
|
this.constructed_with = [custom_type, content, content_operations];
|
|
MapManager.__super__.constructor.call(this, custom_type, uid, content, content_operations);
|
|
}
|
|
|
|
MapManager.prototype.type = "MapManager";
|
|
|
|
MapManager.prototype.applyDelete = function() {
|
|
var name, p, ref;
|
|
ref = this._map;
|
|
for (name in ref) {
|
|
p = ref[name];
|
|
p.applyDelete();
|
|
}
|
|
return MapManager.__super__.applyDelete.call(this);
|
|
};
|
|
|
|
MapManager.prototype.cleanup = function() {
|
|
return MapManager.__super__.cleanup.call(this);
|
|
};
|
|
|
|
MapManager.prototype.map = function(f) {
|
|
var n, ref, v;
|
|
ref = this._map;
|
|
for (n in ref) {
|
|
v = ref[n];
|
|
f(n, v);
|
|
}
|
|
return void 0;
|
|
};
|
|
|
|
MapManager.prototype.val = function(name, content) {
|
|
var o, prop, ref, rep, res, result;
|
|
if (arguments.length > 1) {
|
|
if ((content != null) && (content._getModel != null)) {
|
|
rep = content._getModel(this.custom_types, this.operations);
|
|
} else {
|
|
rep = content;
|
|
}
|
|
this.retrieveSub(name).replace(rep);
|
|
return this.getCustomType();
|
|
} else if (name != null) {
|
|
prop = this._map[name];
|
|
if ((prop != null) && !prop.isContentDeleted()) {
|
|
res = prop.val();
|
|
if (res instanceof ops.Operation) {
|
|
return res.getCustomType();
|
|
} else {
|
|
return res;
|
|
}
|
|
} else {
|
|
return void 0;
|
|
}
|
|
} else {
|
|
result = {};
|
|
ref = this._map;
|
|
for (name in ref) {
|
|
o = ref[name];
|
|
if (!o.isContentDeleted()) {
|
|
result[name] = o.val();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
};
|
|
|
|
MapManager.prototype["delete"] = function(name) {
|
|
var ref;
|
|
if ((ref = this._map[name]) != null) {
|
|
ref.deleteContent();
|
|
}
|
|
return this;
|
|
};
|
|
|
|
MapManager.prototype.retrieveSub = function(property_name) {
|
|
var event_properties, event_this, rm, rm_uid;
|
|
if (this._map[property_name] == null) {
|
|
event_properties = {
|
|
name: property_name
|
|
};
|
|
event_this = this;
|
|
rm_uid = {
|
|
noOperation: true,
|
|
sub: property_name,
|
|
alt: this
|
|
};
|
|
rm = new ops.ReplaceManager(null, event_properties, event_this, rm_uid);
|
|
this._map[property_name] = rm;
|
|
rm.setParent(this, property_name);
|
|
rm.execute();
|
|
}
|
|
return this._map[property_name];
|
|
};
|
|
|
|
return MapManager;
|
|
|
|
})(ops.Operation);
|
|
ops.MapManager.parse = function(json) {
|
|
var content, content_operations, custom_type, uid;
|
|
uid = json['uid'], custom_type = json['custom_type'], content = json['content'], content_operations = json['content_operations'];
|
|
return new this(custom_type, uid, content, content_operations);
|
|
};
|
|
ops.ListManager = (function(superClass) {
|
|
extend(ListManager, superClass);
|
|
|
|
function ListManager(custom_type, uid, content, content_operations) {
|
|
this.beginning = new ops.Delimiter(void 0, void 0);
|
|
this.end = new ops.Delimiter(this.beginning, void 0);
|
|
this.beginning.next_cl = this.end;
|
|
this.beginning.execute();
|
|
this.end.execute();
|
|
ListManager.__super__.constructor.call(this, custom_type, uid, content, content_operations);
|
|
}
|
|
|
|
ListManager.prototype.type = "ListManager";
|
|
|
|
ListManager.prototype.applyDelete = function() {
|
|
var o;
|
|
o = this.beginning;
|
|
while (o != null) {
|
|
o.applyDelete();
|
|
o = o.next_cl;
|
|
}
|
|
return ListManager.__super__.applyDelete.call(this);
|
|
};
|
|
|
|
ListManager.prototype.cleanup = function() {
|
|
return ListManager.__super__.cleanup.call(this);
|
|
};
|
|
|
|
ListManager.prototype.toJson = function(transform_to_value) {
|
|
var i, j, len, o, results, val;
|
|
if (transform_to_value == null) {
|
|
transform_to_value = false;
|
|
}
|
|
val = this.val();
|
|
results = [];
|
|
for (o = j = 0, len = val.length; j < len; o = ++j) {
|
|
i = val[o];
|
|
if (o instanceof ops.Object) {
|
|
results.push(o.toJson(transform_to_value));
|
|
} else if (o instanceof ops.ListManager) {
|
|
results.push(o.toJson(transform_to_value));
|
|
} else if (transform_to_value && o instanceof ops.Operation) {
|
|
results.push(o.val());
|
|
} else {
|
|
results.push(o);
|
|
}
|
|
}
|
|
return results;
|
|
};
|
|
|
|
ListManager.prototype.execute = function() {
|
|
if (this.validateSavedOperations()) {
|
|
this.beginning.setParent(this);
|
|
this.end.setParent(this);
|
|
return ListManager.__super__.execute.apply(this, arguments);
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
ListManager.prototype.getLastOperation = function() {
|
|
return this.end.prev_cl;
|
|
};
|
|
|
|
ListManager.prototype.getFirstOperation = function() {
|
|
return this.beginning.next_cl;
|
|
};
|
|
|
|
ListManager.prototype.toArray = function() {
|
|
var o, result;
|
|
o = this.beginning.next_cl;
|
|
result = [];
|
|
while (o !== this.end) {
|
|
if (!o.is_deleted) {
|
|
result.push(o.val());
|
|
}
|
|
o = o.next_cl;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
ListManager.prototype.map = function(f) {
|
|
var o, result;
|
|
o = this.beginning.next_cl;
|
|
result = [];
|
|
while (o !== this.end) {
|
|
if (!o.is_deleted) {
|
|
result.push(f(o));
|
|
}
|
|
o = o.next_cl;
|
|
}
|
|
return result;
|
|
};
|
|
|
|
ListManager.prototype.fold = function(init, f) {
|
|
var o;
|
|
o = this.beginning.next_cl;
|
|
while (o !== this.end) {
|
|
if (!o.is_deleted) {
|
|
init = f(init, o);
|
|
}
|
|
o = o.next_cl;
|
|
}
|
|
return init;
|
|
};
|
|
|
|
ListManager.prototype.val = function(pos) {
|
|
var o;
|
|
if (pos != null) {
|
|
o = this.getOperationByPosition(pos + 1);
|
|
if (!(o instanceof ops.Delimiter)) {
|
|
return o.val();
|
|
} else {
|
|
throw new Error("this position does not exist");
|
|
}
|
|
} else {
|
|
return this.toArray();
|
|
}
|
|
};
|
|
|
|
ListManager.prototype.ref = function(pos) {
|
|
var o;
|
|
if (pos != null) {
|
|
o = this.getOperationByPosition(pos + 1);
|
|
if (!(o instanceof ops.Delimiter)) {
|
|
return o;
|
|
} else {
|
|
return null;
|
|
}
|
|
} else {
|
|
throw new Error("you must specify a position parameter");
|
|
}
|
|
};
|
|
|
|
ListManager.prototype.getOperationByPosition = function(position) {
|
|
var o;
|
|
o = this.beginning;
|
|
while (true) {
|
|
if (o instanceof ops.Delimiter && (o.prev_cl != null)) {
|
|
o = o.prev_cl;
|
|
while (o.isDeleted() && (o.prev_cl != null)) {
|
|
o = o.prev_cl;
|
|
}
|
|
break;
|
|
}
|
|
if (position <= 0 && !o.isDeleted()) {
|
|
break;
|
|
}
|
|
o = o.next_cl;
|
|
if (!o.isDeleted()) {
|
|
position -= 1;
|
|
}
|
|
}
|
|
return o;
|
|
};
|
|
|
|
ListManager.prototype.push = function(content) {
|
|
return this.insertAfter(this.end.prev_cl, [content]);
|
|
};
|
|
|
|
ListManager.prototype.insertAfter = function(left, contents) {
|
|
var c, j, len, right, tmp;
|
|
right = left.next_cl;
|
|
while (right.isDeleted()) {
|
|
right = right.next_cl;
|
|
}
|
|
left = right.prev_cl;
|
|
if (contents instanceof ops.Operation) {
|
|
(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, null, void 0, void 0, left, right)).execute();
|
|
left = tmp;
|
|
}
|
|
}
|
|
return this;
|
|
};
|
|
|
|
ListManager.prototype.insert = function(position, contents) {
|
|
var ith;
|
|
ith = this.getOperationByPosition(position);
|
|
return this.insertAfter(ith, contents);
|
|
};
|
|
|
|
ListManager.prototype["delete"] = function(position, length) {
|
|
var d, delete_ops, i, j, o, ref;
|
|
if (length == null) {
|
|
length = 1;
|
|
}
|
|
o = this.getOperationByPosition(position + 1);
|
|
delete_ops = [];
|
|
for (i = j = 0, ref = length; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
|
|
if (o instanceof ops.Delimiter) {
|
|
break;
|
|
}
|
|
d = (new ops.Delete(null, void 0, o)).execute();
|
|
o = o.next_cl;
|
|
while ((!(o instanceof ops.Delimiter)) && o.isDeleted()) {
|
|
o = o.next_cl;
|
|
}
|
|
delete_ops.push(d._encode());
|
|
}
|
|
return this;
|
|
};
|
|
|
|
ListManager.prototype.callOperationSpecificInsertEvents = function(op) {
|
|
var getContentType;
|
|
getContentType = function(content) {
|
|
if (content instanceof ops.Operation) {
|
|
return content.getCustomType();
|
|
} else {
|
|
return content;
|
|
}
|
|
};
|
|
return this.callEvent([
|
|
{
|
|
type: "insert",
|
|
position: op.getPosition(),
|
|
object: this.getCustomType(),
|
|
changedBy: op.uid.creator,
|
|
value: getContentType(op.val())
|
|
}
|
|
]);
|
|
};
|
|
|
|
ListManager.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {
|
|
return this.callEvent([
|
|
{
|
|
type: "delete",
|
|
position: op.getPosition(),
|
|
object: this.getCustomType(),
|
|
length: 1,
|
|
changedBy: del_op.uid.creator,
|
|
oldValue: op.val()
|
|
}
|
|
]);
|
|
};
|
|
|
|
return ListManager;
|
|
|
|
})(ops.Operation);
|
|
ops.ListManager.parse = function(json) {
|
|
var content, content_operations, custom_type, uid;
|
|
uid = json['uid'], custom_type = json['custom_type'], content = json['content'], content_operations = json['content_operations'];
|
|
return new this(custom_type, uid, content, content_operations);
|
|
};
|
|
ops.Composition = (function(superClass) {
|
|
extend(Composition, superClass);
|
|
|
|
function Composition(custom_type, _composition_value, composition_value_operations, uid, tmp_composition_ref) {
|
|
var n, o;
|
|
this._composition_value = _composition_value;
|
|
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) {
|
|
if (op.uid.creator === this.tmp_composition_ref.creator && op.uid.op_number === this.tmp_composition_ref.op_number) {
|
|
this.composition_ref = op;
|
|
delete this.tmp_composition_ref;
|
|
o = op.next_cl;
|
|
while (o.next_cl != null) {
|
|
if (!o.isDeleted()) {
|
|
this.callOperationSpecificInsertEvents(o);
|
|
}
|
|
o = o.next_cl;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if (this.composition_ref.next_cl === op) {
|
|
op.undo_delta = this.getCustomType()._apply(op.val());
|
|
} else {
|
|
o = this.end.prev_cl;
|
|
while (o !== op) {
|
|
this.getCustomType()._unapply(o.undo_delta);
|
|
o = o.prev_cl;
|
|
}
|
|
while (o !== this.end) {
|
|
o.undo_delta = this.getCustomType()._apply(o.val());
|
|
o = o.next_cl;
|
|
}
|
|
}
|
|
this.composition_ref = this.end.prev_cl;
|
|
return this.callEvent([
|
|
{
|
|
type: "update",
|
|
changedBy: op.uid.creator,
|
|
newValue: this.val()
|
|
}
|
|
]);
|
|
};
|
|
|
|
Composition.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {};
|
|
|
|
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 = {};
|
|
}
|
|
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 {
|
|
json.composition_ref = this.tmp_composition_ref;
|
|
}
|
|
return Composition.__super__._encode.call(this, json);
|
|
};
|
|
|
|
return Composition;
|
|
|
|
})(ops.ListManager);
|
|
ops.Composition.parse = function(json) {
|
|
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);
|
|
|
|
function ReplaceManager(custom_type, event_properties1, event_this1, uid) {
|
|
this.event_properties = event_properties1;
|
|
this.event_this = event_this1;
|
|
if (this.event_properties['object'] == null) {
|
|
this.event_properties['object'] = this.event_this.getCustomType();
|
|
}
|
|
ReplaceManager.__super__.constructor.call(this, custom_type, uid);
|
|
}
|
|
|
|
ReplaceManager.prototype.type = "ReplaceManager";
|
|
|
|
ReplaceManager.prototype.callEventDecorator = function(events) {
|
|
var event, j, len, name, prop, ref;
|
|
if (!this.isDeleted()) {
|
|
for (j = 0, len = events.length; j < len; j++) {
|
|
event = events[j];
|
|
ref = this.event_properties;
|
|
for (name in ref) {
|
|
prop = ref[name];
|
|
event[name] = prop;
|
|
}
|
|
}
|
|
this.event_this.callEvent(events);
|
|
}
|
|
return void 0;
|
|
};
|
|
|
|
ReplaceManager.prototype.callOperationSpecificInsertEvents = function(op) {
|
|
var old_value;
|
|
if (op.next_cl.type === "Delimiter" && op.prev_cl.type !== "Delimiter") {
|
|
if (!op.is_deleted) {
|
|
old_value = op.prev_cl.val();
|
|
this.callEventDecorator([
|
|
{
|
|
type: "update",
|
|
changedBy: op.uid.creator,
|
|
oldValue: old_value
|
|
}
|
|
]);
|
|
}
|
|
op.prev_cl.applyDelete();
|
|
} else if (op.next_cl.type !== "Delimiter") {
|
|
op.applyDelete();
|
|
} else {
|
|
this.callEventDecorator([
|
|
{
|
|
type: "add",
|
|
changedBy: op.uid.creator
|
|
}
|
|
]);
|
|
}
|
|
return void 0;
|
|
};
|
|
|
|
ReplaceManager.prototype.callOperationSpecificDeleteEvents = function(op, del_op) {
|
|
if (op.next_cl.type === "Delimiter") {
|
|
return this.callEventDecorator([
|
|
{
|
|
type: "delete",
|
|
changedBy: del_op.uid.creator,
|
|
oldValue: op.val()
|
|
}
|
|
]);
|
|
}
|
|
};
|
|
|
|
ReplaceManager.prototype.replace = function(content, replaceable_uid) {
|
|
var o, relp;
|
|
o = this.getLastOperation();
|
|
relp = (new ops.Insert(null, content, null, this, replaceable_uid, o, o.next_cl)).execute();
|
|
return void 0;
|
|
};
|
|
|
|
ReplaceManager.prototype.isContentDeleted = function() {
|
|
return this.getLastOperation().isDeleted();
|
|
};
|
|
|
|
ReplaceManager.prototype.deleteContent = function() {
|
|
(new ops.Delete(null, void 0, this.getLastOperation().uid)).execute();
|
|
return void 0;
|
|
};
|
|
|
|
ReplaceManager.prototype.val = function() {
|
|
var o;
|
|
o = this.getLastOperation();
|
|
return typeof o.val === "function" ? o.val() : void 0;
|
|
};
|
|
|
|
return ReplaceManager;
|
|
|
|
})(ops.ListManager);
|
|
return basic_ops;
|
|
};
|