corrade-http-templates – Diff between revs 22 and 23
?pathlinks?
Rev 22 | Rev 23 | |||
---|---|---|---|---|
1 | <!doctype html> |
1 | <!doctype html> |
|
2 | <html lang="en"> |
2 | <html lang="en"> |
|
3 | |
3 | |
|
4 | <head> |
4 | <head> |
|
5 | <meta charset="utf-8"> |
5 | <meta charset="utf-8"> |
|
6 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
6 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
|
7 | <title>Corrade Instant Message Template</title> |
7 | <title>Corrade Instant Message Template</title> |
|
8 | |
8 | |
|
9 | |
9 | |
|
10 | <link rel="stylesheet" href="css/fonts.css?v=1.0"> |
10 | <link rel="stylesheet" href="css/fonts.css?v=1.0"> |
|
11 | <link rel="stylesheet" href="css/style.css?v=1.9"> |
11 | <link rel="stylesheet" href="css/style.css?v=2.0"> |
|
12 | |
12 | |
|
13 | <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"> |
13 | <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css"> |
|
14 | <script src="https://code.jquery.com/jquery-1.12.4.js"></script> |
14 | <script src="https://code.jquery.com/jquery-1.12.4.js"></script> |
|
15 | <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> |
15 | <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> |
|
16 | |
16 | |
|
17 | <script> |
17 | <script> |
|
18 | $(function() { |
18 | $(function() { |
|
19 | var firstName = $("#firstname"), |
19 | var firstName = $("#firstname"), |
|
20 | lastName = $("#lastname"), |
20 | lastName = $("#lastname"), |
|
21 | tabTemplate = "<li id='#{id}'><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>", |
21 | tabTemplate = "<li id='#{id}'><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>", |
|
22 | tabCounter = 0, |
22 | tabCounter = 0, |
|
23 | messageIntervals = {}, |
23 | messageIntervals = {}, |
|
24 | getConversationsTimeout; |
24 | getConversationsTimeout; |
|
25 | |
25 | |
|
26 | var tabs = $("#tabs").tabs(); |
26 | var tabs = $("#tabs").tabs(); |
|
27 | |
27 | |
|
28 | // Custom buttons and a "close" callback resetting the form inside. |
28 | // Custom buttons and a "close" callback resetting the form inside. |
|
29 | var dialog = $("#dialog").dialog({ |
29 | var dialog = $("#dialog").dialog({ |
|
30 | autoOpen: false, |
30 | autoOpen: false, |
|
31 | modal: true, |
31 | modal: true, |
|
32 | buttons: { |
32 | buttons: { |
|
33 | Add: function() { |
33 | Add: function() { |
|
34 | addTab(); |
34 | addTab(); |
|
35 | $(this).dialog("close"); |
35 | $(this).dialog("close"); |
|
36 | }, |
36 | }, |
|
37 | Cancel: function() { |
37 | Cancel: function() { |
|
38 | $(this).dialog("close"); |
38 | $(this).dialog("close"); |
|
39 | } |
39 | } |
|
40 | }, |
40 | }, |
|
41 | close: function() { |
41 | close: function() { |
|
42 | form[0].reset(); |
42 | form[0].reset(); |
|
43 | } |
43 | } |
|
44 | }); |
44 | }); |
|
45 | |
45 | |
|
46 | // Calls addTab function on submit and closes the dialog. |
46 | // Calls addTab function on submit and closes the dialog. |
|
47 | var form = dialog.find("form").on("submit", function(event) { |
47 | var form = dialog.find("form").on("submit", function(event) { |
|
48 | addTab(); |
48 | addTab(); |
|
49 | dialog.dialog("close"); |
49 | dialog.dialog("close"); |
|
50 | event.preventDefault(); |
50 | event.preventDefault(); |
|
51 | }); |
51 | }); |
|
52 | |
52 | |
|
53 | // The add tab button just opens the dialog and prompts for the avatar name. |
53 | // The add tab button just opens the dialog and prompts for the avatar name. |
|
54 | $("#add_tab") |
54 | $("#add_tab") |
|
55 | .button() |
55 | .button() |
|
56 | .on("click", function() { |
56 | .on("click", function() { |
|
57 | dialog.dialog("open"); |
57 | dialog.dialog("open"); |
|
58 | }); |
58 | }); |
|
59 | |
59 | |
|
60 | // Request the active conversations from the backend script and create tabs. |
60 | // Request the active conversations from the backend script and create tabs. |
|
61 | function getConversations() { |
61 | function getConversations() { |
|
62 | $.get("getConversations.php?t=" + Math.random(), function(data) { |
62 | $.get("getConversations.php?t=" + Math.random(), function(data) { |
|
63 | var json = $.parseJSON(data); |
63 | var json = $.parseJSON(data); |
|
64 | $.each(json, function(index, avatar) { |
64 | $.each(json, function(index, avatar) { |
|
65 | if (!conversationExists(avatar.firstname, avatar.lastname)) |
65 | if (!conversationExists(avatar.firstname, avatar.lastname)) |
|
66 | addTab(avatar.firstname, avatar.lastname); |
66 | addTab(avatar.firstname, avatar.lastname); |
|
67 | }); |
67 | }); |
|
68 | getConversationsTimeout = setTimeout( |
68 | getConversationsTimeout = setTimeout( |
|
69 | getConversations, |
69 | getConversations, |
|
70 | 1000 |
70 | 1000 |
|
71 | ); |
71 | ); |
|
72 | }); |
72 | }); |
|
73 | } |
73 | } |
|
74 | |
74 | |
|
75 | // Function to send the message to an agent by passing it back through PHP. |
75 | // Function to send the message to an agent by passing it back through PHP. |
|
76 | function sendInstantMessage(e) { |
76 | function sendInstantMessage(e) { |
|
77 | var index = e.data.index; |
77 | var index = e.data.index; |
|
78 | // If the parameters are empty, then do not send anything to the PHP script. |
78 | // If the parameters are empty, then do not send anything to the PHP script. |
|
79 | if($.trim($("#firstname-" + index).val()) == '' || |
79 | if($.trim($("#firstname-" + index).val()) == '' || |
|
80 | $.trim($("#lastname-" + index).val()) == '' || |
80 | $.trim($("#lastname-" + index).val()) == '' || |
|
81 | $.trim($("#message-" + index).val()) == '') |
81 | $.trim($("#message-" + index).val()) == '') |
|
82 | return; |
82 | return; |
|
83 | $("#controls-" + index).animate({ |
83 | $("#controls-" + index).animate({ |
|
84 | opacity: 'hide' |
84 | opacity: 'hide' |
|
85 | }, 'slow'); |
85 | }, 'slow'); |
|
86 | $.ajax({ |
86 | $.ajax({ |
|
87 | type: 'post', |
87 | type: 'post', |
|
88 | url: "sendInstantMessage.php", |
88 | url: "sendInstantMessage.php", |
|
89 | data: { |
89 | data: { |
|
90 | name: $("#name").val(), |
90 | name: $("#name").val(), |
|
91 | firstname: $("#firstname-" + index).val(), |
91 | firstname: $("#firstname-" + index).val(), |
|
92 | lastname: $("#lastname-" + index).val(), |
92 | lastname: $("#lastname-" + index).val(), |
|
93 | message: $("#message-" + index).val() |
93 | message: $("#message-" + index).val() |
|
94 | } |
94 | } |
|
95 | }).done(function(data) { |
95 | }).done(function(data) { |
|
96 | if(data) |
96 | if(data) |
|
97 | alert(data); |
97 | alert(data); |
|
98 | $("#message-" + index).val(""); |
98 | $("#message-" + index).val(""); |
|
99 | $("#controls-" + index).animate({ |
99 | $("#controls-" + index).animate({ |
|
100 | opacity: 'show' |
100 | opacity: 'show' |
|
101 | }, 'slow'); |
101 | }, 'slow'); |
|
102 | }); |
102 | }); |
|
103 | } |
103 | } |
|
104 | |
104 | |
|
105 | // Loads all the stored instant messages from the log file named after the avatar. |
105 | // Loads all the stored instant messages from the log file named after the avatar. |
|
106 | function loadInstantMessage(index) { |
106 | function loadInstantMessage(index) { |
|
107 | $.get("messages/" + $("#firstname-" + index).val() + " " + $("#lastname-" + index).val() + ".log" + "?t=" + Math.random(), function(data) { |
107 | $.get("messages/" + $("#firstname-" + index).val() + " " + $("#lastname-" + index).val() + ".log" + "?t=" + Math.random(), function(data) { |
|
108 | $("#chat-" + index).html(data); |
108 | $("#chat-" + index).html(data); |
|
109 | // Scroll to the bottom of the conversation. |
109 | // Scroll to the bottom of the conversation. |
|
110 | $("#chat-" + index).scrollTop($("#chat-" + index)[0].scrollHeight); |
110 | $("#chat-" + index).scrollTop($("#chat-" + index)[0].scrollHeight); |
|
111 | messageIntervals[index] = setTimeout( |
111 | messageIntervals[index] = setTimeout( |
|
112 | loadInstantMessage, |
112 | loadInstantMessage, |
|
113 | 1000, |
113 | 1000, |
|
114 | index |
114 | index |
|
115 | ); |
115 | ); |
|
116 | }); |
116 | }); |
|
117 | } |
117 | } |
|
118 | |
118 | |
|
119 | // This function checks whether a conversation / tab already exists. |
119 | // This function checks whether a conversation / tab already exists. |
|
120 | function conversationExists(firstName, lastName) { |
120 | function conversationExists(firstName, lastName) { |
|
121 | var exists = false; |
121 | var exists = false; |
|
122 | for (var i = tabCounter; i >= 0; --i) { |
122 | for (var i = tabCounter; i >= 0; --i) { |
|
123 | if ($("#firstname-" + i).length && |
123 | if ($("#firstname-" + i).length && |
|
124 | $("#firstname-" + i).val().toUpperCase() == firstName.toUpperCase() && |
124 | $("#firstname-" + i).val().toUpperCase() == firstName.toUpperCase() && |
|
125 | $("#lastname-" + i).length && |
125 | $("#lastname-" + i).length && |
|
126 | $("#lastname-" + i).val().toUpperCase() == lastName.toUpperCase()) { |
126 | $("#lastname-" + i).val().toUpperCase() == lastName.toUpperCase()) { |
|
127 | exists = true; |
127 | exists = true; |
|
128 | break; |
128 | break; |
|
129 | } |
129 | } |
|
130 | } |
130 | } |
|
131 | return exists; |
131 | return exists; |
|
132 | } |
132 | } |
|
133 | |
133 | |
|
134 | // Adds a new tab in case a conversation with that agent does not exist. |
134 | // Adds a new tab in case a conversation with that agent does not exist. |
|
135 | function addTab(first, last) { |
135 | function addTab(first, last) { |
|
136 | var first = typeof first !== 'undefined' ? first : firstName.val(); |
136 | var first = typeof first !== 'undefined' ? first : firstName.val(); |
|
137 | var last = typeof last !== 'undefined' ? last : lastName.val(); |
137 | var last = typeof last !== 'undefined' ? last : lastName.val(); |
|
138 | |
138 | |
|
139 | // A conversation with that agent exists, so just do nothing. |
139 | // A conversation with that agent exists, so just do nothing. |
|
140 | if (conversationExists(first, last)) |
140 | if (conversationExists(first, last)) |
|
141 | return; |
141 | return; |
|
142 | |
142 | |
|
143 | // Normalize avatar names by capitalizing the first letter of the firstname and the last name. |
143 | // Normalize avatar names by capitalizing the first letter of the firstname and the last name. |
|
144 | first = first.charAt(0).toUpperCase() + first.substr(1); |
144 | first = first.charAt(0).toUpperCase() + first.substr(1); |
|
145 | last = last.charAt(0).toUpperCase() + last.substr(1); |
145 | last = last.charAt(0).toUpperCase() + last.substr(1); |
|
146 | |
146 | |
|
147 | // Build the discussion form and add the tab. |
147 | // Build the discussion form and add the tab. |
|
148 | var label = first + " " + last, |
148 | var label = first + " " + last, |
|
149 | id = "tabs-" + tabCounter, |
149 | id = "tabs-" + tabCounter, |
|
150 | li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{id\}/g, tabCounter).replace(/#\{label\}/g, label)); |
150 | li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{id\}/g, tabCounter).replace(/#\{label\}/g, label)); |
|
151 | |
151 | |
|
152 | tabs.find(".ui-tabs-nav").append(li); |
152 | tabs.find(".ui-tabs-nav").append(li); |
|
153 | tabs.append("<div id='" + id + "'><p>" + " \ |
153 | tabs.append("<div id='" + id + "'><p>" + " \ |
|
154 | <div id='container-" + tabCounter + "'> \ |
154 | <div id='container-" + tabCounter + "'> \ |
|
155 | <textarea readonly='readonly' id='chat-" + tabCounter + "' rows='12'></textarea><br/> \ |
155 | <textarea readonly='readonly' id='chat-" + tabCounter + "' rows='12'></textarea><br/> \ |
|
156 | <div id='controls-" + tabCounter + "'> \ |
156 | <div id='controls-" + tabCounter + "'> \ |
|
157 | Message: <input type='text' size='70' id='message-" + tabCounter + "'></input> \ |
157 | Message: <input type='text' size='70' id='message-" + tabCounter + "'></input> \ |
|
158 | <button type='button' id='send-" + tabCounter + "'>Send</button> \ |
158 | <button type='button' id='send-" + tabCounter + "'>Send</button> \ |
|
159 | <input type='hidden' name='firstname' id='firstname-" + tabCounter + "' value='" + first + "'></input> \ |
159 | <input type='hidden' name='firstname' id='firstname-" + tabCounter + "' value='" + first + "'></input> \ |
|
160 | <input type='hidden' name='lastname' id='lastname-" + tabCounter + "' value='" + last + "'></input> \ |
160 | <input type='hidden' name='lastname' id='lastname-" + tabCounter + "' value='" + last + "'></input> \ |
|
161 | </div> \ |
161 | </div> \ |
|
162 | </div> \ |
162 | </div> \ |
|
163 | " + "</p></div>"); |
163 | " + "</p></div>"); |
|
164 | tabs.tabs("refresh"); |
164 | tabs.tabs("refresh"); |
|
165 | |
165 | |
|
166 | // Subscribe to click event to send the instant message. |
166 | // Subscribe to click event to send the instant message. |
|
167 | $("#send-" + tabCounter).on("click", { |
167 | $("#send-" + tabCounter).on("click", { |
|
168 | index: tabCounter |
168 | index: tabCounter |
|
169 | }, sendInstantMessage); |
169 | }, sendInstantMessage); |
|
170 | |
170 | |
|
171 | // Subscribe to pressing enter with the message input box selected. |
171 | // Subscribe to pressing enter with the message input box selected. |
|
172 | $("#message-" + tabCounter).keypress({ |
172 | $("#message-" + tabCounter).keypress({ |
|
173 | index: tabCounter |
173 | index: tabCounter |
|
174 | }, function(e) { |
174 | }, function(e) { |
|
175 | if (e.which == 13) { |
175 | if (e.which == 13) { |
|
176 | sendInstantMessage(e); |
176 | sendInstantMessage(e); |
|
177 | return false; |
177 | return false; |
|
178 | } |
178 | } |
|
179 | }); |
179 | }); |
|
180 | |
180 | |
|
181 | ++tabCounter; |
181 | ++tabCounter; |
|
182 | } |
182 | } |
|
183 | |
183 | |
|
184 | // Close icon: removing the tab on click and delete the conversation. |
184 | // Close icon: removing the tab on click and delete the conversation. |
|
185 | tabs.on("click", "span.ui-icon-close", function() { |
185 | tabs.on("click", "span.ui-icon-close", function() { |
|
186 | var panelId = $(this).closest("li").remove().attr("aria-controls"); |
186 | var panelId = $(this).closest("li").remove().attr("aria-controls"); |
|
187 | var selectedIndex = $(this).closest("li").remove().attr("id"); |
187 | var selectedIndex = $(this).closest("li").remove().attr("id"); |
|
188 | // Pause the conversation retrieval timer. |
188 | // Pause the conversation retrieval timer. |
|
189 | clearTimeout(getConversationsTimeout); |
189 | clearTimeout(getConversationsTimeout); |
|
190 | $.ajax({ |
190 | $.ajax({ |
|
191 | type: 'post', |
191 | type: 'post', |
|
192 | url: "deleteConversation.php", |
192 | url: "deleteConversation.php", |
|
193 | data: { |
193 | data: { |
|
194 | firstname: $("#firstname-" + selectedIndex).val(), |
194 | firstname: $("#firstname-" + selectedIndex).val(), |
|
195 | lastname: $("#lastname-" + selectedIndex).val() |
195 | lastname: $("#lastname-" + selectedIndex).val() |
|
196 | } |
196 | } |
|
197 | }).done(function(data) { |
197 | }).done(function(data) { |
|
198 | $("#" + panelId).remove(); |
198 | $("#" + panelId).remove(); |
|
199 | tabs.tabs("refresh"); |
199 | tabs.tabs("refresh"); |
|
200 | // Resume the conversation retrieval timer. |
200 | // Resume the conversation retrieval timer. |
|
201 | getConversationsTimeout = setTimeout( |
201 | getConversationsTimeout = setTimeout( |
|
202 | getConversations, |
202 | getConversations, |
|
203 | 1000 |
203 | 1000 |
|
204 | ); |
204 | ); |
|
205 | }); |
205 | }); |
|
206 | }); |
206 | }); |
|
207 | |
207 | |
|
208 | // Unbind the message retrieval interval from all other tabs except the active tab. |
208 | // Unbind the message retrieval interval from all other tabs except the active tab. |
|
209 | tabs.on('tabsactivate', function(event, ui) { |
209 | tabs.on('tabsactivate', function(event, ui) { |
|
210 | var selectedIndex = ui.newTab.closest("li").attr("id"); |
210 | var selectedIndex = ui.newTab.closest("li").attr("id"); |
|
211 | $.each(messageIntervals, function(index, value) { |
211 | $.each(messageIntervals, function(index, value) { |
|
212 | if (index != selectedIndex) { |
212 | if (index != selectedIndex) { |
|
213 | clearInterval(value); |
213 | clearInterval(value); |
|
214 | } |
214 | } |
|
215 | }); |
215 | }); |
|
216 | |
216 | |
|
217 | messageIntervals[selectedIndex] = setTimeout( |
217 | messageIntervals[selectedIndex] = setTimeout( |
|
218 | loadInstantMessage, |
218 | loadInstantMessage, |
|
219 | 1000, |
219 | 1000, |
|
220 | selectedIndex |
220 | selectedIndex |
|
221 | ); |
221 | ); |
|
222 | }); |
222 | }); |
|
223 | |
223 | |
|
224 | // Start a timer to load tabs of existing conversations. |
224 | // Start a timer to load tabs of existing conversations. |
|
225 | getConversationsTimeout = setTimeout( |
225 | getConversationsTimeout = setTimeout( |
|
226 | getConversations, |
226 | getConversations, |
|
227 | 1000 |
227 | 1000 |
|
228 | ); |
228 | ); |
|
229 | |
229 | |
|
230 | }); |
230 | }); |
|
231 | </script> |
231 | </script> |
|
232 | </head> |
232 | </head> |
|
233 | |
233 | |
|
234 | <body> |
234 | <body> |
|
235 | |
235 | |
|
236 | <div id="dialog" title="Agent Name"> |
236 | <div id="dialog" title="Agent Name"> |
|
237 | <form> |
237 | <form> |
|
238 | <fieldset class="ui-helper-reset"> |
238 | <fieldset class="ui-helper-reset"> |
|
239 | <label for="firstname">Firstname</label> |
239 | <label for="firstname">Firstname</label> |
|
240 | <input type="text" name="firstname" id="firstname" value="" class="ui-widget-content ui-corner-all"> |
240 | <input type="text" name="firstname" id="firstname" value="" class="ui-widget-content ui-corner-all"> |
|
241 | <label for="lastname">Lastname</label> |
241 | <label for="lastname">Lastname</label> |
|
242 | <input type="text" name="lastname" id="lastname" value="" class="ui-widget-content ui-corner-all"> |
242 | <input type="text" name="lastname" id="lastname" value="" class="ui-widget-content ui-corner-all"> |
|
243 | </fieldset> |
243 | </fieldset> |
|
244 | </form> |
244 | </form> |
|
245 | </div> |
245 | </div> |
|
246 | |
246 | |
|
247 | <p> |
247 | <p> |
|
248 | Your name: <input type="text" size="8" value="Someone" id="name"></input> |
248 | Your name: <input type="text" size="8" value="Someone" id="name"></input> |
|
249 | <button id="add_tab">New Conversation</button> |
249 | <button id="add_tab">New Conversation</button> |
|
250 | </p> |
250 | </p> |
|
251 | |
251 | |
|
252 | <!-- This div holds the tabs that will be created. This structure should not be deleted. --> |
252 | <!-- This div holds the tabs that will be created. This structure should not be deleted. --> |
|
253 | <div id="tabs"> |
253 | <div id="tabs"> |
|
254 | <ul> |
254 | <ul> |
|
255 | |
255 | |
|
256 | </ul> |
256 | </ul> |
|
257 | <div> |
257 | <div> |
|
258 | |
258 | |
|
259 | </div> |
259 | </div> |
|
260 | </div> |
260 | </div> |
|
261 | |
261 | |
|
262 | </body> |
262 | </body> |
|
263 | |
263 | |
|
264 | </html> |
264 | </html> |
|
265 | |
265 | |
|
266 |
|
266 |
|
|
267 | |
267 | |
|
268 | |
268 | |
|
269 | |
269 | |