fada51de by Adam Heath

Creating new module.

1 parent e2a83ad1
{
"directory": "src/lib"
}
.*.swp
.tmp/
bin/coverage/
dist/
node_modules/
src/lib/
.grunt/
_SpecRunner.html
/* global module */
module.exports = function(grunt) {
/* global require */
'use strict';
var config = {};
config.jshint = {
options: {
},
_all: [
'grunt-monty-python.js',
],
browserOptions: {
},
};
var montyPython = require('grunt-monty-python')(grunt);
montyPython.createConfig(config);
};
{
"name": "rivets-switch-binder",
"version": "0.1.0",
"authors": [
"Adam Heath <doogie@brainfood.com>"
],
"main": [
"src/scripts/rivets-switch-binder.js"
],
"private": true,
"ignore": [
"**/.*",
"node_modules",
"src/lib",
"test"
],
"dependencies": {
"jquery": "~1.10.2",
"requirejs": "~2.1.10",
"rivets": "~0.8",
"underscore": "~1.6.0"
},
"devDependencies": {
}
}
{
"name": "rivets-switch-binder",
"version": "0.1.0",
"main": [
"src/scripts/rivets-switch-binder.js"
],
"dependencies": {
"rivets": "~0.8",
"requirejs": "~2.1.10"
},
"devDependencies": {
"grunt": "~0",
"grunt-monty-python": "git+ssh://git@gitlab.brainfood.com:brainfood/grunt-monty-python.git"
},
"engines": {
"node": ">=0.8.0"
}
}
/* global require:true */
var require;
require = (function() {
'use strict';
var require = {
baseUrl: 'scripts',
config: {
'rivets-script-binder': {}
},
shim: {
bootstrap: {
deps: [
'jquery'
]
},
rivets: {
deps: [
'jquery'
]
}
},
paths: {
'backbone-validation': '../lib/backbone-validation/dist/backbone-validation-amd',
backbone: '../lib/backbone/backbone',
underscore: '../lib/underscore/underscore',
rivets: '../lib/rivets/dist/rivets',
bootstrap: '../lib/bootstrap/dist/js/bootstrap',
jquery: '../lib/jquery/dist/jquery',
'rivets-backbone-adapter': '../lib/rivets-backbone-adapter/rivets-backbone',
'backbone-seen': '../lib/backbone-seen/src/scripts/backbone-seen'
}
};
return require;
})();
/* global require */
require(
[],
function() {
'use strict';
}
);
define(function(require, exports, module) {
'use strict';
var $ = require('jquery');
var rivets = require('rivets');
function rivetsBinderCall(binding, binderName, methodName, args) {
var binder = rivets.binders[binderName];
if (binder instanceof Function) {
if (methodName === 'routine') {
binder.apply(binding, args);
}
} else if (binder) {
if (binder[methodName]) {
binder[methodName].apply(binding, args);
}
}
}
function SwitchObject(name) {
var cases = {};
var defaultCase = null;
var lastCase = null;
var switchValue;
function updateCase(foundCase, value) {
rivetsBinderCall(foundCase, 'show', 'routine', [foundCase.el, value]);
}
this.addCase = function(binding) {
/* global console:false */
var value = binding.value !== undefined ? binding.value : binding.observer.value();
console.log('SWITCH[' + name + '].addCase(' + value + ')');
updateCase(cases[value] = binding, false);
applyValue();
};
this.removeCase = function(binding) {
var value = binding.value !== undefined ? binding.value : binding.observer.value();
delete cases[value];
updateCase(binding, true);
applyValue();
};
this.setDefault = function(binding) {
updateCase(defaultCase = binding, false);
applyValue();
};
this.removeDefault = function(binding) {
defaultCase = null;
updateCase(binding, true);
applyValue();
};
this.setValue = function(value) {
/* global console:false */
console.log('SWITCH[' + name + '].setValue(' + value + ')');
switchValue = value;
applyValue();
};
function applyValue() {
var foundCase;
if (switchValue === undefined || switchValue === null || !(foundCase = cases[switchValue]) || foundCase !== lastCase) {
if (lastCase) {
updateCase(lastCase, false);
lastCase = null;
}
if (!foundCase) {
foundCase = defaultCase;
}
if (foundCase) {
updateCase(lastCase = foundCase, true);
}
}
}
this.name = name;
}
function findSwitchObject(el, switchName, doCreate) {
var ptr = el, switchObject;
while (ptr) {
var $ptr = $(ptr);
if ($ptr.attr('rv-switch-' + switchName)) {
switchObject = $ptr.data('rivets-switchObject-' + switchName);
if (!switchObject) {
switchObject = new SwitchObject(switchName);
if (doCreate) {
$ptr.data('rivets-switchObject-' + switchName, switchObject);
}
}
return switchObject;
}
ptr = ptr.parentNode;
}
}
rivets.binders['switch-*'] = {
bind: function(el) {
$(el).data('rivets-switchObject-' + this.args[0], findSwitchObject(el, this.args[0], true));
},
unbind: function(el) {
$(el).data('rivets-switchObject-' + this.args[0], null);
},
routine: function(el, value) {
findSwitchObject(el, this.args[0], true).setValue(value);
}
};
rivets.binders['switch-*-case'] = {
bind: function(el) {
findSwitchObject(el, this.args[0], true).addCase(this);
},
unbind: function(el) {
findSwitchObject(el, this.args[0], false).removeCase(this);
},
routine: function(el, value) {
}
};
rivets.binders['switch-*-default'] = {
bind: function(el) {
findSwitchObject(el, this.args[0], true).setDefault(this);
},
unbind: function(el) {
findSwitchObject(el, this.args[0], false).removeDefault(this);
},
routine: function(el, value) {
}
};
return {};
});