-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalogue.js
140 lines (123 loc) · 3.89 KB
/
catalogue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var app = angular.module('catalogueApp', ['ngRoute', 'infinite-scroll']);
app.factory('catalogueService', function($http) {
var catalogueObj = {};
catalogueObj.fetchCatalogueDetails = function() {
return $http.get("catalogue.json");
}
catalogueObj.loadMore = function(rawSource, curSource) {
curSource = rawSource.slice(0, curSource.length + 10);
return curSource;
}
catalogueObj.get = function(source, id) {
var i;
for (i in source) {
if (source[i].id == id) {
return source[i];
}
}
}
catalogueObj.remove = function(id) {
var i;
for (i in catalogueObj.catalogues) {
if (catalogueObj.catalogues[i].id == id) {
catalogueObj.catalogues.splice(i, 1);
break;
}
}
}
catalogueObj.save = function(source, newproduct) {
if (newproduct.id == null) {
newproduct.id = source.length + 1;
source.push(newproduct);
} else {
var i;
for (i in source) {
if (source[i].id == newproduct.id) {
source[i] = newproduct;
break;
}
}
}
return source;
}
return catalogueObj;
});
app.config(function($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "listView.html",
controller: "listViewCtrl"
})
.when("/gridView", {
templateUrl: "gridView.html",
controller: "gridViewCtrl"
})
.when("/newProduct", {
templateUrl: "newProduct.html",
controller: "newProductCtrl"
})
.when("/editProduct/:id", {
templateUrl: "editProduct.html",
controller: "editProductCtrl"
})
.otherwise({
redirectTo: "/listView.html"
});
});
app.controller('listViewCtrl', function ($scope, catalogueService) {
catalogueService.fetchCatalogueDetails().success(function(data) {
var source = data.data;
for (var i = 1; i <= source.length; i++) {
source[i - 1].id = i;
}
$scope.catalogues = source.slice(0, 10);
$scope.loadMore = function(rawSource, curSource) {
$scope.catalogues = catalogueService.loadMore(source, $scope.catalogues);
}
$scope.deleteProduct = function(id) {
$scope.catalogues = catalogueService.remove($scope.catalogues, id);
}
});
});
app.controller('gridViewCtrl', function ($rootScope, $scope, catalogueService) {
catalogueService.fetchCatalogueDetails().success(function(data) {
var source = data.data;
for (var i = 1; i <= source.length; i++) {
source[i - 1].id = i;
}
$scope.catalogues = source.slice(0, 10);
$scope.loadMore = function(rawSource, curSource) {
$scope.catalogues = catalogueService.loadMore(source, $scope.catalogues);
}
$scope.deleteProduct = function(id) {
$scope.catalogues = catalogueService.remove($scope.catalogues, id);
}
});
});
app.controller('newProductCtrl', function ($scope, catalogueService) {
catalogueService.fetchCatalogueDetails().success(function(data) {
var source = data.data;
for (var i = 1; i <= source.length; i++) {
source[i - 1].id = i;
}
$scope.catalogues = source.slice(0, 10);
$scope.saveChanges = function() {
$scope.catalogues = catalogueService.save($scope.catalogues, $scope.newproduct);
$scope.newproduct = {};
}
});
});
app.controller('editProductCtrl', function ($scope, catalogueService, $routeParams) {
catalogueService.fetchCatalogueDetails().success(function(data) {
var source = data.data;
for (var i = 1; i <= source.length; i++) {
source[i - 1].id = i;
}
$scope.catalogues = source.slice(0, 10);
$scope.newproduct = angular.copy(catalogueService.get($scope.catalogues, $routeParams.id));
$scope.saveChanges = function() {
$scope.catalogues = catalogueService.save($scope.catalogues, $scope.newproduct);
$scope.newproduct = {};
}
});
});