-
16 |
Choose a peripheral with a battery service
14 |" + entryText + "
"; 14 | 15 | if (level == "error") console.error(entryText); 16 | else console.info(entryText); 17 | 18 | //$("#log").prepend(entry); 19 | 20 | } 21 | 22 | var isCordova = function() { 23 | console.log("URL ", document.URL); 24 | //var app = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1 && document.URL.indexOf( 'file://' ) === -1; 25 | return document.URL.indexOf('android_asset') !== -1; 26 | } 27 | 28 | var formatDate = function(d) { 29 | var date = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate(); // Returns the year 30 | var time = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); 31 | return date + " " + time; 32 | } 33 | 34 | // ASCII only 35 | function stringToBytes(string) { 36 | var array = new Uint8Array(string.length); 37 | for (var i = 0, l = string.length; i < l; i++) { 38 | array[i] = string.charCodeAt(i); 39 | } 40 | return array.buffer; 41 | } 42 | 43 | // ASCII only 44 | function bytesToString(buffer) { 45 | return String.fromCharCode.apply(null, new Uint8Array(buffer)); 46 | } 47 | 48 | function intToBytes(num) { 49 | num = parseInt(num); 50 | var result = ""; 51 | while (num != 0) { 52 | result = String.fromCharCode(num % 256) + result; 53 | num = parseInt(num / 256); 54 | } 55 | return result; 56 | } 57 | 58 | function stringToHexString(string) { 59 | var str = ""; 60 | for (var i = 0, l = string.length; i < l; i++) { 61 | var str2 = (string.charCodeAt(i)).toString(16); 62 | if (str2.length == 1) 63 | str += "0" + str2; 64 | else 65 | str += str2; 66 | } 67 | return str; 68 | } -------------------------------------------------------------------------------- /app-js/www/js/ux/chat.js: -------------------------------------------------------------------------------- 1 | var fillWithZero = function(i) { 2 | if (i < 10) return "0" + i; 3 | else return i; 4 | } 5 | var drawChat = function(type, text) { 6 | var MAX_LOG_SIZE = 20; 7 | 8 | var date = new Date(); 9 | var time = date.getHours() + ":" + fillWithZero(date.getMinutes()) + ":" + fillWithZero(date.getSeconds()); 10 | var template = $('#chat-template').html(); 11 | Mustache.parse(template); 12 | var rendered = Mustache.render(template, { time: time, text: text, type: type }); 13 | 14 | var $chat = $("#chat"); 15 | var top = $chat.scrollTop; 16 | var maxHeight = $chat.scrollHeight; 17 | var height = $chat.outerHeight(); 18 | 19 | var isAtBottom = top + height >= maxHeight; 20 | 21 | $chat.append(rendered); 22 | var $chatLog = $chat.find('div'); 23 | 24 | if ($chatLog.length > MAX_LOG_SIZE) { 25 | var $sliceChat = $chatLog.slice($chatLog.length - MAX_LOG_SIZE); 26 | $chat.html($sliceChat); 27 | } 28 | 29 | //Scroll to bottom of chat 30 | if (isAtBottom) $chat.scrollTop(maxHeight); 31 | } 32 | 33 | var isDivAtBottom = function(id) { 34 | 35 | var $div = $(id); 36 | var top = $div[0].scrollTop; 37 | var maxHeight = $div[0].scrollHeight; 38 | var height = $div.outerHeight(); 39 | 40 | return top + height >= maxHeight; 41 | } 42 | var scrollToBottom = function(id) { 43 | var $div = $(id); 44 | var maxHeight = $div[0].scrollHeight; 45 | $div.scrollTop(maxHeight); 46 | } 47 | 48 | var lastMessageType = ""; 49 | if (!isCordova()) { 50 | 51 | setInterval(function() { 52 | if (lastMessageType === "") lastMessageType = "info"; 53 | else if (lastMessageType === "receive") lastMessageType = "send"; 54 | else lastMessageType = "receive"; 55 | 56 | drawChat(lastMessageType, "Sample text"); 57 | }, 2000); 58 | 59 | drawChat("info", "Sample text"); 60 | 61 | } --------------------------------------------------------------------------------