-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjquery.callme.js
232 lines (199 loc) · 6.35 KB
/
jquery.callme.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(function( $ ){
$.fn.callme = function( options ) {
var thisPhone = this, dispatcher = $({}), callMeOptions = {}, phono, call;
var settings = {
apikey : "",
dialpad: true,
togglemic: true,
slideopen: true,
buttontext: "loading...",
buttontextready: "Call me",
numbertodial: "app:9991457008"
};
if(options) {
//lowercase all options passed in
$.each(options, function(k,v){
callMeOptions[k.toLowerCase()] = v;
});
}
return this.each(function(){
if ( options ) {
$.extend( settings, callMeOptions );
}
var phone = buildPhone( settings );
$(this).append(phone);
// event handlers
phone.find(".phono-digit").bind({
mouseenter: function(){
$(this).addClass("ui-state-hover");
},
mouseleave: function(){
$(this).removeClass("ui-state-hover");
},
mousedown: function(){
$(this).addClass("ui-state-active");
},
mouseup: function(){
$(this).removeClass("ui-state-active");
},
click: function(e){
if( call )
call.digit( $(this).attr("title") );
e.preventDefault();
}
});
phone.find(".phono-mic-toggle").bind({
click: function(){
(this.checked)? phono.phone.headset(true): phono.phone.headset(false);
}
});
dispatcher.bind({
// phono is ready, bind events to the call button
phonoReady: function(){
var btn = phone.find("a.phono-phone-button");
btn.bind({
click: function(e){
(call) ? hangUpCall(settings, phone) : makeCall(settings, phone);
e.preventDefault();
}
});
btn.text(settings.buttontextready);
}
});
// initialize phono
phono = $.phono({
apiKey: settings.apikey,
onReady: function(){
dispatcher.trigger("phonoReady");
},
phone: {
onDisconnect: function(event) {
hangUpCall(settings, phone);
}
}
});
});
function buildPhone( settings ){
var phoneHldr = $( "<div/>")
.addClass("phono-hldr ui-widget ui-corner-all");
var phoneContent = $("<div/>")
.addClass("ui-widget-content ui-corner-all")
.css("padding","2px")
.appendTo(phoneHldr);
var statusLink = $("<a>")
.attr("href","#")
.addClass("phono-phone-button ui-widget-header ui-corner-all")
.css({
"text-align":"center",
"display":"block",
"padding":"8px 10px",
"text-decoration":"none"
})
.html(settings.buttontext)
.appendTo(phoneContent);
if ( settings.togglemic ){
var micToggle = $("<div/>")
.addClass("phono-mic-toggle-hldr")
.css({
"margin":"5px 0",
"font-size":"75%",
"text-align":"center"
})
.html("<input class='phono-mic-toggle' type='checkbox'/> Wearing a headset?")
.appendTo(phoneContent);
if(settings.slideopen)
micToggle.css("display","none");
}
if( settings.dialpad ){
var digitTblHldr = $("<div/>")
.addClass("phono-digit-hldr")
.appendTo(phoneContent);
if(settings.slideopen)
digitTblHldr.css("display","none");
var digitTbl = $( "<table/>" )
.addClass( "phono-digits-tbl" )
.css({
"width": "100%"
})
.appendTo( digitTblHldr );
var digits = [
{key:"1", value:" "},
{key:"2", value:"ABC"},
{key:"3", value:"DEF"},
{key:"4", value:"GHI"},
{key:"5", value:"JKL"},
{key:"6", value:"MNO"},
{key:"7", value:"PQRS"},
{key:"8", value:"TUV"},
{key:"9", value:"WXYZ"},
{key:"*", value:" "},
{key:"0", value:"+"},
{key:"#", value:" "}
];
var tblRows = $("<tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr>")
.appendTo(digitTbl);
$.each(tblRows.find("td"), function( i, el ) {
var digitObj = digits[i];
$(el).css({
"padding":"1px",
"vertical-align":"top",
"width":"33%"
});
var digit = $( "<a/> ")
.addClass( "phono-digit ui-state-default ui-corner-all" )
.attr("title", digitObj.key)
.attr("href","#")
.css({
"padding":"3px",
"display":"block",
"text-align":"center",
"text-decoration":"none",
"font-size":"90%"
})
.html( digitObj.key )
.appendTo( el );
var digitText = $("<span>")
.css({
"display":"block",
"font-size":"60%"
})
.html(digitObj.value)
.appendTo(digit);
});
}
return phoneHldr;
}
function makeCall(settings, phone){
phoneBtn = phone.find(".phono-phone-button");
phoneBtn.text("Calling...");
if( settings.togglemic && settings.slideopen )
phone.find(".phono-mic-toggle-hldr").slideDown();
if( settings.dialpad && settings.slideopen )
phone.find(".phono-digit-hldr").slideDown();
call = phono.phone.dial(settings.numbertodial, {
tones: true,
callerId: settings.callerid,
onAnswer: function(event) {
phoneBtn.text("Hangup");
},
onHangup: function() {
hangUpCall(settings, phone);
},
onDisconnect: function() {
hangUpCall(settings, phone);
}
});
}
function hangUpCall(settings, phone){
if(call){
call.hangup();
call = null;
}
phone.find(".phono-phone-button").text(settings.buttontextready);
if( settings.togglemic && settings.slideopen )
phone.find(".phono-mic-toggle-hldr").slideUp();
if( settings.dialpad && settings.slideopen )
phone.find(".phono-digit-hldr").slideUp();
}
};
})( jQuery );