var app = angular.module('TerpoTech', ['ngRoute']) .config(function($routeProvider, $locationProvider) { $routeProvider .when("/", { templateUrl: "/partial/home.html" }) .when("/products", { templateUrl: "/partial/products.html" }) .when("/products/:id", { templateUrl: "/partial/products.html", controller: "Products" }) .when("/product/:id/error", { templateUrl: "/partial/product_unavailable.html" }) .when("/product/:id", { templateUrl: "/partial/product.html", controller: "Product" }) .when("/services", { templateUrl: "/partial/services.html" }) .when("/about", { templateUrl: "/partial/about.html" }) .when("/contact", { templateUrl: "/partial/contact.html" }) .when("/warranty", { templateUrl: "/partial/warranty.html" }); $locationProvider.html5Mode(true); }); app.controller('Categories', function($scope, $location, api) { api.get('Categories', function(data) { $scope.categories = data; }); $scope.select = function(category) { $location.path("/products/" + category.id); }; }); app.controller('Promotions', function($scope, $location, api) { api.get('Products', function(data) { $scope.products = data; $scope.promotions = data.desc('timestamp').filter(function(x) { return x.promote == true; }); if ($scope.promotions.length > 3) { $scope.promotions.slice(0, 3); } }); api.ping(); $scope.view = function(id) { $location.path("/product/" + id); } }); app.controller('Filters', function($rootScope, $scope, $location, $routeParams, api) { $scope.sliderConfig = { min: 0, max: 500, step: 10 }; $scope.category = undefined; $scope.min = $scope.sliderConfig.min; $scope.max = $scope.sliderConfig.max; $scope.removeCategory = function(filter) { $scope.category = undefined; $location.path("/products"); }; $scope.$on('applyFilter', function() { console.log('applyFilter...'); for (i = 0; i < $scope.filters.length; i++) { if ($scope.filters[i].id == api.message().id) { $scope.filters.splice(i, 1); } } if (api.message().value != undefined) $scope.list.push(api.message()); $('#filters').css({ backgroundColor: '#FF5353' }).animate({ backgroundColor: '#FFF' }, 'slow'); }); if ($routeParams.id) { api.get('Categories', function(data) { $scope.categories = data; $scope.category = $scope.categories.get('id', $routeParams.id); }); } function broadcastBand() { $rootScope.$broadcast('applyPriceFilter', { band: { min: $scope.min, max: $scope.max } }); } $scope.$watch('min', function(newVal, oldVal) { if (newVal != oldVal) { broadcastBand(); } }); $scope.$watch('max', function(newVal, oldVal) { if (newVal != oldVal) { broadcastBand(); } }); }); app.controller('Products', function($scope, $location, $routeParams, api) { api.get('Products', function(list) { $scope.products = list; if ($routeParams.id) { filterByCategory($routeParams.id); } }); api.get('Categories', function(data) { $scope.categories = data; }); $scope.$on('applyPriceFilter', function(event, args) { console.log('applyPriceFilter...'); if (args.band != undefined) { api.get('Products', function(list) { $scope.products = list; if ($routeParams.id) { filterByCategory($routeParams.id); } var list = [] for (i = 0; i < $scope.products.length; i++) { if ($scope.products[i].price >= args.band.min && $scope.products[i].price <= args.band.max) { list.push($scope.products[i]); } } $scope.products = list; }); } }); $scope.view = function(id) { $location.path("/product/" + id); } $scope.preview = function(id, image) { $('#image' + id).attr({ src: image }); window.event.cancelBubble = true; } function filterByCategory(id) { var list = []; for (i = 0; i < $scope.products.length; i++) { if ($scope.products[i].category == id) { list.push($scope.products[i]); } } $scope.products = list; }; }); app.controller('Product', function($scope, $location, $routeParams, api) { $scope.item = undefined; api.get('Products', function(list) { $scope.list = list; if ($routeParams.id) { $scope.item = list.get('id', $routeParams.id); //$scope.item.images[0] = $scope.item.images[0].substring(0, $scope.item.images[0].lastIndexOf("=s")) + "=s700"; } if (!$scope.item) { $location.path('/product/' + $routeParams.id + '/error'); }; }); $scope.preview = function(id, image) { //image = image.substring(0, image.lastIndexOf("=s")) + "=s700"; $('#image' + id).attr({ src: image }); window.event.cancelBubble = true; } $scope.next = function(id) { // here image URLs are not clean of size attrs var image = $('#image' + id).attr('src'); image = image.substring(0, image.lastIndexOf("=s")); for (i = 0; i < $scope.item.images.length; i++) { var thumbnail = $scope.item.images[i].substring(0, $scope.item.images[i].lastIndexOf("=s")); if (image.indexOf(thumbnail) > -1) { var next = i + 1; if (next == $scope.item.images.length) next = 0; var largeImage = $scope.item.images[next]; $('#image' + id).attr({ src: largeImage }); } } } }); app.factory('api', function($rootScope, $location, $http) { var cache = []; var msg = undefined; function get(area, callback) { if (cache.get('key', area)) { console.info('serving ' + area + ' from cache.'); callback(cache.get('key', area).data); } else { $http.get('/api/' + area.toLowerCase()).success(function(data) { console.info('retrieving ' + area + '...'); cache.push({ key: area, data: data }); callback(cache.get('key', area).data); }); } } function broadcast(msg) { console.log('broadcasting on topic: ' + msg.topic); this.msg = msg.value; $rootScope.$broadcast(msg.topic); } function message() { return msg; } function ping() { $http.post('/api/ping'); } return { broadcast: broadcast, message: message, ping: ping, get: function(key, callback) { get(key, callback); } }; }); app.filter('filterProducts', function() { return function(items, scope) { var filtered = []; angular.forEach(items, function(item) { function contains(target, substring) { return target.toLowerCase().indexOf(substring.toLowerCase()) > -1; } if (scope.search != undefined) { if (contains(item.brand, scope.search) || contains(item.model, scope.search) || contains(item.brand + ' ' + item.model, scope.search)) { filtered.push(item); } } else { filtered.push(item); } }); return filtered; }; }); app.controller('Services', function($scope, $http, api) { $scope.services = [{ "id": "pc", "name": "Ремонт на компютри", "short": "" }, { "id": "laptops", "name": "Ремонт на лаптопи", "short": "лаптопи" }, { "id": "monitors", "name": "Ремонт на монитори", "short": "монитори" }]; }); app.directive("mySlider", function() { return { restrict: 'A', scope: { config: "=config", min: "=min", max: "=max" }, link: function(scope, elem, attrs) { $(elem).slider({ range: true, min: scope.config.min, max: scope.config.max, values: [scope.config.min, scope.config.max], step: scope.config.step, slide: function(event, ui) { scope.$apply(function() { scope.min = ui.values[0]; scope.max = ui.values[1]; }); } }); } } }); Array.prototype.get = function(key, value) { for (i = 0; i < this.length; i++) { if (this[i][key] == value) return this[i]; } }; Array.prototype.asc = function(key) { return this.sort(function(a, b) { if (typeof a[key] === 'string' || a[key] instanceof String) { return a[key].localeCompare(b[key]); } else { return a[key] - b[key]; } }); }; Array.prototype.desc = function(key) { return this.sort(function(a, b) { if (typeof a[key] === 'string' || a[key] instanceof String) { return -1 * (a[key].localeCompare(b[key])); } else { return -1 * (a[key] - b[key]); } }); }; Array.prototype.paint = function(colors) { for (i = 0; i < this.length; i++) { this[i].color = colors[i]; } return this; }; Date.prototype.today = function() { var dd = this.getDate() < 10 ? '0' + this.getDate() : this.getDate(); var mm = this.getMonth() + 1 < 10 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1; var yyyy = this.getFullYear(); return yyyy + '.' + mm + '.' + dd; } Date.prototype.timestamp = function() { return new Date().now(); }