docpad
Version:
DocPad is a dynamic static site generator. Write your content as files, or import your content from other external sources. Render the content with plugins. And deploy your static or dynamic website to your favourite hosting provider.
128 lines (115 loc) • 3.24 kB
JavaScript
// Generated by CoffeeScript 2.5.1
// =====================================
// Requires
// Standard Library
var FileModel, FilesCollection, Model, QueryCollection, pathUtil;
pathUtil = require('path');
// Local
({QueryCollection, Model} = require('../base'));
FileModel = require('../models/file');
FilesCollection = (function() {
// =====================================
// Classes
/**
* The DocPad files and documents query collection class
* Extends the DocPad QueryCollection class
* Used as the query collection class for DocPad files and documents.
* This differs from standard collections in that it provides backbone.js,
* noSQL style methods for querying the file system. In DocPad this
* is the various files that make up a web project. Typically this is the documents,
* css, script and image files.
*
* Most often a developer will use this class to find (and possibly sort) documents,
* such as blog posts, by some criteria.
* posts: ->
* @getCollection('documents').findAllLive({relativeOutDirPath: 'posts'},[{date:-1}])
* @class FilesCollection
* @constructor
* @extends QueryCollection
*/
class FilesCollection extends QueryCollection {
/**
* Initialize the collection
* @private
* @method initialize
* @param {Object} attrs
* @param {Object} [opts={}]
*/
initialize(attrs, opts = {}) {
var base;
if (this.options == null) {
this.options = {};
}
if ((base = this.options).name == null) {
base.name = opts.name || null;
}
return super.initialize(...arguments);
}
/**
* Fuzzy find one
* Useful for layout searching
* @method fuzzyFindOne
* @param {Object} data
* @param {Object} sorting
* @param {Object} paging
* @return {Object} the file, if found
*/
fuzzyFindOne(data, sorting, paging) {
var escapedData, file, i, len, queries, query;
// Prepare
escapedData = data != null ? data.replace(/[\/]/g, pathUtil.sep) : void 0;
queries = [
{
relativePath: escapedData
},
{
relativeBase: escapedData
},
{
url: data
},
{
relativePath: {
$startsWith: escapedData
}
},
{
fullPath: {
$startsWith: escapedData
}
},
{
url: {
$startsWith: data
}
}
];
// Try the queries
for (i = 0, len = queries.length; i < len; i++) {
query = queries[i];
file = this.findOne(query, sorting, paging);
if (file) {
return file;
}
}
// Didn't find a file
return null;
}
};
/**
* Base Model for all items in this collection
* @private
* @property {Object} model
*/
FilesCollection.prototype.model = FileModel;
/**
* Base Collection for all child collections
* @private
* @property {Object} collection
*/
FilesCollection.prototype.collection = FilesCollection;
return FilesCollection;
}).call(this);
// =====================================
// Export
module.exports = FilesCollection;