/*
 Copyright 2015 Province of British Columbia

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and limitations under the License.
 */

'use strict';

angular.module('bcdevxApp.resources', ['ngRoute', 'ngSanitize', 'ui.highlight'])

    .config(['$routeProvider', function($routeProvider) {

    }])

    .factory('ResourceList', ['$resource', function($resource) {
        return $resource('/resources');
    }])

    .factory('SourceList', ['$resource', function($resource) {
        return $resource('/resources-sources');
    }])


    .controller('ResourcesCtrl', ['$rootScope', '$scope', '$location', '$window',
                'usSpinnerService', 'ResourceList', 'SourceList',

        function($rootScope, $scope, $location, $window, usSpinnerService, ResourceList, SourceList) {

        $scope.selectedSource = '';
        $scope.selectedSourceTitle = '';
        $scope.predicateTitle = '';

        $scope.startSpin = function(){
            console.log("Start the spinner.");
            usSpinnerService.spin("spinner-1");
        }
        $scope.stopSpin = function(){
            console.log("Stop the spinner.");
            setTimeout(function(){
                usSpinnerService.stop("spinner-1");
            }, 100);
        }

        //$scope.stopSpin();

        ResourceList.get({}, function(data) {
            $scope.resources = data.resources;
            $scope.stopSpin();
        });

        SourceList.get({}, function(data) {
            $scope.sources = data.sources;
            $scope.stopSpin();
        });

        $scope.hasMatchingSource = function(actual, expected) {
            if(expected.short_name == "") return true; // Filtering by null should show all results
            if(!actual.short_name || !expected.short_name) return false;
            return actual.short_name == expected.short_name;
        }

        $scope.selectSource = function(event, newSource, newSourceTitle) {
            event.preventDefault();
            $scope.selectedSource = newSource;
            $scope.selectedSourceTitle = newSourceTitle;
        }

        $scope.startSpin();

    }]);
@@ -1121,16 +1121,22 @@
 ceList',
+ '$q',
 %0A%0A      
@@ -1228,16 +1228,20 @@
 urceList
+, $q
 ) %7B%0A%0A   
@@ -1683,17 +1683,16 @@
    %7D, 10
-0
 );%0A     
@@ -1709,26 +1709,657 @@
     
-//$scope.stopSpin(
+$scope.startSpin();%0A%0A        var resourceListDeferred = $q.defer();%0A        var resourcePromise = resourceListDeferred.promise;%0A%0A        var sourceListDeferred = $q.defer();%0A        var sourcePromise = sourceListDeferred.promise;%0A%0A        resourcePromise.then(%0A            function(value)%7B%0A                console.log(%22resolution value %22 + value);%0A            %7D%0A        );%0A        sourcePromise.then(%0A            function(value)%7B%0A                console.log(%22resolution value %22 + value);%0A            %7D%0A        );%0A%0A        $q.all(%5BresourcePromise,sourcePromise%5D).then(%0A            function()%7B%0A                $scope.stopSpin();%0A            %7D%0A        
 );%0A%0A
@@ -2455,32 +2455,102 @@
 es;%0A            
+resourceListDeferred.resolve(%22resource list resolved%22);%0A            //
 $scope.stopSpin(
@@ -2656,32 +2656,98 @@
 es;%0A            
+sourceListDeferred.resolve(%22source list resolved%22);%0A            //
 $scope.stopSpin(
@@ -3301,36 +3301,8 @@
  %7D%0A%0A
-        $scope.startSpin();%0A
 %0A   

Use Angular and promise to controll spinner