Created
March 16, 2017 11:07
-
-
Save formix/e2238022d0a2a72ddef908acb98ca34b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var extend = require("util")._extend; | |
module.exports.example1 = function() { | |
return { | |
characteristics: { | |
length: extend(new Char("length"), { | |
validate: validateLength | |
}), | |
curvedEdges: extend(new Char("curvedEdges", "single-select"), { | |
options: [ | |
new CharValue("None", 0), | |
new CharValue("1", 1), | |
new CharValue("2", 2), | |
new CharValue("3", 3) | |
], | |
validate: validateCurvedEdges | |
}), | |
} | |
}; | |
}; | |
///// Rules used in the model \\\\\ | |
function validateLength(next) { | |
var selection = this.get("selection"); | |
if (isNaN(selection)) { // only number required | |
this.set("valid", false); | |
this.set("invalidMessage", "Length must be a number."); | |
return next(); | |
} | |
var length = Number(selection); | |
if (length < 20) { | |
this.set("valid", false); | |
this.set("invalidMessage", "Length must be greater or equal to 20."); | |
return next(); | |
} | |
if (length > 44) { | |
this.set("valid", false); | |
this.set("invalidMessage", "Length must be lower or equal to 44."); | |
return next(); | |
} | |
return next(); | |
} | |
function validateCurvedEdges(next) { | |
var lengthSelection = this.get("../length/selection"); | |
var lengthValid = this.get("../length/valid"); | |
if (lengthValid) { | |
var length = Number(lengthSelection); | |
if (length > 36) { | |
var selection = this.get("selection"); | |
var options = this.get("options"); | |
var selectedCurvedEdges = getSelectedOption(selection, options); | |
if (selectedCurvedEdges.value === 3) { | |
this.set("valid", false); | |
this.set("invalidMessage", | |
"When length is greater than 36, curved edges " + | |
"can not be 3."); | |
return next(); | |
} | |
} | |
} | |
return next(); | |
} | |
///// Future infernal-framework \\\\\ | |
var charCount = 0; | |
var charTypes = ["text", "single-select"]; | |
// Creates a new characteristic | |
function Char(name, type) { | |
if (typeof name === "undefined") { | |
name = "char" + charCount; | |
charCount++; | |
} | |
if (typeof type === "undefined") { | |
type = "text"; | |
} | |
if (charTypes.indexOf(type) === -1) { | |
throw new Error("the characteristic type must be one of " + | |
JSON.stringify(charTypes)); | |
} | |
this.name = name; | |
this.text = name; | |
this.selection = null; | |
this.options = []; | |
this.valid = true; | |
this.invalidMessage = ""; | |
this.allowNull = true; | |
this.visible = true; | |
if (type === "single-select") { | |
this.validateSelection = validateSingleSelection; | |
} | |
} | |
// Creates a new characteristic value | |
function CharValue(name, value) { | |
this.name = name; | |
this.value = value; | |
this.text = value; | |
this.available = true; | |
} | |
// validate the selection againsts available options | |
function validateSingleSelection(next) { | |
var type = this.get("type"); | |
if (type !== "single-select") { | |
return next(); | |
} | |
var allowNull = this.get("allowNull"); | |
var selection = this.get("selection"); | |
if (!selection && !allowNull) { | |
this.set("valid", false); | |
this.set("invalidMessage", "Empty value not allowed."); | |
return next(); | |
} | |
var options = this.get("options"); | |
var selectedOption = getSelectedOption(selection, options); | |
if (!selectedOption) { | |
this.set("valid", false); | |
this.set("invalidMessage", | |
"The selection is not found in options."); | |
return next(); | |
} | |
if (!selectedOption.available) { | |
this.set("valid", false); | |
this.set("invalidMessage", | |
"This selection is not an available option."); | |
return next(); | |
} | |
return next(); | |
} | |
function getSelectedOption(selection, options) { | |
var selectedOption = null; | |
if (typeof selection === "string") { | |
// finds the corresponding value in options | |
for (var i = 0; i < options.length && !selectedOption; i++) { | |
if (options[i].name === selection) { | |
selectedOption = option[i]; | |
} | |
} | |
} else { | |
if (options.indexOf(selection) !== -1) { | |
selectedOption = selection; | |
} | |
} | |
return selectedOption; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is untested yet! It is currently an incomplete answer to formix/infernal-engine#39