Socket
Socket
Sign inDemoInstall

ampersand-checkbox-view

Package Overview
Dependencies
90
Maintainers
6
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.0.2 to 3.0.0

.travis.yml

198

ampersand-checkbox-view.js
/*$AMPERSAND_VERSION*/
var View = require('ampersand-view');
var domify = require('domify');

@@ -17,110 +18,103 @@ var dom = require('ampersand-dom');

module.exports = View.extend({
initialize: function(opts) {
if (!opts || !opts.name) throw new Error('must pass in a name');
function CheckboxView(opts) {
if (!opts || !opts.name) throw new Error('must pass in a name');
// settings
this.el = opts.el;
this.name = opts.name;
this.startingValue = !!opts.value;
this.value = this.startingValue;
this.label = opts.label || opts.name;
this.required = (typeof opts.required === 'boolean') ? opts.required : false;
this.validClass = opts.validClass || 'input-valid';
this.invalidClass = opts.invalidClass || 'input-invalid';
this.requiredMessage = opts.requiredMessage || 'This box must be checked.';
this.parent = opts.parent || this.parent;
this.autoRender = opts.autoRender;
this.template = opts.template || template;
this.beforeSubmit = opts.beforeSubmit || this.beforeSubmit;
// settings
this.name = opts.name;
this.value = (opts.value === true) || false;
this.el = opts.el;
this.template = opts.template || template;
this.label = opts.label || opts.name;
this.required = (typeof opts.required === 'boolean') ? opts.required : false;
this.validClass = opts.validClass || 'input-valid';
this.invalidClass = opts.invalidClass || 'input-invalid';
this.requiredMessage = opts.requiredMessage || 'This box must be checked.';
this.parent = opts.parent;
this.setValue(this.value);
},
clear: function () {
return this.setValue(false);
},
reset: function () {
return this.setValue(this.startingValue);
},
remove: function () {
if (this.input) this.input.removeEventListener('change', this.handleInputEvent, false);
View.prototype.remove.call(this);
},
render: function () {
// only allow this to be called once
if (this.rendered) return;
var newDom = domify(this.template);
var parent = this.el && this.el.parentNode;
if (parent) parent.replaceChild(newDom, this.el);
this.el = newDom;
this.input = this.el.querySelector('input');
this.labelEl = this.el.querySelector('[data-hook~=label]');
this.messageContainer = this.el.querySelector('[data-hook~=message-container]');
this.messageEl = this.el.querySelector('[data-hook~=message-text]');
// render right away
this.render();
// bind event handlers
this.handleInputEvent = this.handleInputEvent.bind(this);
// bind event handlers
this.handleInputEvent = this.handleInputEvent.bind(this);
// add our event handlers
this.input.addEventListener('change', this.handleInputEvent, false);
// add our event handlers
this.input.addEventListener('change', this.handleInputEvent, false);
if (opts.value) this.setValue(true);
this.test();
}
// remove and destroy element
CheckboxView.prototype.remove = function () {
this.input.removeEventListener('change', this.handleInputEvent, false);
var parent = this.el.parentNode;
if (parent) parent.removeChild(this.el);
};
// very `manual` render to avoid dependencies
CheckboxView.prototype.render = function () {
// only allow this to be called once
if (this.rendered) return;
var newDom = domify(this.template);
var parent = this.el && this.el.parentNode;
if (parent) parent.replaceChild(newDom, this.el);
this.el = newDom;
this.input = this.el.querySelector('input');
this.labelEl = this.el.querySelector('[data-hook~=label]');
this.messageContainer = this.el.querySelector('[data-hook~=message-container]');
this.messageEl = this.el.querySelector('[data-hook~=message-text]');
this.setMessage(this.message);
this.input.checked = !!this.value;
this.input.name = this.name;
this.labelEl.textContent = this.label;
this.rendered = true;
};
// handle input events and show appropriate errors
CheckboxView.prototype.handleInputEvent = function () {
// track whether user has edited directly
if (document.activeElement === this.input) this.directlyEdited = true;
this.value = this.input.checked;
this.test();
if (this.parent) this.parent.update(this);
};
// set the error message if exists
// hides the message container entirely otherwise
CheckboxView.prototype.setMessage = function (message) {
var input = this.input;
this.message = message;
// there is an error
if (message && this.shouldValidate) {
this.messageContainer.style.display = 'block';
this.messageEl.textContent = message;
dom.addClass(input, this.invalidClass);
dom.removeClass(input, this.validClass);
} else {
this.messageContainer.style.display = 'none';
if (this.shouldValidate && this.editedDirectly) {
dom.addClass(input, this.validClass);
dom.removeClass(input, this.invalidClass);
this.setMessage(this.message);
this.input.checked = !!this.value;
this.input.name = this.name;
this.labelEl.textContent = this.label;
},
// handle input events and show appropriate errors
handleInputEvent: function () {
// track whether user has edited directly
if (document.activeElement === this.input) this.directlyEdited = true;
this.value = this.input ? this.input.checked : this.value;
this.test();
if (this.parent) this.parent.update(this);
},
// set the error message if exists
// hides the message container entirely otherwise
setMessage: function (message) {
var input = this.input;
if (!input) return;
this.message = message;
// there is an error
if (message && this.shouldValidate) {
this.messageContainer.style.display = 'block';
this.messageEl.textContent = message;
dom.addClass(input, this.invalidClass);
dom.removeClass(input, this.validClass);
} else {
this.messageContainer.style.display = 'none';
if (this.shouldValidate && this.editedDirectly) {
dom.addClass(input, this.validClass);
dom.removeClass(input, this.invalidClass);
}
}
},
setValue: function (value) {
if (this.input) this.input.checked = !!value;
return this.handleInputEvent();
},
beforeSubmit: function () {
this.shouldValidate = true;
return this.handleInputEvent();
},
test: function () {
var valid = !this.required || !!this.value;
if (valid) this.shouldValidate = true;
this.valid = valid;
if (this.shouldValidate && !valid) {
this.setMessage(this.requiredMessage);
} else {
this.setMessage('');
}
return valid;
}
};
CheckboxView.prototype.setValue = function (value) {
this.input.checked = !!value;
this.handleInputEvent();
};
CheckboxView.prototype.beforeSubmit = function () {
this.shouldValidate = true;
this.handleInputEvent();
};
CheckboxView.prototype.test = function () {
var valid = !this.required || this.value;
if (valid) this.shouldValidate = true;
this.valid = valid;
if (this.shouldValidate && !valid) {
this.setMessage(this.requiredMessage);
} else {
this.setMessage('');
}
return valid;
};
module.exports = CheckboxView;
});
{
"name": "ampersand-checkbox-view",
"description": "A view module for intelligently rendering and validating checkbox input. Works well with ampersand-form-view.",
"version": "2.0.2",
"version": "3.0.0",
"author": "Henrik Joreteg <henrik@andyet.net>",

@@ -15,9 +15,10 @@ "browserify": {

"dependencies": {
"ampersand-dom": "^1.2.1",
"ampersand-version": "^1.0.0",
"domify": "~1.2.2"
"ampersand-dom": "^1.4.0",
"ampersand-version": "^1.0.2",
"ampersand-view": "^7.3.0",
"domify": "~1.3.3"
},
"devDependencies": {
"ampersand-view-conventions": "^1.1.1",
"browserify": "~4.1.10",
"ampersand-view-conventions": ">=1.1.6",
"browserify": "~9.0.8",
"function-bind": "~0.1.0",

@@ -46,3 +47,7 @@ "jshint": "~2.5.1",

"test": "browserify test/* | tape-run | tap-spec",
"start": "run-browser test/*"
"start": "run-browser test/*",
"preversion": "git checkout master && git pull && npm ls",
"publish-patch": "npm run preversion && npm version patch && git push origin master --tags && npm publish",
"publish-minor": "npm run preversion && npm version minor && git push origin master --tags && npm publish",
"publish-major": "npm run preversion && npm version major && git push origin master --tags && npm publish"
},

@@ -49,0 +54,0 @@ "testling": {

# ampersand-checkbox-view
Lead Maintainer: [Michael Garvin](https://github.com/wraithgar)
# overview
A view module for intelligently rendering and validating checkbox input. Works well with [ampersand-form-view](ampersandjs/ampersand-form-view).
[ampersand-checkbox-view](#ampersand-checkbox-view) extends [ampersand-view](ampersandjs/ampersand-view), so you may further `.extend({...})` it to your desire.
It does the following:
- Automatically shows/hides error message based required.
- Automatically shows/hides error message based on if the field is [`required`](#example).
- Will only show error message checkbox is required and:

@@ -12,4 +18,2 @@ - trying to submit form and it's not checked.

Depdent on two micro-libs, so works well with ampersand apps, backbone apps or anything else, really.
<!-- starthide -->

@@ -44,3 +48,3 @@ Part of the [Ampersand.js toolkit](http://ampersandjs.com) for building clientside applications.

value: true, // or false
// optional, this is the element that will be
// optional, this is the element that will be
// replaced by this view. If you don't

@@ -53,4 +57,2 @@ // give it one, it will create one.

validClass: 'input-valid', // <- that's the default
// type value to use for the input tag's type value
type: 'text',
// class to set on input when input is valid

@@ -62,3 +64,5 @@ invalidClass: 'input-invalid', // <- that's the default

// optional, you can pass in the parent view explicitly
parent: someViewInstance
parent: someViewInstance,
// optional, called before form's submitCallback function called
beforeSubmit: function() { console.log('ampersand-checkbox-view rocks!'); }
});

@@ -71,6 +75,29 @@

## browser support
## api
### properties
Commonly accessed properties listed below. See the `initialize()` function for additional properties available.
- `valid` (boolean)
- `value` (boolean)
- `startingValue` (boolean) - this is coerced from any initail `value` provided during construction
### functions
- `new CheckboxView()` - constructor
- see the [example](#example) for constructor options
- `clear()`
- sugar for `view.setValue(false);`
- `reset()`
- sugar for `view.setValue(this.startingValue);`
- `setValue([boolean])`
- sets the value of the view. the view will **always** maintain a value of type `boolean`.
## browser support
[![testling badge](https://ci.testling.com/AmpersandJS/ampersand-checkbox-view.png)](https://ci.testling.com/AmpersandJS/ampersand-checkbox-view)
## changelog
- 3.0.0
- Extend ampersand-view. Add support `autoRender`, `clear`, `reset`
## credits

@@ -77,0 +104,0 @@

@@ -24,7 +24,7 @@ var test = require('tape');

var view = new CheckboxView({name: 'checker'});
var view = new CheckboxView({name: 'checker', autoRender: true});
t.strictEqual(view.value, false ,'should start out as false');
t.equal(view.value, view.input.checked, 'value should be same as input value');
view = new CheckboxView({name: 'checker', label: 'checker', parent: getParent()});
view = new CheckboxView({name: 'checker', label: 'checker', parent: getParent(), autoRender: true});
t.equal(view.labelEl.textContent, 'checker', 'should set label text');

@@ -35,6 +35,7 @@

t.strictEqual(view.value, view.input.checked, 'should be checked now');
t.equal(counter, 1, 'parent update should have been called once');
t.equal(counter, 2, 'parent update should have been called twice');
view = new CheckboxView({name: 'checked', required: true, parent: getParent()});
view = new CheckboxView({name: 'checked', required: true, parent: getParent(), autoRender: true});
t.strictEqual(view.el.children[2].style.display, 'none', 'should not show error right away');
t.equal(counter, 1, 'parent update should have been called once (setValue called on init)');

@@ -44,3 +45,3 @@ // check it directly

t.equal(counter, 1, 'parent update should have been called once');
t.equal(counter, 2, 'parent update should have been called twice');
t.strictEqual(view.value, true, 'should be checked now');

@@ -51,3 +52,3 @@ t.strictEqual(view.valid, true, 'should be valid now');

t.equal(counter, 2, 'parent update should have been called twice');
t.equal(counter, 3, 'parent update should have been called thrice');
t.strictEqual(view.value, false, 'should not be checked now');

@@ -57,3 +58,10 @@ t.strictEqual(view.valid, false, 'should not be valid now');

view = new CheckboxView({name: 'checked', required: true, parent: getParent(), value: true, autoRender: true});
view.setValue(false);
view.reset();
t.equal(view.value, true, 'resets to original value');
view.clear();
t.equal(view.value, false, 'clears to false/unchecked');
t.end();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc