corrade-http-templates – Blame information for rev 43

Subversion Repositories:
Rev:
Rev Author Line No. Line
43 office 1 <!DOCTYPE html>
1 eva 2 <html lang="en">
3  
4 <head>
43 office 5 <title>Corrade Instant Message Template</title>
6  
1 eva 7 <meta charset="utf-8">
43 office 8 <meta http-equiv="X-UA-Compatible" content="IE=edge">
1 eva 9 <meta name="viewport" content="width=device-width, initial-scale=1">
43 office 10 <meta name="description" content="Group Chat Relay using Corrade">
11 <meta name="author" content="Wizardry and Steamworks">
12 <link rel="icon" href="favicon.ico">
1 eva 13  
23 vero 14 <link rel="stylesheet" href="css/style.css?v=2.0">
1 eva 15  
43 office 16 <!-- Bootstrap core CSS -->
17 <link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
18 <!-- jQuery UI CSS -->
19 <link href="bower_components/jquery-ui/themes/base/jquery-ui.min.css" rel="stylesheet">
20 <!-- <link rel="stylesheet" href="css/style.css?v=1.3"> -->
21 </head>
1 eva 22  
43 office 23 <body>
24  
25 <!-- Modal -->
26 <div id="dialog" class="modal fade" role="dialog">
27 <div class="modal-dialog">
28  
29 <!-- Modal content-->
30 <div class="modal-content">
31 <div class="modal-header">
32 <button type="button" class="close" data-dismiss="modal">&times;</button>
33 <h4 class="modal-title">Enter Agent Name</h4>
34 </div>
35 <form role="form" data-toggle="validator" class="form-inline">
36 <div class="modal-body">
37  
38 <div class="form-group">
39 <div class="input-group">
40 <label for="firstname">First Name</label>
41 <input class="form-control" maxlength="8" type="text" id="firstname" placeholder="Corrade" required>
42 </div>
43 <div class="input-group">
44 <label for="lastname">Last Name</label>
45 <input class="form-control" maxlength="8" type="text" id="lastname" placeholder="Resident" required>
46 </div>
47 </div>
48  
49  
50 </div>
51 <div class="modal-footer">
52 <button type="button" id="startConversation" class="btn btn-primary btn-lg">
53 <span class="glyphicon glyphicon-bullhorn" aria-hidden="true"></span> Start
54 </button>
55 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
56 </div>
57 </form>
58 </div>
59  
60 </div>
61 </div>
62  
63 <!-- <div id="dialog" title="Agent Name">
64 <form>
65 <fieldset class="ui-helper-reset">
66 <label for="firstname">Firstname</label>
67 <input type="text" name="firstname" id="firstname" value="" class="ui-widget-content ui-corner-all">
68 <label for="lastname">Lastname</label>
69 <input type="text" name="lastname" id="lastname" value="" class="ui-widget-content ui-corner-all">
70 </fieldset>
71 </form>
72 </div> -->
73  
74 <!-- <p>
75 Your name: <input type="text" size="8" value="Someone" id="name"></input>
76 <button id="add_tab">New Conversation</button>
77 </p> -->
78  
79 <div class="container">
80 <form role="form" data-toggle="validator">
81 <div class="form-group row">
82 <div class="input-group">
83 <span class="input-group-addon">
84 <label for="name">Your Name</label>
85 </span>
86 <input type="text" maxlength="8" value="Someone" id="name" class="form-control" required>
87 <span class="input-group-btn">
88 <button type="button" id="add_tab" class="btn btn-default" data-toggle="modal" data-target="#dialog">New Tab</button>
89 </span>
90 </div>
91 </div>
92 </form>
93  
94 <!-- This div holds the tabs that will be created. This structure should not be deleted. -->
95 <!-- <ul class="nav nav-tabs" role="tablist" id="tabs">
96 </ul> -->
97 <div id="tabs">
98 <ul>
99  
100 </ul>
101 <div>
102  
103 </div>
104 </div>
105 </div>
106  
107 <!-- Include jQuery -->
108 <script src="bower_components/jquery/dist/jquery.min.js"></script>
109 <!-- Include jQuery UI -->
110 <script src="bower_components/jquery-ui/jquery-ui.min.js"></script>
111 <!-- Include Bootstrap -->
112 <script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
113 <!-- Include Bootstrap Validator -->
114 <script src="bower_components/bootstrap-validator/js/validator.js"></script>
115 <!-- Include Velocity -->
116 <script src="bower_components/velocity/velocity.min.js"></script>
117  
1 eva 118 <script>
119 $(function() {
120 var firstName = $("#firstname"),
121 lastName = $("#lastname"),
122 tabTemplate = "<li id='#{id}'><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
123 tabCounter = 0,
124 messageIntervals = {},
5 zed 125 getConversationsTimeout;
1 eva 126  
43 office 127 var tabs = $("#tabs").tabs()
1 eva 128  
129 // Custom buttons and a "close" callback resetting the form inside.
43 office 130 /*var dialog = $("#dialog").dialog({
1 eva 131 autoOpen: false,
132 modal: true,
133 buttons: {
134 Add: function() {
135 addTab();
136 $(this).dialog("close");
137 },
138 Cancel: function() {
139 $(this).dialog("close");
140 }
141 },
142 close: function() {
143 form[0].reset();
144 }
145 });
146  
147 // Calls addTab function on submit and closes the dialog.
148 var form = dialog.find("form").on("submit", function(event) {
149 addTab();
150 dialog.dialog("close");
151 event.preventDefault();
152 });
153  
154 // The add tab button just opens the dialog and prompts for the avatar name.
155 $("#add_tab")
156 .button()
157 .on("click", function() {
158 dialog.dialog("open");
43 office 159 });*/
160  
161 $("#startConversation")
162 .on("click", function() {
163 addTab();
164 $("#dialog").modal('hide');
1 eva 165 });
166  
167 // Request the active conversations from the backend script and create tabs.
168 function getConversations() {
169 $.get("getConversations.php?t=" + Math.random(), function(data) {
170 var json = $.parseJSON(data);
171 $.each(json, function(index, avatar) {
172 if (!conversationExists(avatar.firstname, avatar.lastname))
173 addTab(avatar.firstname, avatar.lastname);
174 });
5 zed 175 getConversationsTimeout = setTimeout(
176 getConversations,
177 1000
178 );
1 eva 179 });
180 }
181  
182 // Function to send the message to an agent by passing it back through PHP.
183 function sendInstantMessage(e) {
184 var index = e.data.index;
4 eva 185 // If the parameters are empty, then do not send anything to the PHP script.
186 if($.trim($("#firstname-" + index).val()) == '' ||
187 $.trim($("#lastname-" + index).val()) == '' ||
188 $.trim($("#message-" + index).val()) == '')
189 return;
1 eva 190 $("#controls-" + index).animate({
191 opacity: 'hide'
192 }, 'slow');
193 $.ajax({
194 type: 'post',
195 url: "sendInstantMessage.php",
196 data: {
197 name: $("#name").val(),
198 firstname: $("#firstname-" + index).val(),
199 lastname: $("#lastname-" + index).val(),
200 message: $("#message-" + index).val()
201 }
202 }).done(function(data) {
203 if(data)
204 alert(data);
205 $("#message-" + index).val("");
206 $("#controls-" + index).animate({
207 opacity: 'show'
208 }, 'slow');
209 });
210 }
211  
212 // Loads all the stored instant messages from the log file named after the avatar.
213 function loadInstantMessage(index) {
214 $.get("messages/" + $("#firstname-" + index).val() + " " + $("#lastname-" + index).val() + ".log" + "?t=" + Math.random(), function(data) {
215 $("#chat-" + index).html(data);
43 office 216 /*$("#chat-" + index).animate({
217 scrollTop: $("#chat-" + index)[0].scrollHeight - $("#chat-" + index).height()
218 }, 1000);*/
22 office 219 // Scroll to the bottom of the conversation.
220 $("#chat-" + index).scrollTop($("#chat-" + index)[0].scrollHeight);
5 zed 221 messageIntervals[index] = setTimeout(
222 loadInstantMessage,
223 1000,
224 index
225 );
1 eva 226 });
227 }
228  
229 // This function checks whether a conversation / tab already exists.
230 function conversationExists(firstName, lastName) {
231 var exists = false;
232 for (var i = tabCounter; i >= 0; --i) {
233 if ($("#firstname-" + i).length &&
234 $("#firstname-" + i).val().toUpperCase() == firstName.toUpperCase() &&
235 $("#lastname-" + i).length &&
236 $("#lastname-" + i).val().toUpperCase() == lastName.toUpperCase()) {
237 exists = true;
238 break;
239 }
240 }
241 return exists;
242 }
243  
244 // Adds a new tab in case a conversation with that agent does not exist.
245 function addTab(first, last) {
246 var first = typeof first !== 'undefined' ? first : firstName.val();
247 var last = typeof last !== 'undefined' ? last : lastName.val();
248  
249 // A conversation with that agent exists, so just do nothing.
250 if (conversationExists(first, last))
251 return;
252  
253 // Normalize avatar names by capitalizing the first letter of the firstname and the last name.
254 first = first.charAt(0).toUpperCase() + first.substr(1);
255 last = last.charAt(0).toUpperCase() + last.substr(1);
256  
257 // Build the discussion form and add the tab.
258 var label = first + " " + last,
259 id = "tabs-" + tabCounter,
260 li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{id\}/g, tabCounter).replace(/#\{label\}/g, label));
261  
262 tabs.find(".ui-tabs-nav").append(li);
263 tabs.append("<div id='" + id + "'><p>" + " \
264 <div id='container-" + tabCounter + "'> \
5 zed 265 <textarea readonly='readonly' id='chat-" + tabCounter + "' rows='12'></textarea><br/> \
1 eva 266 <div id='controls-" + tabCounter + "'> \
267 Message: <input type='text' size='70' id='message-" + tabCounter + "'></input> \
268 <button type='button' id='send-" + tabCounter + "'>Send</button> \
269 <input type='hidden' name='firstname' id='firstname-" + tabCounter + "' value='" + first + "'></input> \
270 <input type='hidden' name='lastname' id='lastname-" + tabCounter + "' value='" + last + "'></input> \
271 </div> \
272 </div> \
273 " + "</p></div>");
274 tabs.tabs("refresh");
275  
276 // Subscribe to click event to send the instant message.
277 $("#send-" + tabCounter).on("click", {
278 index: tabCounter
279 }, sendInstantMessage);
280  
281 // Subscribe to pressing enter with the message input box selected.
282 $("#message-" + tabCounter).keypress({
283 index: tabCounter
284 }, function(e) {
285 if (e.which == 13) {
286 sendInstantMessage(e);
287 return false;
288 }
289 });
290  
291 ++tabCounter;
292 }
293  
294 // Close icon: removing the tab on click and delete the conversation.
295 tabs.on("click", "span.ui-icon-close", function() {
296 var panelId = $(this).closest("li").remove().attr("aria-controls");
297 var selectedIndex = $(this).closest("li").remove().attr("id");
298 // Pause the conversation retrieval timer.
5 zed 299 clearTimeout(getConversationsTimeout);
1 eva 300 $.ajax({
301 type: 'post',
302 url: "deleteConversation.php",
303 data: {
304 firstname: $("#firstname-" + selectedIndex).val(),
305 lastname: $("#lastname-" + selectedIndex).val()
306 }
307 }).done(function(data) {
308 $("#" + panelId).remove();
309 tabs.tabs("refresh");
310 // Resume the conversation retrieval timer.
5 zed 311 getConversationsTimeout = setTimeout(
1 eva 312 getConversations,
313 1000
314 );
315 });
316 });
317  
318 // Unbind the message retrieval interval from all other tabs except the active tab.
319 tabs.on('tabsactivate', function(event, ui) {
320 var selectedIndex = ui.newTab.closest("li").attr("id");
321 $.each(messageIntervals, function(index, value) {
322 if (index != selectedIndex) {
323 clearInterval(value);
324 }
325 });
326  
5 zed 327 messageIntervals[selectedIndex] = setTimeout(
1 eva 328 loadInstantMessage,
329 1000,
330 selectedIndex
331 );
332 });
333  
334 // Start a timer to load tabs of existing conversations.
5 zed 335 getConversationsTimeout = setTimeout(
1 eva 336 getConversations,
337 1000
338 );
339  
340 });
341 </script>
342 </body>
343  
344 </html>