1
0
mirror of /repos/Prototyper.git synced 2026-02-27 16:12:00 +01:00

js checks complient/ code formatter

This commit is contained in:
Aiko Mastboom
2015-07-01 17:04:10 +02:00
parent 2076e471ac
commit 7dea45ab19
16 changed files with 628 additions and 457 deletions

View File

@@ -1,26 +1,26 @@
'use strict';
module.exports = function (mongoDataInstance, previewInstance, importerInstance) {
"use strict";
function getAttribute(options, callback) {
mongoDataInstance.getMongoAttribute(options, callback);
return mongoDataInstance.getMongoAttribute(options, callback);
}
function getContent(options, callback) {
mongoDataInstance.getMongoContent(options, callback);
return mongoDataInstance.getMongoContent(options, callback);
}
function getPreviewHTML(html, options, callback) {
previewInstance.getPreviewHTML(html, options, callback);
return previewInstance.getPreviewHTML(html, options, callback);
}
function importer(doc, options, callback) {
importerInstance.importer(doc, options, callback);
return importerInstance.importer(doc, options, callback);
}
return {
getAttribute: getAttribute,
getContent: getContent,
getAttribute: getAttribute,
getContent: getContent,
getPreviewHTML: getPreviewHTML,
importer: importer
importer: importer
};
};

View File

@@ -1,12 +1,12 @@
'use strict';
var when = require('when');
var _ = require('underscore');
var _ = require('underscore');
module.exports = function (markers) {
"use strict";
function replace(text, marker, getReplacement, once) {
var deferred = when.defer();
var regExp = new RegExp(markers.prefix + marker + markers.postfix, 'gmi');
var matches = text && text.match && text.match(regExp);
var regExp = new RegExp(markers.prefix + marker + markers.postfix, 'gmi');
var matches = text && text.match && text.match(regExp);
if (matches) {
if (once) {
matches = [matches[0]];
@@ -20,7 +20,7 @@ module.exports = function (markers) {
deferred2.reject(err);
} else {
var replace_result = {
regExp: replacement.regExp || regExp,
regExp: replacement.regExp || regExp,
replacement: replacement.value
};
deferred2.resolve(replace_result);
@@ -51,18 +51,18 @@ module.exports = function (markers) {
});
});
} catch (e) {
return callback(e);
return callback && callback(e);
}
return callback(null, text);
return callback && callback(null, text);
},
function onFailure(err) {
return callback(err);
return callback && callback(err);
}
);
}
return {
replace: replace,
replace: replace,
handTextManipulation: handTextManipulation
};
};

View File

@@ -1,12 +1,11 @@
'use strict';
var when = require('when');
var _ = require('underscore');
var _ = require('underscore');
var path = require('path');
var fs = require('fs');
var fs = require('fs');
module.exports = function (config, mongoInstance, helpers, markers) {
"use strict";
var replaceMarkers;
function handleImportMarkers(doc, options, callback) {
@@ -18,28 +17,28 @@ module.exports = function (config, mongoInstance, helpers, markers) {
return new_text;
}
helpers.handTextManipulation(doc,
return helpers.handTextManipulation(doc,
promises,
handler,
callback
);
}
var import_leftovers_tag = markers.import_leftovers_tag;
var import_leftovers_tag = markers.import_leftovers_tag;
var import_leftovers_regexp = markers.import_leftovers_regexp;
function importer(doc, options, cb) {
var promises = helpers.replace(doc, import_leftovers_tag,
function getReplacement(result, callback) {
var parts = import_leftovers_regexp.exec(result);
var parts = import_leftovers_regexp.exec(result);
var context = {
collection: parts[1],
name: parts[2],
attribute: parts[3]
name: parts[2],
attribute: parts[3]
};
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: context
value: context
});
},
// there can be only one import_leftovers
@@ -50,33 +49,33 @@ module.exports = function (config, mongoInstance, helpers, markers) {
handleImportMarkers(doc, options, function handleLeftover(err, remainder) {
if (err) {
config.error && config.error('ERR importer.importer handleImportMarkers', err);
return cb(err);
return cb && cb(err);
}
if (leftover) {
return mongoInstance.ensureContent(leftover.replacement, function parent(err, parent_result) {
if (err) {
config.error && config.error('ERR importer.importer ensureContent', err);
return cb(err);
return cb && cb(err);
}
leftover.replacement.query = { _id: parent_result._id };
leftover.replacement.query = {_id: parent_result._id};
remainder = remainder.replace(leftover.regExp, "");
remainder = remainder.replace(leftover.regExp, '');
return mongoInstance.setMongoAttribute(remainder, leftover.replacement, function savedAttribute(err) {
if (err) {
config.error && config.error('ERR importer.importer setMongoAttribute', err);
return cb(err);
return cb && cb(err);
}
return cb(null, remainder);
return cb && cb(null, remainder);
});
});
}
config.debug && config.debug('no import_leftover tag found');
return cb(null, remainder);
return cb && cb(null, remainder);
});
},
function onFailure(err) {
config.error && config.error('ERR importer.importer onFailure', err);
return cb(err);
return cb && cb(err);
}
);
}
@@ -86,23 +85,23 @@ module.exports = function (config, mongoInstance, helpers, markers) {
return mongoInstance.ensureContent(context, function parent(err, parent_result) {
if (err) {
config.error && config.error('ERR importer.importer ensureContent', err);
return callback(err);
return callback && callback(err);
}
function replaceWithEmptyContent(err) {
var replacement = {
regExp: result,
value: ""
value: ''
};
return callback(err, replacement);
return callback && callback(err, replacement);
}
if (context.attribute === "json") {
var data = null;
if (context.attribute === 'json') {
var data = null;
try {
data = JSON.parse(remainder);
} catch (error) {
config.error && config.error('ERR importer.replaceMarkers JSON.parse(remainder)', remainder, error);
return callback(error);
return callback && callback(error);
}
if (data._id) {
delete data._id;
@@ -112,31 +111,31 @@ module.exports = function (config, mongoInstance, helpers, markers) {
return mongoInstance.setMongoContent(parent_result, context, function (err) {
if (err) {
config.error && config.error('ERR importer.importRemainder setMongoContent', err);
return callback(err);
return callback && callback(err);
}
var documentId = 'json:' + context.collection + ':' + context.name;
var keys = _.keys(parent_result); // reset all attributes;
var keys = _.keys(parent_result); // reset all attributes;
return mongoInstance.updateShareDocument(documentId, parent_result, keys, function () {
return replaceWithEmptyContent(null);
});
});
}
context.query = { _id: parent_result._id };
context.query = {_id: parent_result._id};
return mongoInstance.setMongoAttribute(remainder, context, function savedAttribute(err) {
if (err) {
config.error && config.error('ERR2 importer.importer setMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
return replaceWithEmptyContent(null);
});
});
}
var import_tag = markers.import_tag;
var import_regexp = markers.import_regexp;
var import_tag = markers.import_tag;
var import_regexp = markers.import_regexp;
var import_strip_regexp = markers.import_strip_regexp;
var import_file_tag = markers.import_file_tag;
var import_file_tag = markers.import_file_tag;
var import_file_regexp = markers.import_file_regexp;
replaceMarkers = function (doc, options) {
@@ -144,19 +143,19 @@ module.exports = function (config, mongoInstance, helpers, markers) {
promises.push(
helpers.replace(doc, import_tag, function handleImportMarker(result, callback) {
var parts = import_regexp.exec(result);
var context = {
var parts = import_regexp.exec(result);
var context = {
collection: parts[1],
name: parts[2],
attribute: parts[3]
name: parts[2],
attribute: parts[3]
};
var striped_parts = import_strip_regexp.exec(parts[4]);
var sub_doc = striped_parts[1];
var sub_doc = striped_parts[1];
handleImportMarkers(sub_doc, options, function handleLeftover(err, remainder) {
if (err) {
config.error && config.error('ERR importer.replaceMarkers import_content_marker', err);
return callback(err);
return callback && callback(err);
}
return importRemainder(context, result, remainder, callback);
});
@@ -166,23 +165,23 @@ module.exports = function (config, mongoInstance, helpers, markers) {
promises.push(
helpers.replace(doc, import_file_tag, function handleImportFileMarker(result, callback) {
var parts = import_file_regexp.exec(result);
var parts = import_file_regexp.exec(result);
var filename = path.resolve(config.statics.importer_path, parts[1]);
var context = {
var context = {
collection: parts[2],
name: parts[3],
attribute: parts[4]
name: parts[3],
attribute: parts[4]
};
fs.readFile(filename, 'utf-8', function handleFileContent(err, sub_doc) {
if (err) {
config.error && config.error('ERR importer.replaceMarkers readFile', err);
return callback(err);
return callback && callback(err);
}
// process with leftover marker support
return importer(sub_doc, context, function handleLeftover(err, remainder) {
if (err) {
config.error && config.error('ERR importer.replaceMarkers importer', err);
return callback(err);
return callback && callback(err);
}
return importRemainder(context, result, remainder, callback);
});

View File

@@ -1,23 +1,23 @@
'use strict';
module.exports = function markers(config) {
"use strict";
var marker_prefix = '<!--\\s*@@';
var marker_prefix = '<!--\\s*@@';
var marker_postfix = '\\s*-->';
// Preview markers
var script_tag = 'script__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var script_tag = 'script__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var script_regexp = new RegExp(marker_prefix + script_tag + marker_postfix);
var style_tag = 'style__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var style_tag = 'style__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var style_regexp = new RegExp(marker_prefix + style_tag + marker_postfix);
var less_tag = 'less__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var less_tag = 'less__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var less_regexp = new RegExp(marker_prefix + less_tag + marker_postfix);
var template_tag = 'template__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)__context__([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var template_tag = 'template__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)__context__([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var template_regexp = new RegExp(marker_prefix + template_tag + marker_postfix);
var markdown_tag = 'markdown__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var markdown_tag = 'markdown__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var markdown_regexp = new RegExp(marker_prefix + markdown_tag + marker_postfix);
// remove_tag is greedy, so we can do multiple removes on one page.
@@ -25,16 +25,16 @@ module.exports = function markers(config) {
//var remove_regexp = new RegExp(remove_tag);
// Importer markers
var import_leftovers_tag = 'import_leftovers__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_leftovers_tag = 'import_leftovers__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_leftovers_regexp = new RegExp(marker_prefix + import_leftovers_tag + marker_postfix);
// import tag is non greedy on purpose, you can only import content into 1 object,
// repeating the same tag will result in content overwriting each other.
var import_tag = 'import__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)_([\\w\\W]*)_end_import__\\1_\\2_\\3';
var import_regexp = new RegExp(marker_prefix + import_tag + marker_postfix);
var import_tag = 'import__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)_([\\w\\W]*)_end_import__\\1_\\2_\\3';
var import_regexp = new RegExp(marker_prefix + import_tag + marker_postfix);
var import_strip_regexp = new RegExp(marker_postfix + '([\\w\\W]*)' + marker_prefix);
var import_file_tag = 'import_file__([A-Za-z0-9.\/]+)__into__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_file_tag = 'import_file__([A-Za-z0-9.\/]+)__into__([A-Za-z0-9]+)_([A-Za-z0-9]+)_([A-Za-z0-9]+)';
var import_file_regexp = new RegExp(marker_prefix + import_file_tag + marker_postfix);
@@ -45,27 +45,27 @@ module.exports = function markers(config) {
}
return {
prefix: marker_prefix,
postfix: marker_postfix,
createTag: createTag,
script_tag: script_tag,
script_regexp: script_regexp,
style_tag: style_tag,
style_regexp: style_regexp,
less_tag: less_tag,
less_regexp: less_regexp,
template_tag: template_tag,
template_regexp: template_regexp,
markdown_tag: markdown_tag,
markdown_regexp: markdown_regexp,
remove_tag: remove_tag,
prefix: marker_prefix,
postfix: marker_postfix,
createTag: createTag,
script_tag: script_tag,
script_regexp: script_regexp,
style_tag: style_tag,
style_regexp: style_regexp,
less_tag: less_tag,
less_regexp: less_regexp,
template_tag: template_tag,
template_regexp: template_regexp,
markdown_tag: markdown_tag,
markdown_regexp: markdown_regexp,
remove_tag: remove_tag,
// remove_regexp:remove_regexp,
import_leftovers_tag: import_leftovers_tag,
import_leftovers_tag: import_leftovers_tag,
import_leftovers_regexp: import_leftovers_regexp,
import_tag: import_tag,
import_regexp: import_regexp,
import_strip_regexp: import_strip_regexp,
import_file_tag: import_file_tag,
import_file_regexp: import_file_regexp
import_tag: import_tag,
import_regexp: import_regexp,
import_strip_regexp: import_strip_regexp,
import_file_tag: import_file_tag,
import_file_regexp: import_file_regexp
};
};

View File

@@ -1,51 +1,51 @@
'use strict';
var ObjectID = require('mongodb').ObjectID;
var _ = require('underscore');
var _ = require('underscore');
module.exports = function (config, db, shareModel) {
"use strict";
/*
options:
* options:
* collection (mandatory)
* query (mandatory)
*/
function getMongoContent(options, callback) {
config.debug && config.debug('getMongoContent options', options);
if ( !options.collection) {
if (!options.collection) {
return callback(new Error('Data not found / missing collection'));
}
return db.collection(options.collection, function collection(err, col) {
if (err) {
config.error && config.error('ERR2 getMongoContent', err);
return callback(err);
return callback && callback(err);
}
if (!options.query) {
return callback(new Error('Data not found ' + options.collection + '/ missing query'), null, col);
return callback && callback(new Error('Data not found ' + options.collection + '/ missing query'), null, col);
}
if (options.query._id && !(options.query._id instanceof Object)) {
try {
options.query._id = new ObjectID.createFromHexString(options.query._id);
} catch (error) {
config.error && config.error('ERR3 getMongoContent', error);
return callback(error);
return callback && callback(error);
}
}
return col.findOne(options.query, function foundOne(err, result) {
if (err) {
config.error && config.error('ERR4 getMongoContent', err);
return callback(err);
return callback && callback(err);
}
if (!result) {
return callback(new Error('Data not found ' + options.collection + '/' + JSON.stringify(options.query)), null, col);
return callback && callback(new Error('Data not found ' + options.collection + '/' + JSON.stringify(options.query)), null, col);
}
return callback(null, result, col);
return callback && callback(null, result, col);
});
});
}
/*
options:
* options:
* collection (mandatory)
* query (mandatory)
* attribute (mandatory)
@@ -55,7 +55,7 @@ module.exports = function (config, db, shareModel) {
return getMongoContent(options, function document(err, result) {
if (err) {
config.error && config.error('ERR1 getMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
var attribute_options = null;
config.debug && config.debug('getMongoAttribute result', result);
@@ -64,25 +64,25 @@ module.exports = function (config, db, shareModel) {
result[options.attribute].guid) {
attribute_options = {
collection: options.collection,
query: {_id: result[options.attribute].guid}
query: {_id: result[options.attribute].guid}
};
config.debug && config.debug('getMongoAttribute attribute_options', attribute_options);
getMongoContent(attribute_options, function attribute(err, attribute_result) {
if (err) {
config.error && config.error('ERR2 getMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
config.debug && config.debug('getMongoAttribute attribute_result', attribute_result);
return callback(err, attribute_result);
return callback && callback(err, attribute_result);
});
} else {
config.debug && config.debug('getMongoAttribute try direct lookup');
attribute_options = {
collection: options.collection,
query: {
query: {
parent: result._id,
name: result.name + '.' + options.attribute
name: result.name + '.' + options.attribute
}
};
config.debug && config.debug('getMongoAttribute attribute_options', attribute_options);
@@ -90,10 +90,10 @@ module.exports = function (config, db, shareModel) {
return getMongoContent(attribute_options, function attribute(err, attribute_result) {
if (err) {
config.error && config.error('ERR getMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
config.debug && config.debug('getMongoAttribute direct attribute_result', attribute_result);
return callback(err, attribute_result);
return callback && callback(err, attribute_result);
});
}
});
@@ -107,11 +107,11 @@ module.exports = function (config, db, shareModel) {
collection.save(data, {safe: true}, function (err, result2, result3) {
if (err) {
config.error && config.error('ERR saveData', err);
return callback(err);
return callback && callback(err);
}
config.debug && config.debug('saveData saved', data._id, result2, result3);
return callback(null, data, collection);
return callback && callback(null, data, collection);
});
}
@@ -125,20 +125,20 @@ module.exports = function (config, db, shareModel) {
return updateData(collection, data, callback);
});
} else {
updating = true;
updating = true;
var stopUpdating = function (err, result, col) {
config.debug && config.debug('Stop updating');
updating = false;
if (err) {
return callback(err);
return callback && callback(err);
}
return callback(null, result, col);
return callback && callback(null, result, col);
};
return collection.findOne({ _id: data._id}, function foundOne(err, result) {
return collection.findOne({_id: data._id}, function foundOne(err, result) {
if (err) {
config.error && config.error('ERR updateData', err);
return callback(err);
return callback && callback(err);
}
_.extend(result, data);
return saveData(collection, result, stopUpdating);
@@ -148,7 +148,7 @@ module.exports = function (config, db, shareModel) {
/*
options:
* options:
* collection (mandatory)
* query (mandatory)
* update (optional) : extends existing content
@@ -158,7 +158,7 @@ module.exports = function (config, db, shareModel) {
return db.collection(options.collection, function handleCollection(err, collection) {
if (err) {
config.error && config.error('ERR2 setMongoContent', err);
return callback(err);
return callback && callback(err);
}
if (options.operation) {
data.version = options.operation.v;
@@ -172,7 +172,7 @@ module.exports = function (config, db, shareModel) {
collection.findOne(options.query, function foundOne(err, result) {
if (err) {
config.error && config.error('ERR3 setMongoContent', err);
return callback(err);
return callback && callback(err);
}
if (result) {
data._id = result._id;
@@ -191,16 +191,17 @@ module.exports = function (config, db, shareModel) {
config.warn && config.warn('WARN setMongoAttribute updateShareDocumentPath shareModel.getSnapshot', documentId, err);
return callback && callback();
}
var sub_data = data;
var sub_data = data;
var sub_snapshot_data = doc.snapshot;
var equal_path = [];
var found = false;
var equal_path = [];
var found = false;
var x;
for (x = 0; !found && x < path.length; x += 1) {
var key = path[x];
if (sub_data && sub_data.hasOwnProperty(key)
&& sub_snapshot_data && sub_snapshot_data.hasOwnProperty(key)) {
sub_data = sub_data[key];
if (sub_data && sub_data.hasOwnProperty(key) &&
sub_snapshot_data &&
sub_snapshot_data.hasOwnProperty(key)) {
sub_data = sub_data[key];
sub_snapshot_data = sub_snapshot_data[key];
equal_path.push(key);
} else if (!sub_snapshot_data || !sub_snapshot_data.hasOwnProperty(key)) {
@@ -219,7 +220,10 @@ module.exports = function (config, db, shareModel) {
if (sub_snapshot_data) {
op.od = sub_snapshot_data;
}
return shareModel.applyOp(documentId, { op: [op], v: doc.v }, function (err, result) {
return shareModel.applyOp(documentId, {
op: [op],
v: doc.v
}, function (err, result) {
if (err) {
config.error && config.error('ERR updateShareDocumentPath shareModel.applyOp', documentId, err);
return callback && callback();
@@ -244,10 +248,12 @@ module.exports = function (config, db, shareModel) {
}
if (doc.type.name === 'text') {
ops.push({
d: doc.snapshot, p: 0
d: doc.snapshot,
p: 0
});
ops.push({
i: data, p: 0
i: data,
p: 0
});
} else if (doc.type.name === 'json') {
_.forEach(keys, function (key) {
@@ -265,7 +271,10 @@ module.exports = function (config, db, shareModel) {
}
});
}
return shareModel.applyOp(documentId, { op: ops, v: doc.v }, function (err, result) {
return shareModel.applyOp(documentId, {
op: ops,
v: doc.v
}, function (err, result) {
if (err) {
config.warn && config.warn('WARN updateShareDocument shareModel.applyOp', documentId, err);
return callback && callback();
@@ -297,16 +306,16 @@ module.exports = function (config, db, shareModel) {
config.debug && config.debug('Stop ensuring', options);
ensuring = false;
if (err) {
return callback(err);
return callback && callback(err);
}
return callback(null, result, col);
return callback && callback(null, result, col);
};
getMongoContent(options, function document(err, result, col) {
if (err) {
if (/Data not found*/.test(err.message)) {
var documentId = 'json:' + options.collection + ':' + options.name;
var data = {name: options.name};
var data = {name: options.name};
setMongoContent(data, options, function (err, content_result, col) {
var keys = _.keys(data); // reset all attributes;
return updateShareDocument(documentId, data, keys, function updatedShareDocument() {
@@ -332,21 +341,21 @@ module.exports = function (config, db, shareModel) {
ensureContent(options, function document(err, result, col) {
if (err) {
config.error && config.error('ERR1 setMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
var attribute_options = {
collection: options.collection,
name: result.name + '.' + options.attribute
name: result.name + '.' + options.attribute
};
if (result.hasOwnProperty(options.attribute)
&& result[options.attribute].guid) {
if (result.hasOwnProperty(options.attribute) &&
result[options.attribute].guid) {
attribute_options.query = {
_id: result[options.attribute].guid
};
} else {
attribute_options.query = {
parent: result._id,
name: result.name + '.' + options.attribute
name: result.name + '.' + options.attribute
};
}
@@ -355,7 +364,7 @@ module.exports = function (config, db, shareModel) {
return ensureContent(attribute_options, function attribute(err, attribute_result) {
if (err) {
config.error && config.error('ERR2 setMongoAttribute ensureContent', err);
return callback(err);
return callback && callback(err);
}
var updateContent = true;
if (result[options.attribute]) {
@@ -365,11 +374,11 @@ module.exports = function (config, db, shareModel) {
result[options.attribute].guid = attribute_result._id;
}
} else {
result[options.attribute] = { guid: attribute_result._id };
result[options.attribute] = {guid: attribute_result._id};
}
attribute_result.parent = result._id;
attribute_result.name = result.name + '.' + options.attribute;
attribute_result.parent = result._id;
attribute_result.name = result.name + '.' + options.attribute;
attribute_result[options.attribute] = data;
if (options.operation) {
attribute_result.version = options.operation.v;
@@ -377,18 +386,18 @@ module.exports = function (config, db, shareModel) {
return saveData(col, attribute_result, function saved(err) {
if (err) {
config.error && config.error('ERR3 setMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
var documentId = 'json:' + options.collection + ':' + result.name;
var type = options.type || 'text';
var documentId = 'json:' + options.collection + ':' + result.name;
var type = options.type || 'text';
var attributeDocumentId = type + ':' + options.collection + ':' + result.name + ':' + options.attribute;
var keys = null;
var share_data = null;
var keys = null;
var share_data = null;
if (type === 'json') {
keys = _.keys(attribute_result); // reset all attributes;
share_data = attribute_result;
} else {
keys = [attribute_result.name];
keys = [attribute_result.name];
share_data = data;
}
if (options.no_share) {
@@ -399,15 +408,15 @@ module.exports = function (config, db, shareModel) {
updateData(col, result, function saved(err) {
if (err) {
config.error && config.error('ERR3 setMongoAttribute', err);
return callback(err);
return callback && callback(err);
}
var path = [options.attribute, 'guid']; // reset just guid attribute;
return updateShareDocumentPath(documentId, result, path, function updatedShareContent() {
return callback(null, attribute_result, col);
return callback && callback(null, attribute_result, col);
});
});
} else {
callback(null, attribute_result, col);
return callback && callback(null, attribute_result, col);
}
});
});
@@ -416,11 +425,11 @@ module.exports = function (config, db, shareModel) {
}
return {
getMongoAttribute: getMongoAttribute,
getMongoContent: getMongoContent,
setMongoAttribute: setMongoAttribute,
setMongoContent: setMongoContent,
ensureContent: ensureContent,
getMongoAttribute: getMongoAttribute,
getMongoContent: getMongoContent,
setMongoAttribute: setMongoAttribute,
setMongoContent: setMongoContent,
ensureContent: ensureContent,
updateShareDocument: updateShareDocument
};
};

View File

@@ -1,26 +1,26 @@
'use strict';
var Handlebars = require('handlebars');
var markdown = require('markdown').markdown;
var _ = require('underscore');
var less = require('less');
var when = require('when');
var markdown = require('markdown').markdown;
var _ = require('underscore');
var less = require('less');
var when = require('when');
module.exports = function (config, mongoDataInstance, helpers, markers) {
"use strict";
var getPreviewHTML;
var script_tag = markers.script_tag;
var script_tag = markers.script_tag;
var script_regexp = markers.script_regexp;
var style_tag = markers.style_tag;
var style_tag = markers.style_tag;
var style_regexp = markers.style_regexp;
var less_tag = markers.less_tag;
var less_tag = markers.less_tag;
var less_regexp = markers.less_regexp;
var template_tag = markers.template_tag;
var template_tag = markers.template_tag;
var template_regexp = markers.template_regexp;
var markdown_tag = markers.markdown_tag;
var markdown_tag = markers.markdown_tag;
var markdown_regexp = markers.markdown_regexp;
var remove_tag = markers.remove_tag;
@@ -33,76 +33,76 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
// Script tag handling
promises.push(
helpers.replace(html, script_tag, function handleScriptMarker(result, callback) {
var parts = script_regexp.exec(result);
var parts = script_regexp.exec(result);
var context = {
collection: parts[1],
name: parts[2],
attribute: parts[3]
name: parts[2],
attribute: parts[3]
};
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<script src="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.js" type="text/javascript" charset="utf-8"></script>'
});
return callback && callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<script src="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.js" type="text/javascript" charset="utf-8"></script>'
});
}));
// Style tag handling
promises.push(
helpers.replace(html, style_tag, function handleStyleMarker(result, callback) {
var parts = style_regexp.exec(result);
var parts = style_regexp.exec(result);
var context = {
collection: parts[1],
name: parts[2],
attribute: parts[3]
name: parts[2],
attribute: parts[3]
};
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<link href="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.css" media="all" rel="stylesheet" type="text/css">'
});
return callback && callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<link href="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.css" media="all" rel="stylesheet" type="text/css">'
});
}));
// Less tag handling
promises.push(
helpers.replace(html, less_tag, function handleStyleMarker(result, callback) {
var parts = less_regexp.exec(result);
var parts = less_regexp.exec(result);
var context = {
collection: parts[1],
name: parts[2],
attribute: parts[3]
name: parts[2],
attribute: parts[3]
};
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<link href="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.less" media="all" rel="stylesheet/less" type="text/css">'
});
return callback && callback(null, {
regExp: new RegExp(result, 'gmi'),
value: '<link href="' + config.api.content + '/' + context.collection + '/' + context.name + '/' + context.attribute + '.less" media="all" rel="stylesheet/less" type="text/css">'
});
}));
// Template tag handling
promises.push(
helpers.replace(html, template_tag, function handleTemplateMarker(result, callback) {
var parts = template_regexp.exec(result);
var parts = template_regexp.exec(result);
var template = {
collection: parts[1],
name: parts[2],
attribute: parts[3],
query: { name: parts[2]}
name: parts[2],
attribute: parts[3],
query: {name: parts[2]}
};
var context = {
var context = {
collection: parts[4],
name: parts[5],
query: { name: parts[5]},
req: options.req
name: parts[5],
query: {name: parts[5]},
req: options.req
};
return mongoDataInstance.getMongoContent(context, function handleContext(err, context_result) {
if (err) {
config.error && config.error('ERR template_tag getMongoContent', context);
return callback(err);
return callback && callback(err);
}
return mongoDataInstance.getMongoAttribute(template, function handleTemplate(err, template_result) {
if (err) {
config.error && config.error('ERR template_tag getMongoAttribute', template, err);
return callback(err);
return callback && callback(err);
}
var compiled_template = null;
var keys_to_collect = {};
var keys_to_collect = {};
function collectKey(key) {
return function () {
@@ -110,7 +110,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
return keys_to_collect[key];
}
keys_to_collect[key] = null;
return "waiting for content";
return 'waiting for content';
};
}
@@ -128,7 +128,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
compiled_template = handlebars.compile(template_result[template.attribute]);
} catch (error1) {
config.error && config.error('ERR template_tag Handlebars.compile', template, error1);
return callback(error1);
return callback && callback(error1);
}
var extendHandlebars_context = function (result) {
var handlebars = {};
@@ -141,41 +141,43 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
});
return handlebars;
};
var handlebars_context = extendHandlebars_context(context_result);
var handlebars_context = extendHandlebars_context(context_result);
try {
compiled_template(handlebars_context);
} catch (error2) {
config.error && config.error('ERR template_tag Handlebars.render', template, context, error2);
return callback(error2);
return callback && callback(error2);
}
var promises = [];
_.forEach(_.keys(keys_to_collect), function gatherKeyValues(key) {
var deferred = when.defer();
var promise = deferred.promise;
//noinspection JSUnresolvedFunction
var deferred = when.defer();
var promise = deferred.promise;
promises.push(promise);
var attribute_context = {
collection: context.collection,
attribute: key,
query: { _id: context_result._id}
attribute: key,
query: {_id: context_result._id}
};
return mongoDataInstance.getMongoAttribute(attribute_context, function cacheTemplateKey(err, template_key_result) {
if (err) {
config.error && config.error('ERR handlebar.registerHelper getMongoAttribute', err);
deferred.reject(err);
}
var value = template_key_result[key];
var value = template_key_result[key];
keys_to_collect[key] = value;
deferred.resolve(value);
});
});
//noinspection JSUnresolvedFunction
return when.all(promises).then(
function onSuccess() {
var rendered = null;
var rendered = null;
try {
rendered = compiled_template(handlebars_context);
} catch (err) {
config.error && config.error('ERR template_tag Handlebars.render', template, context, err);
return callback(err);
return callback && callback(err);
}
config.debug && config.debug('// handle markers on rendered template');
context.query = {_id: context_result._id};
@@ -186,13 +188,13 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
}
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: preview_html
value: preview_html
});
});
},
function onFailure(err) {
config.error && config.error('ERR template_tag resolving promises', err);
return callback(err);
return callback && callback(err);
}
);
});
@@ -202,32 +204,32 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
// Markdown tag handling
promises.push(
helpers.replace(html, markdown_tag, function handleMarkDownMarker(result, callback) {
var parts = markdown_regexp.exec(result);
var parts = markdown_regexp.exec(result);
var attribute = {
collection: parts[1],
name: parts[2],
attribute: parts[3],
query: { name: parts[2] }
name: parts[2],
attribute: parts[3],
query: {name: parts[2]}
};
return mongoDataInstance.getMongoAttribute(attribute, function handleMarkdownContent(err, markdown_result) {
if (err) {
return callback(err);
return callback && callback(err);
}
var html = markdown.toHTML(markdown_result[attribute.attribute]);
return callback(null, {
regExp: new RegExp(result, 'gmi'),
value: html
});
return callback && callback(null, {
regExp: new RegExp(result, 'gmi'),
value: html
});
});
}));
// Remove tag handling
promises.push(
helpers.replace(html, remove_tag, function handleRemoveMarker(result, callback) {
return callback(null, {
regExp: null,
value: ""
});
return callback && callback(null, {
regExp: null,
value: ''
});
}, true));
return promises;
@@ -241,7 +243,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
return text.replace(result.regExp, result.replacement);
}
helpers.handTextManipulation(content,
return helpers.handTextManipulation(content,
promises,
handler,
callback
@@ -249,7 +251,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) {
};
return {
getPreviewHTML: getPreviewHTML,
getPreviewHTML: getPreviewHTML,
_replaceMarkers: replaceMarkers
};
};

View File

@@ -1,10 +1,9 @@
'use strict';
/*
* options.ext: determines content-type
* options.attribute: sends result[options.attribute] in stead of result
*/
module.exports = function (config, options, res, next) {
"use strict";
return function responder(err, result) {
if (err) {
config.error && config.error('ERR responder', options, err);

View File

@@ -1,20 +1,20 @@
'use strict';
var responder = require('./responder.js');
var path = require('path');
var fs = require('fs');
var fs = require('fs');
module.exports = function (app, handlers, markers, config) {
"use strict";
var route;
route = config.api.data + '/:collection/:guid/:attribute.:ext(css|less|js|html)';
app.get(route,
function getAttributeByGUID(req, res, next) {
config.debug && config.debug('/data/:collection/:guid/:attribute.:ext(less|js|html)');
config.debug && config.debug(config.api.data + '/:collection/:guid/:attribute.:ext(less|js|html)');
var options = {
collection: req.params.collection,
attribute: req.params.attribute,
ext: req.params.ext,
query: {_id: req.params.guid}
attribute: req.params.attribute,
ext: req.params.ext,
query: {_id: req.params.guid}
};
handlers.getAttribute(options,
responder(config, options, res, next)
@@ -25,11 +25,11 @@ module.exports = function (app, handlers, markers, config) {
route = config.api.data + '/:collection/:guid.:ext(json)';
app.get(route,
function getContentByGUID(req, res, next) {
config.debug && config.debug('/data/:collection/:guid.:ext(json)');
config.debug && config.debug(config.api.data + '/:collection/:guid.:ext(json)');
var options = {
collection: req.params.collection,
ext: req.params.ext,
query: {_id: req.params.guid}
ext: req.params.ext,
query: {_id: req.params.guid}
};
handlers.getContent(options,
responder(config, options, res, next)
@@ -40,12 +40,12 @@ module.exports = function (app, handlers, markers, config) {
route = config.api.content + '/:collection/:name/:attribute.:ext(css|less|js|html)';
app.get(route,
function getAttributeByName(req, res, next) {
config.debug && config.debug('/content/:collection/:name/:attribute.:ext(less|js|html)');
config.debug && config.debug(config.api.content + '/:collection/:name/:attribute.:ext(less|js|html)');
var options = {
collection: req.params.collection,
attribute: req.params.attribute,
ext: req.params.ext,
query: {name: req.params.name}
attribute: req.params.attribute,
ext: req.params.ext,
query: {name: req.params.name}
};
handlers.getAttribute(options,
responder(config, options, res, next)
@@ -53,14 +53,14 @@ module.exports = function (app, handlers, markers, config) {
}
);
route = config.api.content + '/content/:collection/:name.:ext(json)';
route = config.api.content + '/:collection/:name.:ext(json)';
app.get(route,
function getContentByName(req, res, next) {
config.debug && config.debug('/content/:collection/:name.:ext(json)');
config.debug && config.debug(config.api.content + '/:collection/:name.:ext(json)');
var options = {
collection: req.params.collection,
ext: req.params.ext,
query: {name: req.params.name}
ext: req.params.ext,
query: {name: req.params.name}
};
handlers.getContent(options,
responder(config, options, res, next)
@@ -72,19 +72,20 @@ module.exports = function (app, handlers, markers, config) {
route = config.api.preview + '/:collection/:name.:ext(html|md)';
app.get(route,
function getPreviewContent(req, res, next) {
config.debug && config.debug('/page/:collection/:name.:ext(html|md)');
config.debug && config.debug(config.api.preview + '/:collection/:name.:ext(html|md)');
var options = {
collection: req.params.collection,
ext: req.params.ext,
query: {name: req.params.name},
req: { query: req.query || {},
ext: req.params.ext,
query: {name: req.params.name},
req: {
query: req.query || {},
headers: req.headers
}
};
if (options.ext === 'md') {
var attribute_parts = options.query.name.split('.');
var attribute_parts = options.query.name.split('.');
var markdownDocument = markers.createTag('markdown', options.collection, attribute_parts[0], attribute_parts[1]);
return handlers.getPreviewHTML(markdownDocument, { req: options.req },
return handlers.getPreviewHTML(markdownDocument, {req: options.req},
responder(config, options, res, next)
);
}
@@ -94,20 +95,21 @@ module.exports = function (app, handlers, markers, config) {
}
if (result) {
var attribute_parts = options.query.name.split('.');
var attribute = attribute_parts[attribute_parts.length - 1];
var attribute = attribute_parts[attribute_parts.length - 1];
var attribute_value = result[attribute];
if (attribute_value) {
options.name = attribute_parts[0];
options.name = attribute_parts[0];
var preview_options = {
collection: options.collection,
name: options.name,
attribute: attribute,
query: {_id: result._id},
req: options.req
name: options.name,
attribute: attribute,
query: {_id: result._id},
req: options.req
};
config.debug && config.debug('getPreviewContent content', attribute_value);
handlers.getPreviewHTML(attribute_value, preview_options,
handlers.getPreviewHTML(attribute_value,
preview_options,
responder(config, options, res, next)
);
} else {
@@ -124,7 +126,7 @@ module.exports = function (app, handlers, markers, config) {
route = config.api.importer + '/:filename';
app.get(route, function importFile(req, res, next) {
var filename = path.resolve(config.statics.importer_path, req.params.filename);
config.debug && config.debug('/importer/:filename', filename);
config.debug && config.debug(config.api.importer + '/:filename', filename);
fs.readFile(filename, 'utf-8', function handleFileContent(err, sub_doc) {
if (err) {
config.error && config.error('ERR readFile', filename, err);

View File

@@ -1,19 +1,19 @@
'use strict';
var ShareJS = require('share');
module.exports = function (config, app, db) {
"use strict";
// share wraps express app with http.Server
if (config
&& config.share
&& config.share.db
&& config.share.db.type === 'mongo') {
if (config &&
config.share &&
config.share.db &&
config.share.db.type === 'mongo') {
config.share.db.client = db;
}
var server = ShareJS.server.attach(app, config.share);
var model = app.model;
var model = app.model;
return {
server: server,
model: model
model: model
};
};

View File

@@ -1,5 +1,5 @@
'use strict';
module.exports = function (config, model, mongoDataInstance) {
"use strict";
var timers = {};
function handleMongoGetResult(options) {
@@ -13,18 +13,18 @@ module.exports = function (config, model, mongoDataInstance) {
if (options.type === 'json') {
result[options.attribute] = {};
} else {
result[options.attribute] = "";
result[options.attribute] = '';
}
}
notFound = true;
} else {
config.error && config.error('ERR1 handleMongoGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err);
config.error && config.error('ERR1 handleMongoGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err);
}
}
if (result || notFound) {
var operation = null;
config.debug && config.debug('handleMongoGetResult options', options, result);
var data = result;
var data = result;
if (options.attribute) {
data = result[options.attribute];
}
@@ -33,13 +33,19 @@ module.exports = function (config, model, mongoDataInstance) {
if (data instanceof String) {
data = JSON.parse(data);
}
operation = { op: [
{ p: [], oi: data, od: null }
], v: version };
operation = {
op: [
{p: [], oi: data, od: null}
],
v: version
};
} else if (options.type === 'text') {
operation = { op: [
{i: data, p: 0}
], v: version };
operation = {
op: [
{i: data, p: 0}
],
v: version
};
}
if (operation) {
model.applyOp(options.documentId, operation, function appliedOp(error, version) {
@@ -60,13 +66,13 @@ module.exports = function (config, model, mongoDataInstance) {
var splitId = documentId.split(':');
var options = {
documentId: documentId,
type: splitId[0],
type: splitId[0],
collection: splitId[1],
attribute: null
attribute: null
};
if (splitId.length === 4) {
// options.query = {_id: splitId[2]};
options.query = {name: splitId[2]};
options.query = {name: splitId[2]};
options.attribute = splitId[3];
mongoDataInstance.getMongoAttribute(options, handleMongoGetResult(options));
} else {
@@ -79,15 +85,18 @@ module.exports = function (config, model, mongoDataInstance) {
function handleMongoSetResult(options, current, callback) {
function handleResult(err, result) {
if (err) {
config.error && config.error('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err);
config.error && config.error('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err);
return callback && callback(err);
}
config.debug && config.debug('current', current, 'result', result, 'options', options);
if ((!current || !current.name) && (result.name || options.name)) {
var name = result.name || options.name;
var operation = { op: [
{ p: ['name'], oi: name, od: null }
], v: options.operation.v };
var name = result.name || options.name;
var operation = {
op: [
{p: ['name'], oi: name, od: null}
],
v: options.operation.v
};
model.applyOp(options.documentId, operation, function appliedOp(error, version) {
config.debug && config.debug('setResult applyOp version', version);
if (error) {
@@ -97,7 +106,7 @@ module.exports = function (config, model, mongoDataInstance) {
return callback && callback(null, version);
});
} else {
return callback(null, null);
return callback && callback(null, null);
}
}
@@ -107,11 +116,11 @@ module.exports = function (config, model, mongoDataInstance) {
function handleMongoAttributeSetResult(options, current, callback) {
function handleResult(err, result) {
if (err) {
config.error && config.error('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || "", err);
return callback(err);
config.error && config.error('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err);
return callback && callback(err);
}
config.debug && config.debug('current', current, 'result', result);
return callback(null, null);
return callback && callback(null, null);
}
return handleResult;
@@ -160,31 +169,31 @@ module.exports = function (config, model, mongoDataInstance) {
return;
}
var splitId = documentId.split(':');
var options = {
var splitId = documentId.split(':');
var options = {
documentId: documentId,
type: splitId[0],
type: splitId[0],
collection: splitId[1],
name: splitId[2],
attribute: null,
operation: operation,
no_share: true // prevent circular updates.
name: splitId[2],
attribute: null,
operation: operation,
no_share: true // prevent circular updates.
};
var timer = {
var timer = {
current: current,
options: options
};
var attribute = false;
if (splitId.length === 4) {
// options.query = {_id: splitId[2]};
options.query = {name: splitId[2]};
options.query = {name: splitId[2]};
options.attribute = splitId[3];
attribute = true;
attribute = true;
} else {
options.query = {name: splitId[2]};
}
if (timers[documentId]) {
timer.timer_id = timers[documentId].timer_id;
timer.timer_id = timers[documentId].timer_id;
timers[documentId] = timer;
config.debug && config.debug('resetting timer', documentId);
} else {