|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>School Finder</title> |
| 5 | +</head> |
| 6 | +<body ng-app="testApp" ng-strict-di=""> |
| 7 | + |
| 8 | + <div ng-controller="LocationController"> |
| 9 | + <span async-button-disable |
| 10 | + default-text="Get Location" |
| 11 | + working-text="Getting Location" |
| 12 | + watch-value="updatingLocation" |
| 13 | + call-function="updateLocation()"></span> |
| 14 | + <p ng-cloak ng-hide="location === undefined"> |
| 15 | + My location is {{ location.latitude }} {{ location.longitude }} |
| 16 | + </p> |
| 17 | + <p ng-cloak ng-hide="locationError === undefined"> |
| 18 | + {{ locationError }} |
| 19 | + </p> |
| 20 | + </div> |
| 21 | + |
| 22 | + <script src="../../node_modules/angular/angular.js"></script> |
| 23 | + <script src="../../app/services/currentLocationService.js"></script> |
| 24 | + <script> |
| 25 | + // create module |
| 26 | + var sfApp = angular.module('testApp', []); |
| 27 | + |
| 28 | + // register services |
| 29 | + sfApp.service("currentLocationService", SCHOOL_FINDER_CLS.CurrentLocationService); |
| 30 | + |
| 31 | + sfApp.controller("LocationController", [ |
| 32 | + "$scope", |
| 33 | + "currentLocationService", |
| 34 | + function($scope, cls) { |
| 35 | + $scope.updatingLocation = false; |
| 36 | + $scope.location = undefined; |
| 37 | + $scope.locationError = undefined; |
| 38 | + $scope.updateLocation = function() { |
| 39 | + if ($scope.updatingLocation) { |
| 40 | + return; |
| 41 | + } |
| 42 | + $scope.updatingLocation = true; |
| 43 | + $scope.location = undefined; |
| 44 | + $scope.locationError = undefined; |
| 45 | + cls.getCurrentLocation(navigator.geolocation, {timeout: 5000}, function(err, coordinates){ |
| 46 | + $scope.updatingLocation = false; |
| 47 | + if (err) { |
| 48 | + console.log(err); |
| 49 | + $scope.location = undefined; |
| 50 | + $scope.locationError = err; |
| 51 | + } else { |
| 52 | + console.log("Got coordinates"); |
| 53 | + $scope.location = coordinates; |
| 54 | + } |
| 55 | + $scope.$apply(); // trigger angular digest |
| 56 | + }); |
| 57 | + } |
| 58 | + }]); |
| 59 | + |
| 60 | + sfApp.directive("asyncButtonDisable", function(){ |
| 61 | + return { |
| 62 | + link: function(scope, el, attrs) { |
| 63 | + var button = document.createElement("button"); |
| 64 | + button.innerHTML = attrs.defaultText; |
| 65 | + button.disabled = false; |
| 66 | + button.onclick = function(){ |
| 67 | + scope.$apply(attrs.callFunction); |
| 68 | + }; |
| 69 | + el[0].appendChild(button); |
| 70 | + scope.$watch(attrs.watchValue, function(newValue, oldValue){ |
| 71 | + if (newValue) { |
| 72 | + button.innerHTML = attrs.workingText; |
| 73 | + button.disabled = true; |
| 74 | + } else { |
| 75 | + button.innerHTML = attrs.defaultText; |
| 76 | + button.disabled = false; |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + }; |
| 81 | + }); |
| 82 | + |
| 83 | + </script> |
| 84 | +</body> |
| 85 | +</html> |
0 commit comments