corrade-http-templates – Diff between revs 1 and 4
?pathlinks?
Rev 1 | Rev 4 | |||
---|---|---|---|---|
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=1.9"> |
|
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 | getConversationsTimer; |
24 | getConversationsTimer; |
|
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 | }); |
68 | }); |
|
69 | } |
69 | } |
|
70 | |
70 | |
|
71 | // Function to send the message to an agent by passing it back through PHP. |
71 | // Function to send the message to an agent by passing it back through PHP. |
|
72 | function sendInstantMessage(e) { |
72 | function sendInstantMessage(e) { |
|
73 | var index = e.data.index; |
73 | var index = e.data.index; |
|
- | 74 | // If the parameters are empty, then do not send anything to the PHP script. |
||
- | 75 | if($.trim($("#firstname-" + index).val()) == '' || |
||
- | 76 | $.trim($("#lastname-" + index).val()) == '' || |
||
- | 77 | $.trim($("#message-" + index).val()) == '') |
||
- | 78 | return; |
||
74 | $("#controls-" + index).animate({ |
79 | $("#controls-" + index).animate({ |
|
75 | opacity: 'hide' |
80 | opacity: 'hide' |
|
76 | }, 'slow'); |
81 | }, 'slow'); |
|
77 | $.ajax({ |
82 | $.ajax({ |
|
78 | type: 'post', |
83 | type: 'post', |
|
79 | url: "sendInstantMessage.php", |
84 | url: "sendInstantMessage.php", |
|
80 | data: { |
85 | data: { |
|
81 | name: $("#name").val(), |
86 | name: $("#name").val(), |
|
82 | firstname: $("#firstname-" + index).val(), |
87 | firstname: $("#firstname-" + index).val(), |
|
83 | lastname: $("#lastname-" + index).val(), |
88 | lastname: $("#lastname-" + index).val(), |
|
84 | message: $("#message-" + index).val() |
89 | message: $("#message-" + index).val() |
|
85 | } |
90 | } |
|
86 | }).done(function(data) { |
91 | }).done(function(data) { |
|
87 | if(data) |
92 | if(data) |
|
88 | alert(data); |
93 | alert(data); |
|
89 | $("#message-" + index).val(""); |
94 | $("#message-" + index).val(""); |
|
90 | $("#controls-" + index).animate({ |
95 | $("#controls-" + index).animate({ |
|
91 | opacity: 'show' |
96 | opacity: 'show' |
|
92 | }, 'slow'); |
97 | }, 'slow'); |
|
93 | }); |
98 | }); |
|
94 | } |
99 | } |
|
95 | |
100 | |
|
96 | // Loads all the stored instant messages from the log file named after the avatar. |
101 | // Loads all the stored instant messages from the log file named after the avatar. |
|
97 | function loadInstantMessage(index) { |
102 | function loadInstantMessage(index) { |
|
98 | $.get("messages/" + $("#firstname-" + index).val() + " " + $("#lastname-" + index).val() + ".log" + "?t=" + Math.random(), function(data) { |
103 | $.get("messages/" + $("#firstname-" + index).val() + " " + $("#lastname-" + index).val() + ".log" + "?t=" + Math.random(), function(data) { |
|
99 | $("#chat-" + index).html(data); |
104 | $("#chat-" + index).html(data); |
|
100 | }); |
105 | }); |
|
101 | |
106 | |
|
102 | $("#chat-" + index).animate({ |
107 | $("#chat-" + index).animate({ |
|
103 | scrollTop: $("#chat-" + index)[0].scrollHeight - $("#chat-" + index).height() |
108 | scrollTop: $("#chat-" + index)[0].scrollHeight - $("#chat-" + index).height() |
|
104 | }, 1000); |
109 | }, 1000); |
|
105 | } |
110 | } |
|
106 | |
111 | |
|
107 | // This function checks whether a conversation / tab already exists. |
112 | // This function checks whether a conversation / tab already exists. |
|
108 | function conversationExists(firstName, lastName) { |
113 | function conversationExists(firstName, lastName) { |
|
109 | var exists = false; |
114 | var exists = false; |
|
110 | for (var i = tabCounter; i >= 0; --i) { |
115 | for (var i = tabCounter; i >= 0; --i) { |
|
111 | if ($("#firstname-" + i).length && |
116 | if ($("#firstname-" + i).length && |
|
112 | $("#firstname-" + i).val().toUpperCase() == firstName.toUpperCase() && |
117 | $("#firstname-" + i).val().toUpperCase() == firstName.toUpperCase() && |
|
113 | $("#lastname-" + i).length && |
118 | $("#lastname-" + i).length && |
|
114 | $("#lastname-" + i).val().toUpperCase() == lastName.toUpperCase()) { |
119 | $("#lastname-" + i).val().toUpperCase() == lastName.toUpperCase()) { |
|
115 | exists = true; |
120 | exists = true; |
|
116 | break; |
121 | break; |
|
117 | } |
122 | } |
|
118 | } |
123 | } |
|
119 | return exists; |
124 | return exists; |
|
120 | } |
125 | } |
|
121 | |
126 | |
|
122 | // Adds a new tab in case a conversation with that agent does not exist. |
127 | // Adds a new tab in case a conversation with that agent does not exist. |
|
123 | function addTab(first, last) { |
128 | function addTab(first, last) { |
|
124 | var first = typeof first !== 'undefined' ? first : firstName.val(); |
129 | var first = typeof first !== 'undefined' ? first : firstName.val(); |
|
125 | var last = typeof last !== 'undefined' ? last : lastName.val(); |
130 | var last = typeof last !== 'undefined' ? last : lastName.val(); |
|
126 | |
131 | |
|
127 | // A conversation with that agent exists, so just do nothing. |
132 | // A conversation with that agent exists, so just do nothing. |
|
128 | if (conversationExists(first, last)) |
133 | if (conversationExists(first, last)) |
|
129 | return; |
134 | return; |
|
130 | |
135 | |
|
131 | // Normalize avatar names by capitalizing the first letter of the firstname and the last name. |
136 | // Normalize avatar names by capitalizing the first letter of the firstname and the last name. |
|
132 | first = first.charAt(0).toUpperCase() + first.substr(1); |
137 | first = first.charAt(0).toUpperCase() + first.substr(1); |
|
133 | last = last.charAt(0).toUpperCase() + last.substr(1); |
138 | last = last.charAt(0).toUpperCase() + last.substr(1); |
|
134 | |
139 | |
|
135 | // Build the discussion form and add the tab. |
140 | // Build the discussion form and add the tab. |
|
136 | var label = first + " " + last, |
141 | var label = first + " " + last, |
|
137 | id = "tabs-" + tabCounter, |
142 | id = "tabs-" + tabCounter, |
|
138 | li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{id\}/g, tabCounter).replace(/#\{label\}/g, label)); |
143 | li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{id\}/g, tabCounter).replace(/#\{label\}/g, label)); |
|
139 | |
144 | |
|
140 | tabs.find(".ui-tabs-nav").append(li); |
145 | tabs.find(".ui-tabs-nav").append(li); |
|
141 | tabs.append("<div id='" + id + "'><p>" + " \ |
146 | tabs.append("<div id='" + id + "'><p>" + " \ |
|
142 | <div id='container-" + tabCounter + "'> \ |
147 | <div id='container-" + tabCounter + "'> \ |
|
143 | <textarea id='chat-" + tabCounter + "' rows='12'></textarea><br/> \ |
148 | <textarea id='chat-" + tabCounter + "' rows='12'></textarea><br/> \ |
|
144 | <div id='controls-" + tabCounter + "'> \ |
149 | <div id='controls-" + tabCounter + "'> \ |
|
145 | Message: <input type='text' size='70' id='message-" + tabCounter + "'></input> \ |
150 | Message: <input type='text' size='70' id='message-" + tabCounter + "'></input> \ |
|
146 | <button type='button' id='send-" + tabCounter + "'>Send</button> \ |
151 | <button type='button' id='send-" + tabCounter + "'>Send</button> \ |
|
147 | <input type='hidden' name='firstname' id='firstname-" + tabCounter + "' value='" + first + "'></input> \ |
152 | <input type='hidden' name='firstname' id='firstname-" + tabCounter + "' value='" + first + "'></input> \ |
|
148 | <input type='hidden' name='lastname' id='lastname-" + tabCounter + "' value='" + last + "'></input> \ |
153 | <input type='hidden' name='lastname' id='lastname-" + tabCounter + "' value='" + last + "'></input> \ |
|
149 | </div> \ |
154 | </div> \ |
|
150 | </div> \ |
155 | </div> \ |
|
151 | " + "</p></div>"); |
156 | " + "</p></div>"); |
|
152 | tabs.tabs("refresh"); |
157 | tabs.tabs("refresh"); |
|
153 | |
158 | |
|
154 | // Subscribe to click event to send the instant message. |
159 | // Subscribe to click event to send the instant message. |
|
155 | $("#send-" + tabCounter).on("click", { |
160 | $("#send-" + tabCounter).on("click", { |
|
156 | index: tabCounter |
161 | index: tabCounter |
|
157 | }, sendInstantMessage); |
162 | }, sendInstantMessage); |
|
158 | |
163 | |
|
159 | // Subscribe to pressing enter with the message input box selected. |
164 | // Subscribe to pressing enter with the message input box selected. |
|
160 | $("#message-" + tabCounter).keypress({ |
165 | $("#message-" + tabCounter).keypress({ |
|
161 | index: tabCounter |
166 | index: tabCounter |
|
162 | }, function(e) { |
167 | }, function(e) { |
|
163 | if (e.which == 13) { |
168 | if (e.which == 13) { |
|
164 | sendInstantMessage(e); |
169 | sendInstantMessage(e); |
|
165 | return false; |
170 | return false; |
|
166 | } |
171 | } |
|
167 | }); |
172 | }); |
|
168 | |
173 | |
|
169 | ++tabCounter; |
174 | ++tabCounter; |
|
170 | } |
175 | } |
|
171 | |
176 | |
|
172 | // Close icon: removing the tab on click and delete the conversation. |
177 | // Close icon: removing the tab on click and delete the conversation. |
|
173 | tabs.on("click", "span.ui-icon-close", function() { |
178 | tabs.on("click", "span.ui-icon-close", function() { |
|
174 | var panelId = $(this).closest("li").remove().attr("aria-controls"); |
179 | var panelId = $(this).closest("li").remove().attr("aria-controls"); |
|
175 | var selectedIndex = $(this).closest("li").remove().attr("id"); |
180 | var selectedIndex = $(this).closest("li").remove().attr("id"); |
|
176 | // Pause the conversation retrieval timer. |
181 | // Pause the conversation retrieval timer. |
|
177 | clearInterval(getConversationsTimer); |
182 | clearInterval(getConversationsTimer); |
|
178 | $.ajax({ |
183 | $.ajax({ |
|
179 | type: 'post', |
184 | type: 'post', |
|
180 | url: "deleteConversation.php", |
185 | url: "deleteConversation.php", |
|
181 | data: { |
186 | data: { |
|
182 | firstname: $("#firstname-" + selectedIndex).val(), |
187 | firstname: $("#firstname-" + selectedIndex).val(), |
|
183 | lastname: $("#lastname-" + selectedIndex).val() |
188 | lastname: $("#lastname-" + selectedIndex).val() |
|
184 | } |
189 | } |
|
185 | }).done(function(data) { |
190 | }).done(function(data) { |
|
186 | $("#" + panelId).remove(); |
191 | $("#" + panelId).remove(); |
|
187 | tabs.tabs("refresh"); |
192 | tabs.tabs("refresh"); |
|
188 | // Resume the conversation retrieval timer. |
193 | // Resume the conversation retrieval timer. |
|
189 | getConversationsTimer = setInterval( |
194 | getConversationsTimer = setInterval( |
|
190 | getConversations, |
195 | getConversations, |
|
191 | 1000 |
196 | 1000 |
|
192 | ); |
197 | ); |
|
193 | }); |
198 | }); |
|
194 | }); |
199 | }); |
|
195 | |
200 | |
|
196 | // Unbind the message retrieval interval from all other tabs except the active tab. |
201 | // Unbind the message retrieval interval from all other tabs except the active tab. |
|
197 | tabs.on('tabsactivate', function(event, ui) { |
202 | tabs.on('tabsactivate', function(event, ui) { |
|
198 | var selectedIndex = ui.newTab.closest("li").attr("id"); |
203 | var selectedIndex = ui.newTab.closest("li").attr("id"); |
|
199 | $.each(messageIntervals, function(index, value) { |
204 | $.each(messageIntervals, function(index, value) { |
|
200 | if (index != selectedIndex) { |
205 | if (index != selectedIndex) { |
|
201 | clearInterval(value); |
206 | clearInterval(value); |
|
202 | } |
207 | } |
|
203 | }); |
208 | }); |
|
204 | |
209 | |
|
205 | messageIntervals[selectedIndex] = setInterval( |
210 | messageIntervals[selectedIndex] = setInterval( |
|
206 | loadInstantMessage, |
211 | loadInstantMessage, |
|
207 | 1000, |
212 | 1000, |
|
208 | selectedIndex |
213 | selectedIndex |
|
209 | ); |
214 | ); |
|
210 | }); |
215 | }); |
|
211 | |
216 | |
|
212 | // Start a timer to load tabs of existing conversations. |
217 | // Start a timer to load tabs of existing conversations. |
|
213 | getConversationsTimer = setInterval( |
218 | getConversationsTimer = setInterval( |
|
214 | getConversations, |
219 | getConversations, |
|
215 | 1000 |
220 | 1000 |
|
216 | ); |
221 | ); |
|
217 | |
222 | |
|
218 | }); |
223 | }); |
|
219 | </script> |
224 | </script> |
|
220 | </head> |
225 | </head> |
|
221 | |
226 | |
|
222 | <body> |
227 | <body> |
|
223 | |
228 | |
|
224 | <div id="dialog" title="Agent Name"> |
229 | <div id="dialog" title="Agent Name"> |
|
225 | <form> |
230 | <form> |
|
226 | <fieldset class="ui-helper-reset"> |
231 | <fieldset class="ui-helper-reset"> |
|
227 | <label for="firstname">Firstname</label> |
232 | <label for="firstname">Firstname</label> |
|
228 | <input type="text" name="firstname" id="firstname" value="" class="ui-widget-content ui-corner-all"> |
233 | <input type="text" name="firstname" id="firstname" value="" class="ui-widget-content ui-corner-all"> |
|
229 | <label for="lastname">Lastname</label> |
234 | <label for="lastname">Lastname</label> |
|
230 | <input type="text" name="lastname" id="lastname" value="" class="ui-widget-content ui-corner-all"> |
235 | <input type="text" name="lastname" id="lastname" value="" class="ui-widget-content ui-corner-all"> |
|
231 | </fieldset> |
236 | </fieldset> |
|
232 | </form> |
237 | </form> |
|
233 | </div> |
238 | </div> |
|
234 | |
239 | |
|
235 | <p> |
240 | <p> |
|
236 | Your name: <input type="text" size="8" value="Someone" id="name"></input> |
241 | Your name: <input type="text" size="8" value="Someone" id="name"></input> |
|
237 | <button id="add_tab">New Conversation</button> |
242 | <button id="add_tab">New Conversation</button> |
|
238 | </p> |
243 | </p> |
|
239 | |
244 | |
|
240 | <!-- This div holds the tabs that will be created. This structure should not be deleted. --> |
245 | <!-- This div holds the tabs that will be created. This structure should not be deleted. --> |
|
241 | <div id="tabs"> |
246 | <div id="tabs"> |
|
242 | <ul> |
247 | <ul> |
|
243 | |
248 | |
|
244 | </ul> |
249 | </ul> |
|
245 | <div> |
250 | <div> |
|
246 | |
251 | |
|
247 | </div> |
252 | </div> |
|
248 | </div> |
253 | </div> |
|
249 | |
254 | |
|
250 | </body> |
255 | </body> |
|
251 | |
256 | |
|
252 | </html> |
257 | </html> |
|
253 | |
258 | |
|
254 |
|
259 |
|
|
255 | |
260 | |
|
256 | |
261 | |
|
257 | |
262 | |