Creating new module.
Showing
8 changed files
with
240 additions
and
0 deletions
.gitignore
0 → 100644
Gruntfile.js
0 → 100644
1 | /* global module */ | ||
2 | |||
3 | module.exports = function(grunt) { | ||
4 | /* global require */ | ||
5 | 'use strict'; | ||
6 | |||
7 | var config = {}; | ||
8 | config.jshint = { | ||
9 | options: { | ||
10 | }, | ||
11 | _all: [ | ||
12 | 'grunt-monty-python.js', | ||
13 | ], | ||
14 | browserOptions: { | ||
15 | }, | ||
16 | }; | ||
17 | |||
18 | var montyPython = require('grunt-monty-python')(grunt); | ||
19 | montyPython.createConfig(config); | ||
20 | }; |
bower.json
0 → 100644
1 | { | ||
2 | "name": "rivets-switch-binder", | ||
3 | "version": "0.1.0", | ||
4 | "authors": [ | ||
5 | "Adam Heath <doogie@brainfood.com>" | ||
6 | ], | ||
7 | "main": [ | ||
8 | "src/scripts/rivets-switch-binder.js" | ||
9 | ], | ||
10 | "private": true, | ||
11 | "ignore": [ | ||
12 | "**/.*", | ||
13 | "node_modules", | ||
14 | "src/lib", | ||
15 | "test" | ||
16 | ], | ||
17 | "dependencies": { | ||
18 | "jquery": "~1.10.2", | ||
19 | "requirejs": "~2.1.10", | ||
20 | "rivets": "~0.8", | ||
21 | "underscore": "~1.6.0" | ||
22 | }, | ||
23 | "devDependencies": { | ||
24 | } | ||
25 | } |
package.json
0 → 100644
1 | { | ||
2 | "name": "rivets-switch-binder", | ||
3 | "version": "0.1.0", | ||
4 | "main": [ | ||
5 | "src/scripts/rivets-switch-binder.js" | ||
6 | ], | ||
7 | "dependencies": { | ||
8 | "rivets": "~0.8", | ||
9 | "requirejs": "~2.1.10" | ||
10 | }, | ||
11 | "devDependencies": { | ||
12 | "grunt": "~0", | ||
13 | "grunt-monty-python": "git+ssh://git@gitlab.brainfood.com:brainfood/grunt-monty-python.git" | ||
14 | }, | ||
15 | "engines": { | ||
16 | "node": ">=0.8.0" | ||
17 | } | ||
18 | } | ||
19 |
src/scripts/config.js
0 → 100644
1 | /* global require:true */ | ||
2 | var require; | ||
3 | require = (function() { | ||
4 | 'use strict'; | ||
5 | |||
6 | var require = { | ||
7 | baseUrl: 'scripts', | ||
8 | config: { | ||
9 | 'rivets-script-binder': {} | ||
10 | }, | ||
11 | shim: { | ||
12 | bootstrap: { | ||
13 | deps: [ | ||
14 | 'jquery' | ||
15 | ] | ||
16 | }, | ||
17 | rivets: { | ||
18 | deps: [ | ||
19 | 'jquery' | ||
20 | ] | ||
21 | } | ||
22 | }, | ||
23 | paths: { | ||
24 | 'backbone-validation': '../lib/backbone-validation/dist/backbone-validation-amd', | ||
25 | backbone: '../lib/backbone/backbone', | ||
26 | underscore: '../lib/underscore/underscore', | ||
27 | rivets: '../lib/rivets/dist/rivets', | ||
28 | bootstrap: '../lib/bootstrap/dist/js/bootstrap', | ||
29 | jquery: '../lib/jquery/dist/jquery', | ||
30 | 'rivets-backbone-adapter': '../lib/rivets-backbone-adapter/rivets-backbone', | ||
31 | 'backbone-seen': '../lib/backbone-seen/src/scripts/backbone-seen' | ||
32 | } | ||
33 | }; | ||
34 | |||
35 | return require; | ||
36 | })(); |
src/scripts/main.js
0 → 100644
src/scripts/rivets-switch-binder.js
0 → 100644
1 | define(function(require, exports, module) { | ||
2 | 'use strict'; | ||
3 | var $ = require('jquery'); | ||
4 | var rivets = require('rivets'); | ||
5 | |||
6 | function rivetsBinderCall(binding, binderName, methodName, args) { | ||
7 | var binder = rivets.binders[binderName]; | ||
8 | if (binder instanceof Function) { | ||
9 | if (methodName === 'routine') { | ||
10 | binder.apply(binding, args); | ||
11 | } | ||
12 | } else if (binder) { | ||
13 | if (binder[methodName]) { | ||
14 | binder[methodName].apply(binding, args); | ||
15 | } | ||
16 | } | ||
17 | } | ||
18 | function SwitchObject(name) { | ||
19 | var cases = {}; | ||
20 | var defaultCase = null; | ||
21 | var lastCase = null; | ||
22 | var switchValue; | ||
23 | function updateCase(foundCase, value) { | ||
24 | rivetsBinderCall(foundCase, 'show', 'routine', [foundCase.el, value]); | ||
25 | } | ||
26 | this.addCase = function(binding) { | ||
27 | /* global console:false */ | ||
28 | var value = binding.value !== undefined ? binding.value : binding.observer.value(); | ||
29 | console.log('SWITCH[' + name + '].addCase(' + value + ')'); | ||
30 | updateCase(cases[value] = binding, false); | ||
31 | applyValue(); | ||
32 | }; | ||
33 | this.removeCase = function(binding) { | ||
34 | var value = binding.value !== undefined ? binding.value : binding.observer.value(); | ||
35 | delete cases[value]; | ||
36 | updateCase(binding, true); | ||
37 | applyValue(); | ||
38 | }; | ||
39 | this.setDefault = function(binding) { | ||
40 | updateCase(defaultCase = binding, false); | ||
41 | applyValue(); | ||
42 | }; | ||
43 | this.removeDefault = function(binding) { | ||
44 | defaultCase = null; | ||
45 | updateCase(binding, true); | ||
46 | applyValue(); | ||
47 | }; | ||
48 | this.setValue = function(value) { | ||
49 | /* global console:false */ | ||
50 | console.log('SWITCH[' + name + '].setValue(' + value + ')'); | ||
51 | switchValue = value; | ||
52 | applyValue(); | ||
53 | }; | ||
54 | function applyValue() { | ||
55 | var foundCase; | ||
56 | if (switchValue === undefined || switchValue === null || !(foundCase = cases[switchValue]) || foundCase !== lastCase) { | ||
57 | if (lastCase) { | ||
58 | updateCase(lastCase, false); | ||
59 | lastCase = null; | ||
60 | } | ||
61 | if (!foundCase) { | ||
62 | foundCase = defaultCase; | ||
63 | } | ||
64 | if (foundCase) { | ||
65 | updateCase(lastCase = foundCase, true); | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | this.name = name; | ||
70 | } | ||
71 | |||
72 | function findSwitchObject(el, switchName, doCreate) { | ||
73 | var ptr = el, switchObject; | ||
74 | while (ptr) { | ||
75 | var $ptr = $(ptr); | ||
76 | if ($ptr.attr('rv-switch-' + switchName)) { | ||
77 | switchObject = $ptr.data('rivets-switchObject-' + switchName); | ||
78 | if (!switchObject) { | ||
79 | switchObject = new SwitchObject(switchName); | ||
80 | if (doCreate) { | ||
81 | $ptr.data('rivets-switchObject-' + switchName, switchObject); | ||
82 | } | ||
83 | } | ||
84 | return switchObject; | ||
85 | } | ||
86 | ptr = ptr.parentNode; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | rivets.binders['switch-*'] = { | ||
91 | bind: function(el) { | ||
92 | $(el).data('rivets-switchObject-' + this.args[0], findSwitchObject(el, this.args[0], true)); | ||
93 | }, | ||
94 | unbind: function(el) { | ||
95 | $(el).data('rivets-switchObject-' + this.args[0], null); | ||
96 | }, | ||
97 | routine: function(el, value) { | ||
98 | findSwitchObject(el, this.args[0], true).setValue(value); | ||
99 | } | ||
100 | }; | ||
101 | rivets.binders['switch-*-case'] = { | ||
102 | bind: function(el) { | ||
103 | findSwitchObject(el, this.args[0], true).addCase(this); | ||
104 | }, | ||
105 | unbind: function(el) { | ||
106 | findSwitchObject(el, this.args[0], false).removeCase(this); | ||
107 | }, | ||
108 | routine: function(el, value) { | ||
109 | } | ||
110 | }; | ||
111 | rivets.binders['switch-*-default'] = { | ||
112 | bind: function(el) { | ||
113 | findSwitchObject(el, this.args[0], true).setDefault(this); | ||
114 | }, | ||
115 | unbind: function(el) { | ||
116 | findSwitchObject(el, this.args[0], false).removeDefault(this); | ||
117 | }, | ||
118 | routine: function(el, value) { | ||
119 | } | ||
120 | }; | ||
121 | return {}; | ||
122 | }); |
-
Please register or sign in to post a comment