-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This and the following commits should be PR ready, and should not break existing routes. This means refactored code that affect more than one route will be duplicated and renamed. We can easily cleanup extra code after implementing the entire refactor.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
var moment = require('moment'); | ||
|
||
/** | ||
* Parse persisted model data and return a new object with additional generated fields used in view templates. | ||
*/ | ||
|
||
var Script = require('../models/script').Script; | ||
|
||
/** | ||
* Script | ||
*/ | ||
|
||
// Urls | ||
var getScriptPageUrl = function(script) { | ||
var isLib = script.isLib || false; | ||
var scriptPath = script.installName | ||
.replace(isLib ? /\.js$/ : /\.user\.js$/, ''); | ||
return (isLib ? '/libs/' : '/scripts/') + scriptPath; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
}; | ||
|
||
var getScriptViewSourcePageUrl = function(script) { | ||
return getScriptPageUrl(script) + '/source'; | ||
}; | ||
|
||
var getScriptEditAboutPageUrl = function(script) { | ||
var isLib = script.isLib || false; | ||
var scriptPath = script.installName | ||
.replace(isLib ? /\.js$/ : /\.user\.js$/, ''); | ||
var editUrl = scriptPath.split('/'); | ||
editUrl.shift(); | ||
return (isLib ? '/lib/' : '/script/') + editUrl.join('/') + '/edit'; | ||
}; | ||
|
||
var getScriptEditSourcePageUrl = function(script) { | ||
return getScriptViewSourcePageUrl(script); | ||
}; | ||
|
||
var getScriptInstallPageUrl = function(script) { | ||
var isLib = script.isLib || false; | ||
return (isLib ? '/libs/src/' : '/install/') + script.installName; | ||
}; | ||
|
||
// | ||
exports.parseScript = function(scriptData) { | ||
var script = scriptData.toObject ? scriptData.toObject() : scriptData; | ||
|
||
// Script Good/Bad bar. | ||
// script.votes = upvotes | ||
// script.flags = downvotes + flags? | ||
var sumVotesAndFlags = script.votes + script.flags; | ||
var votesRatio = sumVotesAndFlags > 0 ? script.votes / sumVotesAndFlags : 0; | ||
var flagsRatio = sumVotesAndFlags > 0 ? script.flags / sumVotesAndFlags : 0; | ||
script.votesPercent = votesRatio * 100; | ||
script.flagsPercent = flagsRatio * 100; | ||
|
||
// Urls: Public | ||
script.scriptPageUrl = getScriptPageUrl(script); | ||
script.scriptInstallPageUrl = getScriptInstallPageUrl(script); | ||
script.scriptViewSourcePageUrl = getScriptViewSourcePageUrl(script); | ||
|
||
// Urls: Author | ||
script.scriptEditMetadataPageUrl = getScriptEditAboutPageUrl(script); | ||
script.scriptEditSourcePageUrl = getScriptEditSourcePageUrl(script); | ||
|
||
// Dates | ||
script.updatedISOFormat = script.updated.toISOString(); | ||
script.updatedHumanized = moment(script.updated).fromNow(); | ||
|
||
return script; | ||
}; | ||
|
||
/** | ||
* User | ||
*/ | ||
|
||
var getUserPageUrl = function(script) { | ||
var isLib = script.isLib || false; | ||
return (isLib ? '/libs/src/' : '/install/') + script.installName; | ||
}; | ||
|
||
// | ||
exports.parseUser = function(userData) { | ||
var user = userData.toObject ? userData.toObject() : userData; | ||
|
||
// Urls: Public | ||
user.userPageUrl = '/users/' + user.name; | ||
|
||
return user; | ||
}; | ||
|
||
/** | ||
* Group | ||
*/ | ||
|
||
// | ||
exports.parseGroup = function(groupData) { | ||
var group = groupData.toObject ? groupData.toObject() : groupData; | ||
|
||
group.size = group._scriptIds.length; | ||
group.multiple = group._scriptIds.length > 1; | ||
|
||
group.groupPageUrl = '/group/' + group.name.replace(/\s+/g, '_'); | ||
|
||
return group; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var orderDirs = ['asc', 'desc']; | ||
function parseScriptsSortQuery(scriptsQuery, query, defaultFn) { | ||
if (query.orderBy) { | ||
var orderBy = query.orderBy; | ||
var orderDir = query.orderDir; | ||
if (_.isUndefined(query.orderDir) || !_.contains(orderDirs, orderDir)) | ||
orderDir = 'asc'; | ||
|
||
if (_.has(Script.schema.paths, query.orderBy)) { | ||
var sortBy = {}; | ||
sortBy[orderBy] = orderDir; | ||
scriptsQuery.sort(sortBy); | ||
return; | ||
} | ||
} | ||
defaultFn(scriptsQuery); | ||
} | ||
|
||
var parseSearchConditions = function(q, prefixSearchFields, fullSearchFields) { | ||
var conditions = []; | ||
var query = null; | ||
var prefixStr = ''; | ||
var fullStr = ''; | ||
var prefixRegex = null; | ||
var fullRegex = null; | ||
var terms = q.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1').split(/\s+/); | ||
|
||
// Match all the terms but in any order | ||
terms.forEach(function (term) { | ||
prefixStr += '(?=.*?\\b' + term + ')'; | ||
fullStr += '(?=.*?' + term + ')'; | ||
}); | ||
prefixRegex = new RegExp(prefixStr, 'i'); | ||
fullRegex = new RegExp(fullStr, 'i'); | ||
|
||
// One of the searchable fields must match the conditions | ||
prefixSearchFields.forEach(function (prop) { | ||
var condition = {}; | ||
condition[prop] = prefixRegex; | ||
conditions.push(condition); | ||
}); | ||
|
||
fullSearchFields.forEach(function (prop) { | ||
var condition = {}; | ||
condition[prop] = fullRegex; | ||
conditions.push(condition); | ||
}); | ||
return conditions; | ||
}; | ||
exports.parseSearchConditions = parseSearchConditions; | ||
|
||
exports.parseScriptSearchQuery = function(scriptsQuery, query) { | ||
var q = unescape(query); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Martii
Member
|
||
var partialWordMatchFields = ['name', 'author', 'about', 'meta.description']; | ||
var fullWordMatchFields = ['meta.include', 'meta.match']; | ||
scriptsQuery.find({ | ||
'$or': parseSearchConditions(q, partialWordMatchFields, fullWordMatchFields) | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
exports.paginateTemplate = function(opts) { | ||
// Required | ||
var currentPage = opts.currentPage; | ||
var lastPage = opts.lastPage; | ||
var urlFn = opts.urlFn; | ||
|
||
// Optional | ||
var distVisible = opts.distVisible || 4; | ||
var firstVisible = opts.firstVisible || true; | ||
var lastVisible = opts.firstVisible || true; | ||
|
||
var linkedPages = []; | ||
|
||
for (var i = Math.max(1, currentPage - distVisible); i <= Math.min(currentPage + distVisible, lastPage); i++) | ||
linkedPages.push(i); | ||
|
||
if (firstVisible && linkedPages.length > 0 && linkedPages[0] != 1) | ||
linkedPages.splice(0, 0, 1); // insert the value 1 at index 0 | ||
|
||
if (lastVisible && linkedPages.length > 0 && linkedPages[linkedPages.length-1] != lastPage) | ||
linkedPages.push(lastPage); | ||
|
||
|
||
var html = ''; | ||
html += '<ul class="pagination">'; | ||
for (var i = 0; i < linkedPages.length; i++) { | ||
var linkedPage = linkedPages[i]; | ||
html += '<li'; | ||
if (linkedPage == currentPage) | ||
html += ' class="active"'; | ||
html += '>'; | ||
html += '<a href="'; | ||
html += urlFn(linkedPage); | ||
html += '">'; | ||
html += linkedPage; | ||
html += '</a>'; | ||
html += '</li>'; | ||
} | ||
html += '</ul>'; | ||
return html; | ||
} |
Should be:
return (isLib ? '/libs/' : '/scripts/') + encodeURI(scriptPath);
#200