-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodelmanager.js
132 lines (98 loc) · 2.58 KB
/
modelmanager.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
var app = angular.module('MyLittlePony');
app.service('modelManager', function(modelService) {
this.getPrePopPonies = function() {
return modelService.prePopulatedPonies;
};
this.getFavPonies = function() {
return modelService.favPonies;
};
this.getFavFlicks = function() {
return modelService.favFlicks;
}
this.addFavPony = function(ponyObj, success, error) {
var valid = ponyValidator(ponyObj);
if (valid) {
modelService.favPonies.unshift(ponyObj);
success();
} else {
error("Make sure you fill out all fields!");
}
};
this.deleteFavPony = function(index, success, error) {
if (index >= 0) {
var ponyArray = modelService.favPonies;
ponyArray.splice(index, 1);
success();
} else {
error("Error occurred!");
}
};
this.addFavFlick = function(flickObj, success, error) {
var validObj = flickValidator(flickObj);
if (validObj) {
modelService.favFlicks.unshift(flickObj);
success();
} else {
error("Make sure you fill out all fields and enter a valid Youtube video URL!")
}
};
this.deleteFavFlick = function(index, success, error) {
if (index >= 0) {
var flickArray = modelService.favFlicks;
flickArray.splice(index, 1);
success();
} else {
error("Error occurred!");
}
}
var ponyValidator = function(obj) {
var validProp = function(obj) {
if (obj.name && obj.img && obj.userName) {
return true;
} else {
return false;
}
};
var hasNumbers = function(string) {
return /\d/.test(string); //\d is short for digit [0-9]. The test() method tests for a match in a string.
};
var longEnough = function(string) {
return string.length > 3;
};
if (obj && validProp(obj) && !hasNumbers(obj.name) && longEnough(obj.name)) {
return true;
} else {
return false;
}
};
var flickValidator = function(obj) {
var validProp = function(obj) {
if (obj.title && obj.video && obj.userName) {
return true;
} else {
return false;
}
};
var validUrl = function(string) {
if (string.indexOf("youtu") > -1 && string.indexOf("v=") > -1) {
var index = string.indexOf("v=") + 2;
var end = string.substr(index);
string = "https://www.youtube.com/embed/" + end;
return string;
} else if ("youtu" && "/") {
var index = string.lastIndexOf("/") + 1;
var end = string.substr(index);
string = "https://www.youtube.com/embed/" + end;
return string;
} else {
return false;
}
};
if (obj && validProp(obj) && validUrl(obj.video)) {
obj.video = validUrl(obj.video);
return obj;
} else {
return false;
}
};
});