mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
Extract database-forms into an Atmosphere package and remove it from the source code.
This commit is contained in:
parent
31571aa055
commit
7d7137527a
8 changed files with 12 additions and 7138 deletions
|
@ -13,3 +13,4 @@ moment
|
|||
spiderable
|
||||
email
|
||||
profiler
|
||||
database-forms
|
|
@ -1,63 +0,0 @@
|
|||
/* Simple JavaScript Inheritance
|
||||
* By John Resig http://ejohn.org/
|
||||
* MIT Licensed.
|
||||
*/
|
||||
// Inspired by base2 and Prototype
|
||||
(function(){
|
||||
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
|
||||
// The base Class implementation (does nothing)
|
||||
this.Class = function(){};
|
||||
|
||||
// Create a new Class that inherits from this class
|
||||
Class.extend = function(prop) {
|
||||
var _super = this.prototype;
|
||||
|
||||
// Instantiate a base class (but only create the instance,
|
||||
// don't run the init constructor)
|
||||
initializing = true;
|
||||
var prototype = new this();
|
||||
initializing = false;
|
||||
|
||||
// Copy the properties over onto the new prototype
|
||||
for (var name in prop) {
|
||||
// Check if we're overwriting an existing function
|
||||
prototype[name] = typeof prop[name] == "function" &&
|
||||
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
|
||||
(function(name, fn){
|
||||
return function() {
|
||||
var tmp = this._super;
|
||||
|
||||
// Add a new ._super() method that is the same method
|
||||
// but on the super-class
|
||||
this._super = _super[name];
|
||||
|
||||
// The method only need to be bound temporarily, so we
|
||||
// remove it when we're done executing
|
||||
var ret = fn.apply(this, arguments);
|
||||
this._super = tmp;
|
||||
|
||||
return ret;
|
||||
};
|
||||
})(name, prop[name]) :
|
||||
prop[name];
|
||||
}
|
||||
|
||||
// The dummy class constructor
|
||||
function Class() {
|
||||
// All construction is actually done in the init method
|
||||
if ( !initializing && this.init )
|
||||
this.init.apply(this, arguments);
|
||||
}
|
||||
|
||||
// Populate our constructed prototype object
|
||||
Class.prototype = prototype;
|
||||
|
||||
// Enforce the constructor to be what we expect
|
||||
Class.prototype.constructor = Class;
|
||||
|
||||
// And make this class extendable
|
||||
Class.extend = arguments.callee;
|
||||
|
||||
return Class;
|
||||
};
|
||||
})();
|
|
@ -1,26 +0,0 @@
|
|||
// A class that generates a form on the current page based on a FromModel object (see form_model.js)
|
||||
var DatabaseForm = Class.extend({
|
||||
|
||||
// Grab the form element from the DOM and add inputs to it based on the model object provided
|
||||
generateFor: function (model, formSelector) {
|
||||
this.model = model;
|
||||
this.formOptions = model.formOptions;
|
||||
|
||||
$(formSelector).jsonForm({ schema: this.model.formSchema() });
|
||||
},
|
||||
|
||||
// Read the values currently entered into the form and set them onto the model. Save the model to the database.
|
||||
submit: function (createHandler, updateHandler) {
|
||||
this.updateModelFromFormValues()
|
||||
this.model.save(createHandler, updateHandler)
|
||||
},
|
||||
|
||||
updateModelFromFormValues: function() {
|
||||
for (field in this.model.schema) {
|
||||
var regexExpression = ':regex(id, jsonform.*' + field + ')';
|
||||
var htmlElement = $(regexExpression);
|
||||
if (this.model.schema[field].constructor == Boolean) this.model.schema[field] = !!htmlElement.attr('checked');
|
||||
else this.model.schema[field] = htmlElement.val();
|
||||
}
|
||||
}
|
||||
});
|
|
@ -1,124 +0,0 @@
|
|||
// A utility class that we use in FormModel to set up a convention for the default labels for each form input field.
|
||||
var StringUtils = {
|
||||
|
||||
/*
|
||||
Assumes that the argument is a camel-case string.
|
||||
It places spaces between each word and capitalizes the first letter. So, a string like "camelCase" becomes "Camel Case".
|
||||
*/
|
||||
humanize: function(string) {
|
||||
return this.capitalize(this.convertCamelToSpaces(string));
|
||||
},
|
||||
|
||||
capitalize: function(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1);
|
||||
},
|
||||
|
||||
convertCamelToSpaces: function(string) {
|
||||
return string.replace(/([A-Z])/g, function(match) {
|
||||
return ' ' + match;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// A class that provides all the functionality required to generate a form from a database record.
|
||||
var FormModel = Class.extend({
|
||||
|
||||
/*
|
||||
Class constructor:
|
||||
collection: The MongoDB collection the record will be saved to.
|
||||
data: The record data if generating a form for an already-existing record. This argument is optional.
|
||||
*/
|
||||
init: function(collection, data) {
|
||||
this.collection = collection;
|
||||
|
||||
/*
|
||||
blankSchema is an object that each extending class must provide.
|
||||
It contains all the fields to be saved to the database and their default values.
|
||||
The schema field holds the data to be saved to the database.
|
||||
*/
|
||||
this.schema = this.blankSchema;
|
||||
|
||||
// jsonform options that would override the defaults
|
||||
this.formOptions = {};
|
||||
|
||||
this.load(data)
|
||||
},
|
||||
|
||||
// If data has been provided in the constructor, load it into the current object
|
||||
load: function(data) {
|
||||
if (data) {
|
||||
this._id = data._id;
|
||||
|
||||
for (field in this.schema) {
|
||||
if (data[field]) this.schema[field] = data[field];
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// Generate a jsonform schema for this object based on the form options given and conventions
|
||||
formSchema: function() {
|
||||
var formSchema = {};
|
||||
|
||||
// Each field stored in the database is represented by a form input
|
||||
for (var field in this.schema) {
|
||||
|
||||
formSchema[field] = {
|
||||
// If type is already given in formOptions, use that, otherwise guess the input type by inspecting the field type
|
||||
type: this.option(field, 'type') || this.schema[field].constructor.name.toLowerCase(),
|
||||
|
||||
// If the label text is already specified in formOptions, use that, otherwise use a humanized version of the field name (see StringUtils)
|
||||
title: this.option(field, 'title') || StringUtils.humanize(field),
|
||||
|
||||
// Use the field name as an ID; jsonform will add a prefix to avoid name conflicts
|
||||
id: field,
|
||||
|
||||
// Display the data in the input
|
||||
default: this.schema[field]
|
||||
}
|
||||
|
||||
// If an 'enum' option is provided in formOptions, turn the input into a select by adding the enum options to the jsonform schema
|
||||
if(this.option(field, 'enum')) formSchema[field]['enum'] = this.option(field, 'enum');
|
||||
}
|
||||
|
||||
return formSchema;
|
||||
},
|
||||
|
||||
option: function(field, optionName) {
|
||||
if (this.formOptions[field]) return this.formOptions[field][optionName];
|
||||
return null;
|
||||
},
|
||||
|
||||
// Overwrite the default label for a field by adding a 'title' entry to its form options
|
||||
overwriteTitle: function(field, title) {
|
||||
if (this.formOptions[field]) this.formOptions[field]['title'] = title;
|
||||
else this.formOptions[field] = { 'title': title };
|
||||
},
|
||||
|
||||
// Overwrite the default type for a field by adding a 'type' entry to its form options
|
||||
overwriteType: function(field, type) {
|
||||
if (this.formOptions[field]) this.formOptions[field]['type'] = type;
|
||||
else this.formOptions[field] = { 'type': type };
|
||||
},
|
||||
|
||||
// Make the input type of a string field be a select by editing formOptions appropriately
|
||||
makeSelect: function(field, options) {
|
||||
if (this.formOptions[field]) this.formOptions[field]['enum'] = options;
|
||||
else this.formOptions[field] = { 'enum': options };
|
||||
},
|
||||
|
||||
/*
|
||||
If the record was originally loaded from the database, update it.
|
||||
Otherwise, create a new record with the appropriate data.
|
||||
|
||||
createHandler, updateHandler: Functions to run after the database operations completes (such as displaying a message confirming things were saved properly).
|
||||
These usually come from a DatabaseForm, which in turn gets them from a template helper.
|
||||
*/
|
||||
save: function(createHandler, updateHandler) {
|
||||
if (this._id) {
|
||||
this.collection.update(this._id, {$set: this.schema}, updateHandler);
|
||||
} else {
|
||||
this._id = this.collection.insert(this.schema, createHandler);
|
||||
}
|
||||
}
|
||||
});
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -5,6 +5,7 @@
|
|||
"packages": {
|
||||
"router": {},
|
||||
"moment": {},
|
||||
"profiler": {"git": "https://github.com/tmeasday/meteor-profiler.git"}
|
||||
"profiler": {"git": "https://github.com/tmeasday/meteor-profiler.git"},
|
||||
"database-forms": {}
|
||||
}
|
||||
}
|
||||
|
|
12
smart.lock
12
smart.lock
|
@ -11,7 +11,8 @@
|
|||
"profiler": {
|
||||
"git": "https://github.com/tmeasday/meteor-profiler.git",
|
||||
"branch": "master"
|
||||
}
|
||||
},
|
||||
"database-forms": {}
|
||||
},
|
||||
"packages": {
|
||||
"router": {
|
||||
|
@ -29,10 +30,15 @@
|
|||
"branch": "master",
|
||||
"commit": "25a14a4e94a4748745738031baab1400860eede7"
|
||||
},
|
||||
"database-forms": {
|
||||
"git": "https://github.com/lyudmil/database-forms.git",
|
||||
"tag": "v0.0.1",
|
||||
"commit": "3e458c17f1c6e3499188be01182cdade92683357"
|
||||
},
|
||||
"page-js": {
|
||||
"git": "https://github.com/tmeasday/meteor-page-js.git",
|
||||
"tag": "v1.3.1",
|
||||
"commit": "ee0b651869928abe50b5d06d7f32ccd2e657883a"
|
||||
"tag": "v1.3.3",
|
||||
"commit": "c1e9832af3fc2b2f5139a16d93a82ca6fa0655da"
|
||||
},
|
||||
"node-modules": {
|
||||
"git": "https://github.com/possibilities/meteor-node-modules.git",
|
||||
|
|
Loading…
Add table
Reference in a new issue