summaryrefslogtreecommitdiff
path: root/web/index.html
blob: 3655b59e7c2f1361c43a3711b082c8fbecdb83b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="google-signin-scope" content="profile email">
  <meta name="google-signin-client_id" content="962011751339-3gia1j74sqhu1ju40o0scbnfsf39llja.apps.googleusercontent.com">
  <script src="https://apis.google.com/js/platform.js" async defer></script>
  <!--[if lt IE 9]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <link href='https://fonts.googleapis.com/css?family=Noto+Sans' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="style.css">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
  <script>
    var authentication_token = null;

    function addPending(albumName) {
      function makeElement(name) {
        return $('<li>' + name + ' (<a href="javascript:void(0)">edit</a>)</li>');
      }
      var element = makeElement(albumName);
      $(element).find('a').click(function() {
        var input =
          $('<input type="text" value="'+albumName+'" autofocus="true">');
        setTimeout(function() {
          var position = input[0].value.length;
          input[0].setSelectionRange(position, position);
        }, 0);
        $(input).keypress(function(e) {
          if (e.which == 13) {
            $(input).replaceWith($('<i>renaming...</i>'));
            renamePending(albumName, input[0].value);
          }
        });
        $(this).replaceWith(input);
      });
      $('#pending-container').append(element);
    }

    function addPermanentAlbum(albumName) {
      function makeElement(name) {
        return $('<li>' + name + '</li>');
      }
      $('#album-container').append(makeElement(albumName));
    }

    function fetchAlbums() {
      $.ajax({
        url: "/api/albums",
        headers: {'X-Token': authentication_token},
        success: function(data) {
          $('#pending-error').empty();
          $('#pending-container').empty();
          $('#album-container').empty();
          console.log('Got albums:');
          console.log(data);
          data.forEach(function (album) {
            if (album.pending) {
              addPending(album.name, album.pending);
            } else {
              addPermanentAlbum(album.name);
            }
            console.log('Added album ' + album.name);
          });
        },
        error: function(unused, unusedStatus, error) {
          $('#pending-container').empty();
          $('#pending-error').html(
              '<p><b>Error</b> fetching pending albums: ' + error);
        }
      });
    }

    function showError(error) {
      $('#generic-error').html(
          '<p>' + error + '</p>');
    }

    function renamePending(oldName, newName) {
      var request = {
        from: { name: oldName, pending: true },
        to: { name: newName, pending: false }
      };
      $.ajax({
        url: '/api/rename',
        headers: {'X-Token': authentication_token},
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(request),
        success: function(data) {
          if ('Left' in data) {
            showError('Error renaming ' + oldName + ' to ' + newName + ': ' +
                data.Left);
          }
          fetchAlbums();
        }
      });
    }

    function onSignIn(googleUser) {
      // Useful data for your client-side scripts:
      var profile = googleUser.getBasicProfile();
      console.log("ID: " + profile.getId()); // Don't send this directly to your server!
      console.log("Name: " + profile.getName());
      console.log("Image URL: " + profile.getImageUrl());
      console.log("Email: " + profile.getEmail());
      authentication_token = googleUser.getAuthResponse().id_token;
      // The ID token you need to pass to your backend:
      var id_token = googleUser.getAuthResponse().id_token;
      console.log("ID Token: " + id_token);
      $('.g-signin2').remove();
      fetchAlbums();
    };
  </script>
</head>
<body>
  <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
  <span align="center" id="generic-error"></span>

  <h1>Pending Photos</h1>
  <span id="pending-error"></span>
  <ul id="pending-container"></ul>

  <h1>Albums</h1>
  <ul id="album-container"></ul>
</body>
</html>