|
1 | 1 | // jshint browser:true, jquery: true |
2 | | -import RuntimeUA from '../src/runtimeUA'; |
3 | 2 |
|
| 3 | +import {addLoader, removeLoader, documentReady, errorMessage} from './support'; |
| 4 | + |
| 5 | +import RuntimeUA from '../src/runtimeUA'; |
4 | 6 | import SandboxFactory from '../resources/sandboxes/SandboxFactory'; |
5 | 7 |
|
6 | 8 | let sandboxFactory = new SandboxFactory(); |
7 | | - |
| 9 | +let avatar = 'https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg'; |
8 | 10 | let runtime = new RuntimeUA(sandboxFactory); |
9 | | -window.runtime = runtime; |
10 | 11 |
|
11 | | -let hypertiesList = ['http://ua.pt/HelloHyperty', 'http://ua.pt/WorldHyperty']; |
12 | | - |
13 | | -function errorMessage(reason) { |
14 | | - console.log(reason); |
| 12 | +// Check if the document is ready |
| 13 | +if (document.readyState === 'complete') { |
| 14 | + documentReady(); |
| 15 | +} else { |
| 16 | + window.addEventListener('onload', documentReady, false); |
| 17 | + document.addEventListener('DOMContentLoaded', documentReady, false); |
15 | 18 | } |
16 | 19 |
|
17 | 20 | let loginBtn = document.querySelector('.login'); |
18 | | -loginBtn.addEventListener('click', function() { |
19 | | - runtime.identityModule.loginWithRP().then(userLoged).catch(errorMessage); |
| 21 | +loginBtn.addEventListener('click', function(e) { |
| 22 | + |
| 23 | + let loginPanel = document.querySelector('.login-panel'); |
| 24 | + let content = loginPanel.querySelector('.card-action'); |
| 25 | + addLoader(content); |
| 26 | + |
| 27 | + runtime.identityModule.loginWithRP().then(function(result) { |
| 28 | + removeLoader(content); |
| 29 | + userLoged(result); |
| 30 | + }).catch(errorMessage); |
| 31 | + |
| 32 | + let btn = $(e.currentTarget); |
| 33 | + btn.addClass('hide'); |
| 34 | + |
20 | 35 | }); |
21 | 36 |
|
22 | 37 | function userLoged(result) { |
23 | | - console.log('result: ', result); |
24 | 38 |
|
25 | | - let hypertyHolder = document.querySelector('.hyperties'); |
26 | | - hypertyHolder.className = hypertyHolder.className.replace('hide', ''); |
| 39 | + let hypertyHolder = $('.hyperties'); |
| 40 | + hypertyHolder.removeClass('hide'); |
| 41 | + |
| 42 | + let cardHolder = $('.card-content'); |
| 43 | + let html = '<div class="row"><div class="col s12"><span class="card-title">Logged</span></div><div class="col s2"><img alt="" class="circle responsive-img" src=' + result.picture + ' ></div><div class="col s8"><p><b>id:</b> ' + result.id + '</p><p><b>email:</b> ' + result.email + '</p><p><b>name:</b> ' + result.name + '</p><p><b>locale:</b> ' + result.locale + '</p></div>'; |
| 44 | + |
| 45 | + cardHolder.html(html); |
| 46 | + |
| 47 | + console.log(result); |
| 48 | + |
| 49 | + let hyperty = 'http://ua.pt/HelloHyperty'; |
| 50 | + |
| 51 | + // Load First Hyperty |
| 52 | + runtime.loadHyperty(hyperty).then(hypertyDeployed).catch(function(reason) { |
| 53 | + errorMessage(reason); |
| 54 | + }); |
27 | 55 |
|
28 | | - loadHyperties(); |
29 | 56 | } |
30 | 57 |
|
31 | | -// let hypertiesList = ['http://localhost:4000/HelloHyperty', 'http://localhost:4000/WorldHyperty']; |
| 58 | +function hypertyDeployed(result) { |
32 | 59 |
|
33 | | -function errorMessage(reason) { |
34 | | - console.log(reason); |
| 60 | + let loginPanel = $('.login-panel'); |
| 61 | + let cardAction = loginPanel.find('.card-action'); |
| 62 | + let hypertyInfo = '<span class="white-text"><p><b>hypertyURL:</b> ' + result.runtimeHypertyURL + '</br><b>status:</b> ' + result.status + '</p></span>'; |
| 63 | + |
| 64 | + loginPanel.attr('data-url', result.runtimeHypertyURL); |
| 65 | + cardAction.append(hypertyInfo); |
| 66 | + |
| 67 | + // Prepare to discover email: |
| 68 | + discoverEmail(); |
| 69 | + |
| 70 | + // Prepare the chat |
| 71 | + let messageChat = $('.hyperty-chat'); |
| 72 | + messageChat.removeClass('hide'); |
| 73 | + |
| 74 | + runtime.messageBus.addListener(result.runtimeHypertyURL, newMessageRecived); |
35 | 75 | } |
36 | 76 |
|
37 | | -function deployedHyperties(hyperty, result) { |
| 77 | +function discoverEmail() { |
38 | 78 |
|
39 | | - let hypertyName = hyperty.substr(hyperty.lastIndexOf('/') + 1); |
40 | | - let hypertyEl = document.querySelector('.' + hypertyName); |
| 79 | + let section = $('.discover'); |
| 80 | + let searchForm = section.find('.form'); |
| 81 | + let inputField = searchForm.find('.friend-email'); |
41 | 82 |
|
42 | | - hypertyEl.querySelector('.status').innerHTML = result.status; |
43 | | - hypertyEl.querySelector('.name').innerHTML = hypertyName; |
44 | | - hypertyEl.querySelector('.runtime-hyperty-url').innerHTML = result.runtimeHypertyURL; |
45 | | - hypertyEl.querySelector('.form').setAttribute('data-url', result.runtimeHypertyURL); |
46 | | - hypertyEl.querySelector('.send').addEventListener('click', function(e) { |
| 83 | + section.removeClass('hide'); |
47 | 84 |
|
48 | | - let target = e.target; |
49 | | - let form = target.parentElement; |
50 | | - let fromHyperty = form.getAttribute('data-url'); |
51 | | - let toHyperty = form.querySelector('.toHyperty').value; |
52 | | - let messageHypert = form.querySelector('.messageHyperty').value; |
| 85 | + searchForm.on('submit', function(event) { |
| 86 | + event.preventDefault(); |
53 | 87 |
|
54 | | - if (fromHyperty && toHyperty && messageHypert) { |
55 | | - sendMessage(fromHyperty, toHyperty, messageHypert); |
56 | | - } |
| 88 | + let collection = section.find('.collection'); |
| 89 | + let collectionItem = '<li class="collection-item"><div class="preloader-wrapper small active"><div class="spinner-layer spinner-blue-only"><div class="circle-clipper left"><div class="circle"></div></div><div class="gap-patch"><div class="circle"></div></div><div class="circle-clipper right"><div class="circle"></div></div></div></div></li>'; |
57 | 90 |
|
58 | | - form.reset(); |
| 91 | + collection.removeClass('hide'); |
| 92 | + collection.addClass('center-align'); |
| 93 | + collection.html(collectionItem); |
| 94 | + |
| 95 | + let email = inputField.val(); |
| 96 | + console.log(email); |
| 97 | + runtime.registry.getUserHyperty(email).then(emailDiscovered).catch(emailDiscoveredError); |
59 | 98 |
|
60 | | - e.preventDefault(); |
61 | 99 | }); |
| 100 | +} |
62 | 101 |
|
63 | | - runtime.messageBus.addListener(result.runtimeHypertyURL, newMessageRecived); |
| 102 | +function emailDiscovered(result) { |
| 103 | + // Email Discovered: Object {id: "openidtest10@gmail.com", descriptor: "http://ua.pt/HelloHyperty", hypertyURL: "hyperty://ua.pt/27d080c8-22ef-445f-9f9a-f2c750fc6d5a"} |
| 104 | + |
| 105 | + console.log('Email Discovered: ', result); |
| 106 | + |
| 107 | + let section = $('.discover'); |
| 108 | + let collection = section.find('.collection'); |
| 109 | + let collectionItem = '<li class="collection-item avatar"><img src="' + avatar + '" alt="" class="circle"><span class="title">' + result.id + '</span><p>' + result.descriptor + '<br>' + result.hypertyURL + '</p><a href="#!" class="message-btn"><i class="material-icons left">message</i>Send Message</a></li>'; |
| 110 | + |
| 111 | + collection.empty(); |
| 112 | + collection.removeClass('center-align'); |
| 113 | + collection.append(collectionItem); |
| 114 | + |
| 115 | + let messageChatBtn = collection.find('.message-btn'); |
| 116 | + messageChatBtn.on('click', function(event) { |
| 117 | + event.preventDefault(); |
| 118 | + openChat(result); |
| 119 | + }); |
64 | 120 |
|
65 | 121 | } |
66 | 122 |
|
67 | | -function newMessageRecived(msg) { |
| 123 | +function emailDiscoveredError(result) { |
68 | 124 |
|
69 | | - let fromHyperty = msg.from; |
70 | | - let toHyperty = msg.to; |
| 125 | + // result = { |
| 126 | + // id: 'openidtest10@gmail.com', |
| 127 | + // descriptor: 'http://ua.pt/HelloHyperty', |
| 128 | + // hypertyURL: 'hyperty://ua.pt/27d080c8-22ef-445f-9f9a-f2c750fc6d5a' |
| 129 | + // }; |
| 130 | + // |
| 131 | + // emailDiscovered(result); |
71 | 132 |
|
72 | | - let elTo = document.querySelector('form[data-url="' + toHyperty + '"]'); |
73 | | - let listTo = elTo.parentElement.querySelector('.list'); |
| 133 | + console.error('Email Discovered Error: ', result); |
74 | 134 |
|
75 | | - let itemTo = document.createElement('li'); |
76 | | - itemTo.setAttribute('class', 'collection-item avatar right-align'); |
77 | | - itemTo.innerHTML = '<i class="material-icons circle green">call_received</i><label class="name title">' + fromHyperty + '</label><p class="message">' + msg.body.value.replace(/\n/g, '<br>') + '</p>'; |
| 135 | + let section = $('.discover'); |
| 136 | + let collection = section.find('.collection'); |
78 | 137 |
|
79 | | - listTo.appendChild(itemTo); |
| 138 | + let collectionItem = '<li class="collection-item orange lighten-3"><i class="material-icons left circle">error_outline</i>' + result + '</li>'; |
80 | 139 |
|
| 140 | + collection.empty(); |
| 141 | + collection.removeClass('center-align'); |
| 142 | + collection.removeClass('hide'); |
| 143 | + collection.append(collectionItem); |
81 | 144 | } |
82 | 145 |
|
83 | | -function sendMessage(from, to, message) { |
| 146 | +function openChat(result) { |
84 | 147 |
|
85 | | - let messageObject = { |
86 | | - to: to, |
87 | | - from: from, |
88 | | - type: 'message', |
89 | | - body:{ |
90 | | - value: message |
| 148 | + let messagesChat = $('.messages'); |
| 149 | + let messageForm = messagesChat.find('.form'); |
| 150 | + let loginPanel = $('.login-panel'); |
| 151 | + let fromUser = loginPanel.attr('data-url'); |
| 152 | + let toUserEl = messagesChat.find('.runtime-hyperty-url'); |
| 153 | + let toUser = result.hypertyURL; |
| 154 | + |
| 155 | + toUserEl.html(toUser); |
| 156 | + |
| 157 | + messageForm.on('submit', function(e) { |
| 158 | + |
| 159 | + let messageText = messagesChat.find('.message-text').val(); |
| 160 | + |
| 161 | + if (messageText) { |
| 162 | + sendMessage(fromUser, toUser, messageText); |
91 | 163 | } |
92 | | - }; |
93 | 164 |
|
94 | | - let form = document.querySelector('form[data-url="' + from + '"]'); |
95 | | - if (form) { |
96 | | - let listFrom = form.parentElement.querySelector('.list'); |
97 | | - let itemFrom = document.createElement('li'); |
98 | | - itemFrom.setAttribute('class', 'collection-item avatar'); |
99 | | - itemFrom.innerHTML = '<i class="material-icons circle yellow">call_made</i><label class="name title">' + from + '</label><p class="message">' + messageObject.body.value.replace(/\n/g, '<br>') + '</p>'; |
| 165 | + messageForm[0].reset(); |
100 | 166 |
|
101 | | - listFrom.appendChild(itemFrom); |
102 | | - } |
| 167 | + e.preventDefault(); |
| 168 | + }); |
| 169 | + |
| 170 | + messagesChat.removeClass('hide'); |
103 | 171 |
|
104 | | - runtime.messageBus.postMessage(messageObject); |
105 | 172 | } |
106 | 173 |
|
107 | | -function loadHyperties() { |
| 174 | +function newMessageRecived(msg) { |
108 | 175 |
|
109 | | - let time = 1; |
| 176 | + // Object {to: "hyperty://ua.pt/71552726-ae61-411a-bab0-41843b26b56f", from: "hyperty://ua.pt/586f5f0a-aa98-4d23-b864-a6efd3ccdd74", type: "message", body: Object, id: 2} |
| 177 | + processMessage(msg, 'in'); |
110 | 178 |
|
111 | | - hypertiesList.forEach(function(hyperty) { |
| 179 | +} |
112 | 180 |
|
113 | | - setTimeout(function() { |
| 181 | +function processMessage(msg, type) { |
114 | 182 |
|
115 | | - // Load First Hyperty |
116 | | - runtime.loadHyperty(hyperty).then(function(result) { |
117 | | - deployedHyperties(hyperty, result); |
118 | | - }).catch(function(reason) { |
119 | | - errorMessage(reason); |
120 | | - }); |
| 183 | + let side = 'right-align'; |
| 184 | + if (type === 'out') { |
| 185 | + side = 'left-align'; |
| 186 | + } |
121 | 187 |
|
122 | | - // This load stub is not necessary, because for domain ua.pt the loadHyperty |
123 | | - // create a stub for the same domain; |
124 | | - // r.loadStub('ua.pt').then(function(result) { |
125 | | - // console.log(result); |
126 | | - // }).catch(function(reason) { |
127 | | - // console.log(reason); |
128 | | - // }); |
| 188 | + let messageCollection = $('.hyperty-chat .collection'); |
| 189 | + let messageItem = '<li class="collection-item avatar ' + side + '"><img src="' + avatar + '" alt="" class="circle"><span class="title">' + msg.from + '</span><span class="text left">' + msg.body.value.replace(/\n/g, '<br>') + '</span></li>'; |
129 | 190 |
|
130 | | - }, (1000 * time)); |
| 191 | + messageCollection.append(messageItem); |
| 192 | +} |
131 | 193 |
|
132 | | - time++; |
| 194 | +function sendMessage(from, to, message) { |
133 | 195 |
|
134 | | - }); |
| 196 | + let msg = { |
| 197 | + to: to, |
| 198 | + from: from, |
| 199 | + type: 'message', |
| 200 | + body:{ |
| 201 | + value: message |
| 202 | + } |
| 203 | + }; |
135 | 204 |
|
| 205 | + processMessage(msg, 'out'); |
| 206 | + runtime.messageBus.postMessage(msg); |
136 | 207 | } |
137 | | - |
138 | | -// END Testing code; |
|
0 commit comments