garyconstable
1/29/2015 - 9:28 AM

Angular JS App Skeleton

Angular JS App Skeleton

'use strict';

/* ----------------------------------------------------------------
App Definition
-----------------------------------------------------------------*/

var webApp = angular.module('webApp', [
  'ngRoute',
  'webAppAnimations',
  'webAppControllers',
  'webAppFilters',
  'webAppServices',
  'webAppDirectives',
]);

/* ----------------------------------------------------------------
App Config
-----------------------------------------------------------------*/

webApp.config(['$locationProvider', '$routeProvider', '$disqusProvider',
    function($locationProvider, $routeProvider, $disqusProvider){
        
        //http://stackoverflow.com/questions/16677528/location-switching-between-html5-and-hashbang-mode-link-rewriting
        //$locationProvider.html5Mode({enabled: true, requireBase: false}).hashPrefix('!');
        $locationProvider.html5Mode(false).hashPrefix('!');
        
        $disqusProvider.setShortname('mtbfeeds'); // Configure the disqus shortname

        $routeProvider.
        when('/', {
            templateUrl: '/',
            controller: 'webAppListCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
  }]);
  
webApp.run([
'$rootScope', function ($rootScope) {
    $rootScope.somevar = '1461363630752862'; // set your facebook app id here
    }
]);

/* ----------------------------------------------------------------
App Annimations
-----------------------------------------------------------------*/

var webAppAnimations = angular.module('webAppAnimations', ['ngAnimate']);

webAppAnimations.animation('.phone', function() {

  var animateUp = function(element, className, done) {
    if(className != 'active') {
      return;
    }
    element.css({
      position: 'absolute',
      top: 500,
      left: 0,
      display: 'block'
    });

    jQuery(element).animate({
      top: 0
    }, done);

    return function(cancel) {
      if(cancel) {
        element.stop();
      }
    };
  }

  var animateDown = function(element, className, done) {
    if(className != 'active') {
      return;
    }
    element.css({
      position: 'absolute',
      left: 0,
      top: 0
    });

    jQuery(element).animate({
      top: -500
    }, done);

    return function(cancel) {
      if(cancel) {
        element.stop();
      }
    };
  }

  return {
    addClass: animateUp,
    removeClass: animateDown
  };
});

/* ----------------------------------------------------------------
App Controllers
-----------------------------------------------------------------*/

var webAppControllers = angular.module('webAppControllers', []);

webAppControllers.controller('webAppListCtrl', ['$scope', 'Feeds',
    function($scope, Feeds, sharedProperties, $routeParams, $location) {  
}]);

/* ----------------------------------------------------------------
App Filters
-----------------------------------------------------------------*/
 
angular.module('webAppFilters', [])

.filter('unsafe', function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    };
})

/* ----------------------------------------------------------------
App Service
-----------------------------------------------------------------*/

var webAppServices = angular.module('webAppServices', ['ngResource']);

webAppServices.factory('Feeds', ['$resource',
    function($resource){
        return $resource('/some/url/', {}, {
            query: {method:'GET', params:{}, isArray:true}
        });
}]);

/* ----------------------------------------------------------------
App Directives
-----------------------------------------------------------------*/

var webAppDirectives = angular.module('webAppDirectives', []);

webAppDirectives.directive("scroll",  ['$window', 'sharedProperties',
    function($window, sharedProperties) {
        
        return function(scope, element, attrs) {
        angular.element($window).bind("scroll", function() {
            
            if( sharedProperties.get('canScroll') === true){
                var pos = this.pageYOffset;
                sharedProperties.set('windowScroll', pos);
                scope.$apply();
            }
            
        });
    };
}]);