-
Notifications
You must be signed in to change notification settings - Fork 0
/
rides-sidebar-ng.html
205 lines (163 loc) · 8 KB
/
rides-sidebar-ng.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html ng-app="pelotonApp">
<head>
<style>
</style>
<base target="_top">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.0/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<script>
angular.module('pelotonApp', [])
.controller('PelotonRideController', function($scope, $httpParamSerializer) {
var rideController = this;
rideController.loading=false;
rideController.formOptions={};
rideController.totalRides=null;
rideController.rides=[];
rideController.page=0;
rideController.pages=0;
rideController.filters=JSON.parse("<?= JSON.stringify(getRideFilters()) ?>");
rideController.loadFilters=function(){
angular.forEach(rideController.filters,function(f){
console.log(f);
console.log(f.values.length);
rideController.formOptions[f.name]=null;
if(f.type=='collection'){
f.values.sort(function(a,b){return a.list_order-b.list_order});
}
});
};
rideController.loadRides=function(page){
if(page==null) rideController.resetRides();
rideController.loading=true;
google.script.run.withSuccessHandler(function(result){
console.log(JSON.stringify(result));
rideController.totalRides=result.total;
rideController.page=result.page;
rideController.rides.push.apply(rideController.rides,result.rides);
rideController.pages=result.pages;
$scope.$digest();
rideController.loading=false;
$scope.$digest();
})
.getRides(page,10, rideController.getQueryParams());
};
rideController.resetRides=function(){
rideController.page=0;
rideController.pages=0;
rideController.rides=[];
};
rideController.loadRideCounts=function(){
google.script.run.withSuccessHandler(function(result){
console.log(JSON.stringify(result));
rideController.resetRides();
rideController.totalRides=result.total;
$scope.$digest();
})
.getRides(0,1, rideController.getQueryParams());
};
rideController.getQueryParams=function(){
var form=rideController.formOptions;
console.log(JSON.stringify(form));
var obj={};
$(Object.keys(form)).each(function(idx, key){
if(form[key]==null) return;
else if(form[key]['value']) obj[key]=form[key].value;
else obj[key]=form[key];
});
console.log(JSON.stringify(obj));
return $httpParamSerializer(obj);
};
rideController.addBookmark=function (ride){
console.log("Add Bookmark "+JSON.stringify(ride));
google.script.run.withSuccessHandler(function(result){
if(result.success){
rideController.rides.map(function(ride){ if(ride.id==result.id) ride.is_favorite=true;});
$scope.$digest();
}
}).addFavoriteRide(ride.id);
};
rideController.showDetails=function(ride){
google.script.run.showRideDetails(ride.id);
};
rideController.clearBookmark=function (ride){
console.log("Clear Bookmark "+JSON.stringify(ride));
google.script.run.withSuccessHandler(function(result){
if(result.success){
rideController.rides.map(function(ride){ if(ride.id==result.id) ride.is_favorite=false;});
$scope.$digest();
}
}).removeFavoriteRide(ride.id);
}
}).directive("formOnChange", function($parse){
return {
require: "form",
link: function(scope, element, attrs){
var callBack = $parse(attrs.formOnChange);
element.on("change", function(){
callBack(scope);
return true;
});
}
}
});
</script>
</head>
<body ng-controller="PelotonRideController as peloton" ng-init="peloton.loadFilters()">
<div class="card" style="width: 18rem;">
<div class="card-header">Find Classes</div>
<div class="card-body">
<form form-on-change="peloton.loadRideCounts()" id="ride-filter-form">
<div id="ride-toggles">
<div ng-repeat="filter in peloton.filters | filter: {type:'toggle'}"><input type="checkbox" ng-model="peloton.formOptions[filter.name]"> <span class='glyphicon' ng-if="filter.values[0].display_image_url" style='background:url({{filter.values[0].display_image_url}}); background-repeat:none; background-size:18px 18px; '> </span> {{filter.display_name}}</div>
</div>
<div id="ride-dropdowns">
<div ng-repeat="filter in peloton.filters | filter: {type:'collection'}">
{{filter.display_name}}: <select ng-model="peloton.formOptions[filter.name]" ng-options="val as val.display_name for val in filter.values">
<option value="">Select</option>
</select>
</div>
</div>
</form>
</div>
<div class="card-footer">
<button ng-disabled="! peloton.totalRides" ng-click="peloton.loadRides()"
class="badge badge-pill badge-danger" id="load">Load <span id='total' ng-show="peloton.totalRides!=null">{{peloton.totalRides}}</span> Classes </button>
<a ng-disabled="! peloton.totalRides" target="_blank" class="text-danger" title="Search on Peloton Site" href="https://members.onepeloton.com/classes/cycling?utm_source=ios_app&utm_medium=in_app&{{peloton.getQueryParams()}}">
<span class="fa fa-share"></span>
</a>
</div>
</div>
<div id="rides" class="card-columns">
<div class="card" ng-repeat="ride in peloton.rides" style="max-width: 400px">
<img class="card-img-top" src="{{ride.image_url}}" alt="{{ride.title}} with {{ride.instructor.name}}">
<div class="card-body">
<span ng-if="ride.user_workouts" class="badge badge-pill badge-success" title="You took this ride {{ride.user_workouts}} time(s)">Taken</span>
<button ng-if="ride.is_favorite" ng-click="peloton.clearBookmark(ride)" class="badge badge-pill badge-secondary">Bookmarked</button>
<button ng-if="!ride.is_favorite" ng-click="peloton.addBookmark(ride)" class="badge badge-pill badge-info">Add Bookmark</button>
<h5 class="card-title">{{ride.title}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{ride.instructor.name}}<br>Aired on {{ride.aired | date:'MM/dd/yyyy @ h:mma'}}</h6>
<p class="card-text">{{ride.description}}</p>
</div>
<div class="card-footer">
<small class="text-muted">{{ride.workouts}} total workouts</small>
<button ng-click="peloton.showDetails(ride)" data-ride-id="{{ride.id}}" class="ride-load-button badge-pill badge badge-danger">Details</button>
<a target="_blank" class="text-danger" title="Open on Peloton Site" href="https://members.onepeloton.com/classes/cycling?utm_source=ios_app&utm_medium=in_app&{{peloton.getQueryParams()}}&modal=classDetailsModal&classId={{ride.id}}">
<span class="fa fa-share"></span>
</a>
</div>
</div>
<div ng-if="peloton.rides.length>0" class="card" id="load">
<div class="card-body">
<p class="card-text"><small class="text-muted">Showing page {{peloton.page+1}} out of {{peloton.pages}}(s).
Displaying {{peloton.rides.length}} out of {{peloton.totalRides}} total rides found.</small></p>
<button ng-show="!peloton.loading && peloton.page+1 < peloton.pages" ng-click="peloton.loadRides(peloton.page+1)"
class="badge badge-pill badge-danger">Load More (Page {{peloton.page+1}}/{{peloton.pages}}) </button>
</div>
</div>
</div>
</body>
</html>