An Angular Controller
file: code/Winxle-Admin/CampaignCtrl.js
1: wApp.factory('CampaignIterator', function () {
2: "use strict";
3:
4: var campaigns = {
5: cur_pos: 0,
6: list: [],
7: fakeID: Date.now()
8: };
9:
10: campaigns.setList = function(new_list){
11: campaigns.list = new_list;
12: campaigns.cur_pos = 0;
13: };
14: campaigns.nextItem = function(){
15: var requested_pos = campaigns.cur_pos + 1;
16: var item = campaigns.list[requested_pos];
17: if (item) {
18: campaigns.cur_pos++;
19: return item;
20: }
21: return;
22: };
23: campaigns.prevItem = function(){
24: var requested_pos = campaigns.cur_pos - 1;
25: var item = campaigns.list[requested_pos];
26: if (item) {
27: campaigns.cur_pos--;
28: return item;
29: }
30: return;
31: };
32:
33: return campaigns;
34: });
35:
36: wApp.controller('CampaignCtrl',
37: ['$scope','$routeParams', '$sce', 'webService','CampaignIterator',
38: function ($scope, $routeParams, $sce, webService, CampaignIterator) {
39:
40: "use strict";
41: $scope.campaigns = [];
42: $scope.campaign = {};
43:
44: $scope.count = 0;
45: $scope.types_list = [];
46: $scope.status_list = [];
47:
48: $scope.filter = {
49: 'type': null,
50: 'status': null,
51: 'filterString': ''
52: };
53:
54: webService.get( 'campaign/globals').then(
55: function(data){
56: $scope.status_list = data.status;
57: $scope.types_list = data.types;
58: $scope.count = data.count;
59: },
60: function(err){
61: $log.error(['CampaignCtrl > no results',err]);
62: }
63: );
64:
65: $scope.n = 10;
66: $scope.o = 0;
67:
68: $scope.pageStart = $scope.o + 1;
69: $scope.pageEnd = $scope.o + $scope.campaigns.length;
70: $scope.lastPage = false;
71:
72: $scope.trustSrc = function(src) {
73: return $sce.trustAsResourceUrl( src );
74: };
75:
76: $scope.acceptNext = function() {
77: updateStatusAndNext($scope.campaign, 'approved');
78: };
79:
80: $scope.noteNext = function(status) {
81: $scope.campaign.status = status;
82: $('.note-box').show();
83: $('body').addClass('has-note-box');
84: };
85:
86: $scope.nextCampaign = function() {
87: gotoCampaign(CampaignIterator.nextItem());
88: };
89:
90: $scope.prevCampaign = function() {
91: gotoCampaign(CampaignIterator.prevItem());
92: };
93:
94: $scope.nextPage = function () {
95: if($scope.lastPage === false) {
96: $scope.o += $scope.n;
97: getCampaigns();
98: }
99: };
100:
101: $scope.$watch('status', function() {
102: $scope.viewParams.filter = $scope.status;
103: });
104:
105: $scope.prevPage = function () {
106: if($scope.pageStart - $scope.n > 0) {
107: $scope.o -= $scope.n;
108: $scope.lastPage = false;
109: getCampaigns();
110: }
111: };
112:
113: $scope.noteUpdate = function() {
114: updateStatusAndNext($scope.campaign, $scope.campaign.status, $scope.campaign.note);
115: $('.note-box').hide();
116: $('body').removeClass('has-note-box');
117: };
118: $scope.noteCancle = function() {
119: $scope.campaign.note='';
120: $('.note-box').hide();
121: $('body').removeClass('has-note-box');
122: };
123:
124: $scope.refresh = function() {
125: loadDetails( $scope.campaign.uuid );
126: };
127:
128: var url = 'campaign',
129: updatePagecount = function () {
130: $scope.pageStart = $scope.o + 1;
131: $scope.pageEnd = $scope.o + $scope.campaigns.length;
132: },
133: getCampaigns = function () {
134: var queryObject = {
135: "s": $scope.status,
136: "n": $scope.n,
137: "o": $scope.o
138: };
139:
140: if($scope.lastPage === false) {
141: webService.get( url, queryObject ).then(
142: function(data){
143: //Update UI using data or use the data to call another
144: if( ! data.isEmpty()){
145: if(data.length < $scope.n){
146: $scope.lastPage = true;
147: }
148: $scope.campaigns = data.map(function(item){
149: item.valid_from=Date.create(item.valid_from);
150: item.valid_until=Date.create(item.valid_until);
151: return item;
152: });
153:
154: CampaignIterator.setList($scope.campaigns);
155: updatePagecount();
156: }
157: },
158: function(err){
159: console.error(['CampaingCtrl > no results',err]);
160: }
161:
162: );
163: }
164:
165: },
166: loadDetails = function ( id ) {
167: var loadID = id || $routeParams.id;
168: var durl = url + '/' + loadID + '?filter='+ $scope.viewParams.filter;
169: webService.get( durl ).then(
170: function(data){
171: $scope.campaign = data;
172: $scope.campaign.valid_from = Date.create(data.valid_from);
173: $scope.campaign.valid_until = Date.create(data.valid_until);
174: },
175: function(err){
176: console.error(['CampaignCtrl > no details',err]);
177: }
178: );
179: },
180: gotoCampaign = function(campaign) {
181: if (campaign) {
182: loadDetails( campaign.uuid );
183: }
184: else {
185: alert("NO MORE CAMPAIGNS");
186: }
187: },
188: updateStatusAndNext = function(campaign, status, note) {
189: webService.put(
190: 'campaign/' + campaign.uuid + '/status',
191: { status: status,
192: note: note
193: }
194: ).then(
195: function(data){
196: $scope.campaign.status = status;
197: $scope.campaign.note = '';
198: gotoCampaign(CampaignIterator.nextItem());
199: },
200: function(err){
201: console.error(['Could not set status to approved',err]);
202: }
203: );
204: };
205:
206: if($routeParams.id) {
207: loadDetails();
208: }
209: else {
210: getCampaigns();
211: }
212:
213: }]);