// // Service for keeping track of which memberships are selected // angular.module('artfully').factory('membershipSelections', function() { // Map of selected membership ids var map = {}; // Initial values $('input[name=membership_ids\\[\\]]:checked').each(function(i,input) { map[input.value] = true }); // Watch for changes $('body').on('change', 'input[name=membership_ids\\[\\]]', function(change) { var e = $(change.target); var row = e.closest('tr'); var type = $(row.find('.item-description')[0]); var price = $(row.find('.price')[0]); var membership_id = e.val(); if ($(e).prop( "checked" )) { // Add the membership id if it's not already in the list if (map[membership_id] === undefined) { map[membership_id] = { id: membership_id, type: type.text(), price: price.text() }; } } else { // Remove the membership id delete map[membership_id]; } }); return map; }); // // Controller // angular.module('artfully').controller('ChangeMembershipController', ['$scope', 'membershipSelections', function($scope, membershipSelections) { // Template variables $scope.payment_method = 'cash'; $scope.price = 0; $scope.total = 0; $scope.selected = {}; // Helpers $scope.anySelected = function() { if (Object.keys(membershipSelections).length == 0 ) { return false; } return true; } $scope.changeSelected = function(click) { click.preventDefault(); if (!$scope.anySelected()) { alert("Select one or more memberships to change."); return false; } // Open the modal $('#change').modal('show'); return false; } $scope.updateTotal = function() { var price = parseFloat($scope.price.substr(1).replace(/,/, "")); var total = (Object.keys($scope.selected).length * price); $scope.total = '$'+total.toFixed(2); } // Update the list of selected ids when the modal is shown $("#change").on('shown', function(shown) { $scope.selected = membershipSelections; // Mask the price field touchCurrency(); }); // Clear the list of selected ids when the modal is hidden $("#change").on('hidden', function(hidden) { $scope.payment_method = 'cash'; $scope.price = 0; $scope.total = 0; $scope.selected = {}; // Reset the form var form = $(this).find('form')[0]; if (form) { form.reset(); } }); }]);