-
Notifications
You must be signed in to change notification settings - Fork 0
/
Greetr.js
executable file
·126 lines (116 loc) · 3.17 KB
/
Greetr.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
/*!
* Greetr JavaScript Library v.0.0.1
* http://greetr.org
*
* Includes Sizzle.js
* http://sizzlejs.com/
*
* Copyright 2005, 2014 Amir Yacaman Foundation, Inc. and other contributors
* Released under the MIT license
*
* Date: 2015-08-03T16:01Z
*/
(function(global,$){
var Greetr = function(firstName, lastName, language) {
// Return function constructor so that the user does
// not have to declare the new keyword.
return new Greetr.init(firstName, lastName, language);
}
// Setting up languages our framework supports.
var supportedLangs = ['en', 'es'];
// Setting up greetings to be called.
var greetings = {
en: 'Hello',
es: 'Hola'
};
// Setting up formal greetings to be called.
var formalGreetings = {
en: 'Greetings',
es: 'Saludos'
};
// It is good practice to log a message when
// an item has been used.
var logMessages = {
en: 'Logged In',
es: 'Inició Sesión'
}
// This is where we will store Properties & Methods that we want to use
// inside the object that is returned from Greetr.
Greetr.prototype = {
fullName: function(){
return this.firstName + ' ' + this.lastName;
},
// Verifies they have selected a valid language.
validate: function(){
if (supportedLangs.indexOf(this.language) === -1) {
throw "Invalid Language";
}
},
greeting: function(){
return greetings[this.language] + ' ' + this.firstName + '!';
},
formalGreeting: function() {
return formalGreetings[this.language] + ', ' + this.fullName();
},
greet: function(formal){
var msg;
// if undefined or null it will coerced to 'false'
if (formal){
msg = this.formalGreeting();
}
else {
msg = this.greeting();
}
if (console) {
console.log(msg);
}
// 'this' refers to the calling object at execution time
// make the method chainable.
return this;
},
log: function(){
if (console) {
console.log(logMessages[this.language] + ': ' + this.fullName());
}
return this;
},
setLang: function(lang) {
this.language = lang;
this.validate();
return this;
},
HTMLGreeting: function(selector, formal) {
// Throw an error if no jQuery is detected.
if (!$){
throw 'jQuery not loaded';
}
// Throw an error if there is no selector passed.
if (!selector) {
throw 'Missing jQuery selector';
}
var msg;
if (formal) {
msg = this.formalGreeting();
}
else {
msg = this.greeting();
}
$(selector).html(msg);
return this;
}
};
// This is the Greetr function.
Greetr.init = function(firstName, lastName, language) {
// Check to see if firstName & lastName are initialize,
// and if not set them equal to an empty string.
// For language set it equal to english if no language was passed.
var self = this;
self.firstName = firstName || '';
self.lastName = lastName || '';
self.language = language || 'en';
}
// Here is where the objects will point to.
Greetr.init.prototype = Greetr.prototype;
// Exposing the Greetr function so the user can use 'G$()'.
global.Greetr = global.G$ = Greetr;
})(window,jQuery);