From 33cf05ab8381534046665dfbd4831de134f73eed Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 11 Apr 2014 12:37:32 +1000 Subject: [PATCH] Manually bringing over the login links for checkout --- .../javascripts/darkswarm/all.js.coffee | 2 + .../controllers/checkout_controller.js.coffee | 8 +- .../controllers/menu_controller.js.coffee | 13 -- .../javascripts/darkswarm/darkswarm.js.coffee | 3 +- .../shared/angular-local-storage.js | 171 ++++++++++++++++++ .../shop/checkout/_authentication.html.haml | 4 +- app/views/shop/checkout/_form.html.haml | 2 +- app/views/shop/shop/show.html.haml | 2 +- 8 files changed, 186 insertions(+), 19 deletions(-) delete mode 100644 app/assets/javascripts/darkswarm/controllers/menu_controller.js.coffee create mode 100644 app/assets/javascripts/shared/angular-local-storage.js diff --git a/app/assets/javascripts/darkswarm/all.js.coffee b/app/assets/javascripts/darkswarm/all.js.coffee index ea8c47eed8..7f09206ea3 100644 --- a/app/assets/javascripts/darkswarm/all.js.coffee +++ b/app/assets/javascripts/darkswarm/all.js.coffee @@ -4,8 +4,10 @@ #= require spin # #= require angular +#= require angular-cookies #= require angular-resource #= require ../shared/mm-foundation-tpls-0.2.0-SNAPSHOT +#= require ../shared/angular-local-storage.js # #= require ../shared/jquery.timeago #= require foundation diff --git a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee index 1d0604d76c..1bb7692239 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee @@ -1,6 +1,7 @@ -Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, order) -> +Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, order, $location, $anchorScroll) -> $scope.require_ship_address = false $scope.order = order + $scope.userOpen = true $scope.initialize = -> # Our shipping_methods comes through as a hash like so: {id: requires_shipping_address} @@ -25,5 +26,10 @@ Darkswarm.controller "CheckoutCtrl", ($scope, $rootScope, order) -> event.preventDefault() checkout.submit() + $scope.scrollTo = (name)-> + $location.hash(name); + $anchorScroll(); + $scope.userOpen = false + $scope.initialize() diff --git a/app/assets/javascripts/darkswarm/controllers/menu_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/menu_controller.js.coffee deleted file mode 100644 index 889c8fa8f4..0000000000 --- a/app/assets/javascripts/darkswarm/controllers/menu_controller.js.coffee +++ /dev/null @@ -1,13 +0,0 @@ -window.MenuCtrl = Darkswarm.controller "MenuCtrl", ($scope, Navigation) -> - - $scope.toggleLogin = -> - Navigation.navigate "/login" - - $scope.toggleSignup = -> - Navigation.navigate "/signup" - - $scope.toggleSignup = -> - Navigation.navigate "/signup" - - $scope.toggle = (path = null)-> - Navigation.navigate(path) diff --git a/app/assets/javascripts/darkswarm/darkswarm.js.coffee b/app/assets/javascripts/darkswarm/darkswarm.js.coffee index 6be3a4a155..d2ce7b6758 100644 --- a/app/assets/javascripts/darkswarm/darkswarm.js.coffee +++ b/app/assets/javascripts/darkswarm/darkswarm.js.coffee @@ -1,5 +1,6 @@ -window.Darkswarm = angular.module("Darkswarm", ["ngResource", "filters", 'mm.foundation']).config ($httpProvider, $tooltipProvider) -> +window.Darkswarm = angular.module("Darkswarm", ["ngResource", "filters", 'mm.foundation', 'angularLocalStorage']).config ($httpProvider, $tooltipProvider) -> $httpProvider.defaults.headers.post['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content') + $httpProvider.defaults.headers.put['X-CSRF-Token'] = $('meta[name="csrf-token"]').attr('content') $httpProvider.defaults.headers['common']['X-Requested-With'] = 'XMLHttpRequest' $httpProvider.defaults.headers.common.Accept = "application/json, text/javascript, */*" diff --git a/app/assets/javascripts/shared/angular-local-storage.js b/app/assets/javascripts/shared/angular-local-storage.js new file mode 100644 index 0000000000..2ac18eba9e --- /dev/null +++ b/app/assets/javascripts/shared/angular-local-storage.js @@ -0,0 +1,171 @@ +/* + * Angular.js localStorage module + * https://github.com/agrublev/angularLocalStorage + */ + +(function (window, angular, undefined) { + 'use strict'; + + angular.module('angularLocalStorage', ['ngCookies']).factory('storage', ['$parse', '$cookieStore', '$window', '$log', function ($parse, $cookieStore, $window, $log) { + /** + * Global Vars + */ + var storage = (typeof $window.localStorage === 'undefined') ? undefined : $window.localStorage; + var supported = typeof storage !== 'undefined'; + + var privateMethods = { + /** + * Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object) + * @param res - a string that will be parsed for type + * @returns {*} - whatever the real type of stored value was + */ + parseValue: function (res) { + var val; + try { + val = angular.fromJson(res); + if (typeof val === 'undefined') { + val = res; + } + if (val === 'true') { + val = true; + } + if (val === 'false') { + val = false; + } + if ($window.parseFloat(val) === val && !angular.isObject(val)) { + val = $window.parseFloat(val); + } + } catch (e) { + val = res; + } + return val; + } + }; + + var publicMethods = { + /** + * Set - let's you set a new localStorage key pair set + * @param key - a string that will be used as the accessor for the pair + * @param value - the value of the localStorage item + * @returns {*} - will return whatever it is you've stored in the local storage + */ + set: function (key, value) { + if (!supported) { + try { + $cookieStore.put(key, value); + return value; + } catch(e) { + $log.log('Local Storage not supported, make sure you have angular-cookies enabled.'); + } + } + var saver = angular.toJson(value); + storage.setItem(key, saver); + return privateMethods.parseValue(saver); + }, + + /** + * Get - let's you get the value of any pair you've stored + * @param key - the string that you set as accessor for the pair + * @returns {*} - Object,String,Float,Boolean depending on what you stored + */ + get: function (key) { + if (!supported) { + try { + return privateMethods.parseValue($.cookie(key)); + } catch (e) { + return null; + } + } + var item = storage.getItem(key); + return privateMethods.parseValue(item); + }, + + /** + * Remove - let's you nuke a value from localStorage + * @param key - the accessor value + * @returns {boolean} - if everything went as planned + */ + remove: function (key) { + if (!supported) { + try { + $cookieStore.remove(key); + return true; + } catch (e) { + return false; + } + } + storage.removeItem(key); + return true; + }, + + /** + * Bind - let's you directly bind a localStorage value to a $scope variable + * @param {Angular $scope} $scope - the current scope you want the variable available in + * @param {String} key - the name of the variable you are binding + * @param {Object} opts - (optional) custom options like default value or unique store name + * Here are the available options you can set: + * * defaultValue: the default value + * * storeName: add a custom store key value instead of using the scope variable name + * @returns {*} - returns whatever the stored value is + */ + bind: function ($scope, key, opts) { + var defaultOpts = { + defaultValue: '', + storeName: '' + }; + // Backwards compatibility with old defaultValue string + if (angular.isString(opts)) { + opts = angular.extend({},defaultOpts,{defaultValue:opts}); + } else { + // If no defined options we use defaults otherwise extend defaults + opts = (angular.isUndefined(opts)) ? defaultOpts : angular.extend(defaultOpts,opts); + } + + // Set the storeName key for the localStorage entry + // use user defined in specified + var storeName = opts.storeName || key; + + // If a value doesn't already exist store it as is + if (!publicMethods.get(storeName)) { + publicMethods.set(storeName, $parse(key)($scope) || opts.defaultValue); + } else { + // If it does exist assign it to the $scope value + $parse(key).assign($scope, publicMethods.get(storeName)); + } + + + // Register a listener for changes on the $scope value + // to update the localStorage value + $scope.$watch(key, function (val) { + if (angular.isDefined(val)) { + publicMethods.set(storeName, val); + } + }, true); + + return publicMethods.get(storeName); + }, + /** + * Unbind - let's you unbind a variable from localStorage while removing the value from both + * the localStorage and the local variable and sets it to null + * @param $scope - the scope the variable was initially set in + * @param key - the name of the variable you are unbinding + * @param storeName - (optional) if you used a custom storeName you will have to specify it here as well + */ + unbind: function($scope,key,storeName) { + storeName = storeName || key; + $parse(key).assign($scope, null); + $scope.$watch(key, function () { }); + publicMethods.remove(storeName); + }, + /** + * Clear All - let's you clear out ALL localStorage variables, use this carefully! + */ + clearAll: function() { + storage.clear(); + } + }; + + return publicMethods; + }]); + +})(window, window.angular); diff --git a/app/views/shop/checkout/_authentication.html.haml b/app/views/shop/checkout/_authentication.html.haml index 891958de25..62aba59509 100644 --- a/app/views/shop/checkout/_authentication.html.haml +++ b/app/views/shop/checkout/_authentication.html.haml @@ -1,9 +1,9 @@ %fieldset - %accordion-group{heading: "User", "is-open" => "accordion.user"} + %accordion-group{heading: "User", "is-open" => "userOpen"} .row .large-4.columns.text-center{"ng-controller" => "AuthenticationActionsCtrl"} %button{"ng-click" => "toggle('/login')"} Login .large-4.columns.text-center{"ng-controller" => "AuthenticationActionsCtrl"} %button{"ng-click" => "toggle('/signup')"} Signup .large-4.columns.text-center - %button{"ng-click" => "show('details')"} Checkout as guest + %button{"ng-click" => "scrollTo('details')"} Checkout as guest diff --git a/app/views/shop/checkout/_form.html.haml b/app/views/shop/checkout/_form.html.haml index 8ac9f67c90..cd3b277dbb 100644 --- a/app/views/shop/checkout/_form.html.haml +++ b/app/views/shop/checkout/_form.html.haml @@ -9,7 +9,7 @@ -#{{ order | json }} .large-12.columns - %fieldset#details + %fieldset#details{name: "details"} %legend Customer Details .row .large-6.columns diff --git a/app/views/shop/shop/show.html.haml b/app/views/shop/shop/show.html.haml index f8380c6c39..08f8ff448e 100644 --- a/app/views/shop/shop/show.html.haml +++ b/app/views/shop/shop/show.html.haml @@ -5,7 +5,7 @@ %select.avenir#order_cycle_id{"ng-model" => "order_cycle.order_cycle_id", "ng-change" => "changeOrderCycle()", "ng-options" => "oc.id as oc.time for oc in #{@order_cycles.map {|oc| {time: pickup_time(oc), id: oc.id}}.to_json}", - "popover-placement" => "bottom", "popover" => "testy", "popover-trigger" => "openTrigger"} + "popover-placement" => "bottom", "popover" => "Select an order cycle", "popover-trigger" => "openTrigger"} %closing{"ng-if" => "OrderCycle.selected()"} Orders close