質問編集履歴

1

jsファイル部分追加

2018/01/01 11:53

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -114,8 +114,6 @@
114
114
 
115
115
  <form id="image-form" action="#">
116
116
 
117
- <input id="mediaCapture" type="file" accept="image/*,capture=camera">
118
-
119
117
  <button id="submitImage" title="Add an image" class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-color--amber-400 mdl-color-text--white">
120
118
 
121
119
  <i class="material-icons">image</i>
@@ -154,15 +152,15 @@
154
152
 
155
153
  var config = {
156
154
 
157
- apiKey: "XXXX",
155
+ apiKey: "XX",
158
-
156
+
159
- authDomain: "XXXX.com",
157
+ authDomain: "XX.com",
160
-
158
+
161
- databaseURL: "https://XXXX.firebaseio.com",
159
+ databaseURL: "https://XXX.firebaseio.com",
162
-
160
+
163
- projectId: "XXYY",
161
+ projectId: "XYZ",
164
-
162
+
165
- storageBucket: "XXXXYYYY.appspot.com",
163
+ storageBucket: "XXYY.appspot.com",
166
164
 
167
165
  messagingSenderId: "1234"
168
166
 
@@ -186,6 +184,380 @@
186
184
 
187
185
 
188
186
 
187
+ main.js
188
+
189
+ ```
190
+
191
+ 'use strict';
192
+
193
+
194
+
195
+ function FriendlyChat() {
196
+
197
+ this.checkSetup();
198
+
199
+
200
+
201
+ this.messageList = document.getElementById('messages');
202
+
203
+ this.messageForm = document.getElementById('message-form');
204
+
205
+ this.messageInput = document.getElementById('message');
206
+
207
+ this.submitButton = document.getElementById('submit');
208
+
209
+ this.submitImageButton = document.getElementById('submitImage');
210
+
211
+ this.imageForm = document.getElementById('image-form');
212
+
213
+ this.userPic = document.getElementById('user-pic');
214
+
215
+ this.userName = document.getElementById('user-name');
216
+
217
+ this.signInButton = document.getElementById('sign-in');
218
+
219
+ this.signOutButton = document.getElementById('sign-out');
220
+
221
+ this.signInSnackbar = document.getElementById('must-signin-snackbar');
222
+
223
+
224
+
225
+ this.messageForm.addEventListener('submit', this.saveMessage.bind(this));
226
+
227
+ this.signOutButton.addEventListener('click', this.signOut.bind(this));
228
+
229
+ this.signInButton.addEventListener('click', this.signIn.bind(this));
230
+
231
+
232
+
233
+ var buttonTogglingHandler = this.toggleButton.bind(this);
234
+
235
+ this.messageInput.addEventListener('keyup', buttonTogglingHandler);
236
+
237
+ this.messageInput.addEventListener('change', buttonTogglingHandler);
238
+
239
+
240
+
241
+ this.submitImageButton.addEventListener('click', function(e) {
242
+
243
+ e.preventDefault();
244
+
245
+ }.bind(this));
246
+
247
+
248
+
249
+ this.initFirebase();
250
+
251
+ }
252
+
253
+
254
+
255
+ FriendlyChat.prototype.initFirebase = function() {
256
+
257
+ this.auth = firebase.auth();
258
+
259
+ this.database = firebase.database();
260
+
261
+ this.store = firebase.storage();
262
+
263
+
264
+
265
+ this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
266
+
267
+ };
268
+
269
+
270
+
271
+ FriendlyChat.prototype.loadMessages = function() {
272
+
273
+ this.messagesRef = this.database.ref('messages');
274
+
275
+ this.messagesRef.off();
276
+
277
+
278
+
279
+ var setMessage = function(data) {
280
+
281
+ var val = data.val();
282
+
283
+ this.displayMessage(data.key, val.name, val.text, val.photoUrl, val.imageUrl);
284
+
285
+ }.bind(this);
286
+
287
+ this.messagesRef.limitToLast(12).on('child_added', setMessage);
288
+
289
+ this.messagesRef.limitToLast(12).on('child_changed', setMessage);
290
+
291
+ };
292
+
293
+
294
+
295
+ FriendlyChat.prototype.saveMessage = function(e) {
296
+
297
+ e.preventDefault();
298
+
299
+ if (this.messageInput.value && this.checkSignedInWithMessage()) {
300
+
301
+ var currentUser = this.auth.currentUser;
302
+
303
+ this.messagesRef.push({
304
+
305
+ name: currentUser.displayName,
306
+
307
+ text: this.messageInput.value,
308
+
309
+ photoUrl: currentUser.photoURL || '/images/profile_placeholder.png'
310
+
311
+ }).then(function() {
312
+
313
+ FriendlyChat.resetMaterialTextfield(this.messageInput);
314
+
315
+ this.toggleButton();
316
+
317
+ }.bind(this)).catch(function(error) {
318
+
319
+ console.error('Error writing new message to Firebase Database', error);
320
+
321
+ });
322
+
323
+ }
324
+
325
+ };
326
+
327
+
328
+
329
+ FriendlyChat.prototype.setImageUrl = function(imageUri, imgElement) {
330
+
331
+ imgElement.src = imageUri;
332
+
333
+ };
334
+
335
+
336
+
337
+ FriendlyChat.prototype.signIn = function() {
338
+
339
+ var provider = new firebase.auth.GoogleAuthProvider();
340
+
341
+ firebase.auth().signInWithPopup(provider).then(function(result) {
342
+
343
+ var token = result.credential.accessToken;
344
+
345
+ var user = result.user;
346
+
347
+ }).catch(function(error) {
348
+
349
+ });
350
+
351
+ };
352
+
353
+
354
+
355
+ FriendlyChat.prototype.signOut = function() {
356
+
357
+ this.auth.signOut();
358
+
359
+ };
360
+
361
+
362
+
363
+ FriendlyChat.prototype.onAuthStateChanged = function(user) {
364
+
365
+ if (user) { // User is signed in!
366
+
367
+ var profilePicUrl = user.photoURL;
368
+
369
+ var userName = user.displayName;
370
+
371
+
372
+
373
+ this.userPic.style.backgroundImage = 'url(' + profilePicUrl + ')';
374
+
375
+ this.userName.textContent = userName;
376
+
377
+
378
+
379
+ this.userName.removeAttribute('hidden');
380
+
381
+ this.userPic.removeAttribute('hidden');
382
+
383
+ this.signOutButton.removeAttribute('hidden');
384
+
385
+
386
+
387
+ this.signInButton.setAttribute('hidden', 'true');
388
+
389
+
390
+
391
+ this.loadMessages();
392
+
393
+
394
+
395
+ this.saveMessagingDeviceToken();
396
+
397
+ } else {
398
+
399
+ this.userName.setAttribute('hidden', 'true');
400
+
401
+ this.userPic.setAttribute('hidden', 'true');
402
+
403
+ this.signOutButton.setAttribute('hidden', 'true');
404
+
405
+
406
+
407
+ this.signInButton.removeAttribute('hidden');
408
+
409
+ }
410
+
411
+ };
412
+
413
+
414
+
415
+ FriendlyChat.prototype.checkSignedInWithMessage = function() {
416
+
417
+ if (this.auth.currentUser) {
418
+
419
+ return true;
420
+
421
+ }
422
+
423
+
424
+
425
+ var data = {
426
+
427
+ message: 'You must sign-in first',
428
+
429
+ timeout: 2000
430
+
431
+ };
432
+
433
+ this.signInSnackbar.MaterialSnackbar.showSnackbar(data);
434
+
435
+ return false;
436
+
437
+ };
438
+
439
+
440
+
441
+ FriendlyChat.resetMaterialTextfield = function(element) {
442
+
443
+ element.value = '';
444
+
445
+ element.parentNode.MaterialTextfield.boundUpdateClassesHandler();
446
+
447
+ };
448
+
449
+
450
+
451
+ FriendlyChat.MESSAGE_TEMPLATE =
452
+
453
+ '<div class="message-container">' +
454
+
455
+ (省略)
456
+
457
+ '</div>';
458
+
459
+
460
+
461
+ FriendlyChat.prototype.displayMessage = function(key, name, text, picUrl, imageUri) {
462
+
463
+ var div = document.getElementById(key);
464
+
465
+ if (!div) {
466
+
467
+ var container = document.createElement('div');
468
+
469
+ container.innerHTML = FriendlyChat.MESSAGE_TEMPLATE;
470
+
471
+ div = container.firstChild;
472
+
473
+ div.setAttribute('id', key);
474
+
475
+ this.messageList.appendChild(div);
476
+
477
+ }
478
+
479
+ if (picUrl) {
480
+
481
+ div.querySelector('.pic').style.backgroundImage = 'url(' + picUrl + ')';
482
+
483
+ }
484
+
485
+ div.querySelector('.name').textContent = name;
486
+
487
+ var messageElement = div.querySelector('.message');
488
+
489
+ if (text) {
490
+
491
+ messageElement.textContent = text;
492
+
493
+ messageElement.innerHTML = messageElement.innerHTML.replace(/\n/g, '<br>');
494
+
495
+ } else if (imageUri) {
496
+
497
+ var image = document.createElement('img');
498
+
499
+ image.addEventListener('load', function() {
500
+
501
+ this.messageList.scrollTop = this.messageList.scrollHeight;
502
+
503
+ }.bind(this));
504
+
505
+ this.setImageUrl(imageUri, image);
506
+
507
+ messageElement.innerHTML = '';
508
+
509
+ messageElement.appendChild(image);
510
+
511
+ }
512
+
513
+ setTimeout(function() {div.classList.add('visible')}, 1);
514
+
515
+ this.messageList.scrollTop = this.messageList.scrollHeight;
516
+
517
+ this.messageInput.focus();
518
+
519
+ };
520
+
521
+
522
+
523
+ FriendlyChat.prototype.toggleButton = function() {
524
+
525
+ if (this.messageInput.value) {
526
+
527
+ this.submitButton.removeAttribute('disabled');
528
+
529
+ } else {
530
+
531
+ this.submitButton.setAttribute('disabled', 'true');
532
+
533
+ }
534
+
535
+ };
536
+
537
+
538
+
539
+ FriendlyChat.prototype.checkSetup = function() {
540
+
541
+ if (!window.firebase || !(firebase.app instanceof Function) || !firebase.app().options) {
542
+
543
+ window.alert('You have not configured and imported the Firebase SDK.');
544
+
545
+ }
546
+
547
+ };
548
+
549
+
550
+
551
+ window.onload = function() {
552
+
553
+ window.friendlyChat = new FriendlyChat();
554
+
555
+ };
556
+
557
+ ```
558
+
559
+
560
+
189
561
 
190
562
 
191
563
  足りない情報等ありましたら、ご指摘お願いします。