corrade-http-templates – Blame information for rev 1

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