Skip to content

Instantly share code, notes, and snippets.

@agustinvinao
Created January 21, 2014 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save agustinvinao/8546440 to your computer and use it in GitHub Desktop.
Save agustinvinao/8546440 to your computer and use it in GitHub Desktop.
OpenDataMDP AngularJS providers app
'use strict';
angular.module("providersApp", ["providersApp.services", "providersApp.filters", "providersApp.controllers", "ngResource", "ngRoute", "domstorage"])
.config(["$routeProvider", "$httpProvider", function($routeProvider, $httpProvider) {
$routeProvider.when("/", {
templateUrl: "/angular/views/providers/index.angular.html",
controller: 'ProvidersCntl'
});
$routeProvider.when("/details/:slug", {
templateUrl: "/angular/views/providers/details.angular.html",
controller: 'ProviderDetailCntl'
});
$routeProvider.otherwise({
redirectTo: '/'
});
$httpProvider.interceptors.push('DOMStorageHttpInterceptor');
}
])
.run(["$rootScope", "$locale", "$window", "$log", function($rootScope, $locale, $window, $log) {
}]);
angular.module("providersApp.services", [])
.factory("PurchaseOrderDetail", ['$resource', function($resource) {
var functions = {
test: function(){
}
};
this.addFunctions = function(purchase_order_detail) {
return angular.extend(purchase_order_detail, functions);
};
return this;
}])
.factory("PurchaseOrder", ['$resource', function($resource) {
var functions = {
ocurredAt: function() {
return new Date(this.occurred_at);
},
yearShow: function(){
return this.ocurredAt().getYear()+1900;
}
};
this.addFunctions = function(purchase_order) {
return angular.extend(purchase_order, functions);
};
return this;
}])
.factory("Provider", ["$resource", "PurchaseOrder", "PurchaseOrderDetail", "$log", function($resource, PurchaseOrder, PurchaseOrderDetail, $log) {
var ProviderResource = $resource("/providers/:id.json",
{id: '@id'},
{
allAmounts: { method: "get", url: "/providers/singlepage.json", isArray: true, params: {} }
}
);
var functions = {
purchaseOrdersByYear: function(){
var byYear = _.groupBy(this.purchase_orders, function(purchaseOrder){
return purchaseOrder.yearShow();
});
var byYearFinal = [];
for (var key in byYear) {
byYearFinal.push({ year: key, purchaseOrders: byYear[key], count: byYear[key].length });
}
return byYearFinal;
}
};
this.addFunctions = function(provider) {
angular.extend(provider, functions);
angular.forEach(provider.purchase_orders, function(purchase_order) {
PurchaseOrder.addFunctions(purchase_order);
angular.forEach(purchase_order.purchase_order_details, function(purchase_order_detail) {
PurchaseOrderDetail.addFunctions(purchase_order_detail);
});
});
};
return angular.extend(this, ProviderResource);
}]);
angular.module("providersApp.controllers", [])
.controller("ProviderDetailCntl", ["$scope", "$routeParams", "Provider", "$log", function($scope, $routeParams, Provider, $log) {
$scope.slug = $routeParams.slug;
$scope.currentProvider = Provider.get({id: $routeParams.slug}, function(provider) {
Provider.addFunctions(provider.provider);
});
$scope.$watch('currentProvider.provider', function(){
if($scope.currentProvider.provider){
$scope.purchaseOrdersByYear = $scope.currentProvider.provider.purchaseOrdersByYear();
}
})
}])
.controller("ProvidersCntl", ["$scope", "$filter", "Provider", "$log", function($scope, $filter, Provider, $log) {
$scope.providers = Provider.allAmounts({}, function(providers) {
$scope.totalProvidersAmount = 0;
$scope.totalProvidersOrders = 0;
$scope.totalProvidersAmount = _.reduce(providers, function(memo, num) {
return memo + num.total_amount;
}, 0);
$scope.totalProvidersOrders = _.reduce(providers, function(memo, num) {
return memo + num.purchase_orders_count;
}, 0);
});
$scope.$watch("search", function(query) {
$scope.filteredProviders = $filter("filter")($scope.providers, query);
});
$scope.$watch("filteredProviders", function() {
if ($scope.filteredProviders) {
$scope.totalProvidersAmount = _.reduce($scope.filteredProviders, function(memo, num) {
return memo + num.total_amount;
}, 0);
$scope.totalProvidersOrders = _.reduce($scope.filteredProviders, function(memo, num) {
return memo + num.purchase_orders_count;
}, 0);
}
});
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment