Browse files

include map states to class

  • Loading branch information...
1 parent 7b3e1f2 commit 478f99279f4eda1ab5c299aa7ac847da9312746b @tdreyno committed Oct 13, 2010
View
59 map_states_to_class/map_states_to_class.js
@@ -0,0 +1,59 @@
+steal.plugins("ss/controller/state_machine")
+ .then(function($) {
+
+ $.Controller.extend('SS.Controller.MapStatesToClass',
+ {
+ onDocument: true,
+ mappings: {}
+ },
+ {
+ "didMoveToState subscribe": function(event_name, params) {
+ var controller = params.controller,
+ mapping = SS.Controller.MapStatesToClass.mappings[controller.Class.fullName];
+
+ if (!mapping) {
+ return;
+ }
+
+ var newClass = mapping.inverseMapping[params.to] || mapping.defaultClass;
+
+ if (typeof controller.element !== "undefined") {
+ controller.element.addClass(newClass).removeClass(controller.currentStateClass);
+ }
+
+ var callbackFunc = mapping.callback;
+ if ((typeof callbackFunc === "string") &&
+ ($.isFunction(controller[callbackFunc]))) {
+ callbackFunc = controller[callbackFunc];
+ }
+
+ if ($.isFunction(callbackFunc)) {
+ callbackFunc.apply(controller, [newClass]);
+ }
+
+ controller.currentStateClass = newClass;
+ }
+ }
+ );
+
+ // Add to list of mapped states
+ SS.Controller.StateMachine.mapStatesToClass = function(defaultClass, stateMapping, callback) {
+ this.prototype.currentStateClass = defaultClass;
+
+ var inverseMapping = {};
+ for (var klassName in stateMapping) {
+ var states = stateMapping[klassName];
+ for (var i = 0; i < states.length; i++) {
+ var state = states[i];
+ inverseMapping[state] = klassName;
+ }
+ }
+
+ SS.Controller.MapStatesToClass.mappings[this.fullName] = {
+ defaultClass: defaultClass,
+ stateMapping: stateMapping,
+ inverseMapping: inverseMapping,
+ callback: callback || $.noop
+ };
+ };
+});
View
24 map_states_to_class/qunit.html
@@ -0,0 +1,24 @@
+<html>
+ <head>
+ <link rel="stylesheet" type="text/css" href="../../../../funcunit/qunit/qunit.css" />
+ <title>Map States to Class Test Suite</title>
+ <style>
+ body {
+ margin: 0px; padding: 0px;
+ }
+ </style>
+ <script type='text/javascript'>
+ steal = {ignoreControllers: true}
+ </script>
+ <script type='text/javascript' src='../../../../steal/steal.js?steal[app]=ss/controller/state_machine/map_states_to_class/test/qunit'></script>
+ </head>
+ <body>
+ <h1 id="qunit-header">Map States to Class Test Suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <div id="test-content"></div>
+ <ol id="qunit-tests"></ol>
+ <div id="qunit-test-area"></div>
+ </body>
+</html>
View
67 map_states_to_class/test/qunit/map_states_to_class_test.js
@@ -0,0 +1,67 @@
+module("Map States to Class Controller");
+
+SS.Controller.StateMachine.extend("OpaqueStatesTestController", {
+ states: {
+ initial: {},
+ stateTwo: {},
+ stateThree: {},
+ stateFour: {}
+ },
+
+ init: function(){
+ this.mapStatesToClass("customTransparent", {
+ customOpaque: ["stateTwo", "stateFour"]
+ }, "changeOpacity");
+ }
+},
+{
+});
+
+test("Initial should be default class", function(){
+ var testController = new OpaqueStatesTestController($("<div />").get(0));
+ equals(testController.currentStateClass, "customTransparent");
+});
+
+asyncTest("stateTwo & stateFour should be customOpaque", function(){
+ var testController = new OpaqueStatesTestController($("<div />").get(0));
+
+ testController.changeOpacity = start;
+ testController.moveToState("stateTwo");
+ stop();
+ equals(testController.currentStateClass, "customOpaque");
+
+ testController.moveToState("initial");
+ stop();
+ equals(testController.currentStateClass, "customTransparent");
+
+ testController.moveToState("stateFour");
+ stop();
+ equals(testController.currentStateClass, "customOpaque");
+});
+
+asyncTest("stateThree should be customTransparent", function(){
+ var testController = new OpaqueStatesTestController($("<div />").get(0));
+ testController.moveToState("stateTwo");
+ testController.changeOpacity = start;
+ testController.moveToState("stateThree");
+ stop();
+ equals(testController.currentStateClass, "customTransparent");
+});
+
+asyncTest("Custom changeOpacity should fire", function(){
+ var testController = new OpaqueStatesTestController($("<div />").get(0));
+
+ testController.changeOpacity = function(newClass) {
+ start();
+ equals(newClass, "customOpaque");
+ };
+ stop();
+ testController.moveToState("stateTwo");
+
+ testController.changeOpacity = function(newClass) {
+ start();
+ equals(newClass, "customTransparent");
+ };
+ stop();
+ testController.moveToState("stateThree");
+});
View
4 map_states_to_class/test/qunit/qunit.js
@@ -0,0 +1,4 @@
+steal
+ .plugins("ss/controller/state_machine") //load your app
+ .plugins("funcunit/qunit") //load qunit
+ .then("map_states_to_class_test")
View
2 state_machine.js
@@ -216,4 +216,4 @@ steal.plugins('jquery/controller/subscribe',
}
}
);
-})
+}).then("map_states_to_class/map_states_to_class")

0 comments on commit 478f992

Please sign in to comment.