example.html 3.61 KB
Newer Older
Tristan Cavelier's avatar
Tristan Cavelier committed
1
<!DOCTYPE html>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<!--
Copyright 2012, Nexedi SA

This program is free software: you can Use, Study, Modify and Redistribute
it under the terms of the GNU General Public License version 3, or (at your
option) any later version, as published by the Free Software Foundation.

You can also Link and Combine this program with other software covered by
the terms of any of the Free Software licenses or any of the Open Source
Initiative approved licenses and Convey the resulting work. Corresponding
source of such a combination shall include the source code for all other
software used.

This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See COPYING file for full licensing terms.
See https://www.nexedi.com/licensing for rationale and options.
-->
Tristan Cavelier's avatar
Tristan Cavelier committed
21 22 23 24 25 26 27 28 29
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jIO Example</title>
</head>
<body>
  <script type="text/javascript">
    <!--
var log = function (o) {
Tristan Cavelier's avatar
Tristan Cavelier committed
30 31 32
  var node = document.createElement ('div');
  node.textContent = o;
  document.getElementById('log').appendChild(node);
Tristan Cavelier's avatar
Tristan Cavelier committed
33 34 35 36 37
};
        //-->
  </script>
  <div id="log">
  </div>
Tristan Cavelier's avatar
Tristan Cavelier committed
38
  <script src="../src/sha256.amd.js"></script>
39
  <script src="../lib/rsvp/rsvp-custom.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
40 41
  <script src="../jio.js"></script>
  <script src="../src/jio.storage/localstorage.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
42 43
  <script type="text/javascript">
    <!--
Tristan Cavelier's avatar
Tristan Cavelier committed
44
var jio_instance = null;
Tristan Cavelier's avatar
Tristan Cavelier committed
45

Tristan Cavelier's avatar
Tristan Cavelier committed
46 47
log('Welcome to the jIO example.html!')
log('-> Create jIO instance');
Tristan Cavelier's avatar
Tristan Cavelier committed
48

Tristan Cavelier's avatar
Tristan Cavelier committed
49
jio_instance = jIO.createJIO({
Tristan Cavelier's avatar
Tristan Cavelier committed
50 51 52
  "type": 'local',
  "username": 'jIOtest',
  "applicationname": 'example'
Tristan Cavelier's avatar
Tristan Cavelier committed
53 54
});

Tristan Cavelier's avatar
Tristan Cavelier committed
55
// Careful! These are asynchronous methods!
56 57 58 59 60
// And they use promises with a custom version of RSVP.js
// For more information, see
// RSVP         https://github.com/tildeio/rsvp.js#rsvpjs--
// Promises A+  http://promisesaplus.com/
// CommonJS     http://wiki.commonjs.org/wiki/Promises
Tristan Cavelier's avatar
Tristan Cavelier committed
61 62 63 64 65 66 67 68
log('-> post "video" document metadata to localStorage');

jio_instance.post({
  "_id": 'video',
  "title": 'My Video Title',
  "codec": 'vorbis',
  "language": 'en',
  "description": 'Image compilation'
Tristan Cavelier's avatar
Tristan Cavelier committed
69
}).then(function () {
Tristan Cavelier's avatar
Tristan Cavelier committed
70 71

  log('-> put "thumbnail" attachment to localStorage');
Tristan Cavelier's avatar
Tristan Cavelier committed
72

Tristan Cavelier's avatar
Tristan Cavelier committed
73
  return jio_instance.putAttachment({
Tristan Cavelier's avatar
Tristan Cavelier committed
74 75
    "_id": "video",
    "_attachment": "thumb.jpg",
Tristan Cavelier's avatar
Tristan Cavelier committed
76
    "_data": "This is the thumbnail content",
Tristan Cavelier's avatar
Tristan Cavelier committed
77
    "_mimetype": 'image/jpeg'
Tristan Cavelier's avatar
Tristan Cavelier committed
78
  });
Tristan Cavelier's avatar
Tristan Cavelier committed
79

Tristan Cavelier's avatar
Tristan Cavelier committed
80 81 82 83 84 85 86 87
}).then(function () {

  log('-> put "video" attachment to localStorage');

  return jio_instance.putAttachment({
    "_id":"video",
    "_attachment": "myvideo.ogg",
    "_data": new Blob(["This is the video content"], {"type": "video/ogg"})
Tristan Cavelier's avatar
Tristan Cavelier committed
88 89
  });

Tristan Cavelier's avatar
Tristan Cavelier committed
90 91 92 93
}).then(function (response) {

  log('Done! Refresh the page to see get and remove command.');

Tristan Cavelier's avatar
Tristan Cavelier committed
94
}, function (err) {
Tristan Cavelier's avatar
Tristan Cavelier committed
95

Tristan Cavelier's avatar
Tristan Cavelier committed
96
  log('Error! ' + err.statusText + ": " + err.reason + ", " + err.message);
Tristan Cavelier's avatar
Tristan Cavelier committed
97 98
  log('-> get "video" document metadata from localStorage');

Tristan Cavelier's avatar
Tristan Cavelier committed
99 100 101
  jio_instance.get({
    "_id": "video"
  }).then(function (response) {
Tristan Cavelier's avatar
Tristan Cavelier committed
102

Tristan Cavelier's avatar
Tristan Cavelier committed
103 104
    log('Title is: "' + response.data.title + '"');
    log('-> get "video" attachment content');
Tristan Cavelier's avatar
Tristan Cavelier committed
105

Tristan Cavelier's avatar
Tristan Cavelier committed
106 107 108
    return jio_instance.getAttachment({
      "_id": "video",
      "_attachment": "myvideo.ogg"
Tristan Cavelier's avatar
Tristan Cavelier committed
109
    });
Tristan Cavelier's avatar
Tristan Cavelier committed
110

Tristan Cavelier's avatar
Tristan Cavelier committed
111 112
  }).then(function (response) {

113
    return jIO.util.readBlobAsBinaryString(response.data);
Tristan Cavelier's avatar
Tristan Cavelier committed
114

115
  }).then(function (event) {
Tristan Cavelier's avatar
Tristan Cavelier committed
116

117
    log('Video content is: ' + event.target.result);
Tristan Cavelier's avatar
Tristan Cavelier committed
118 119 120 121 122 123 124 125
    log('-> remove "video" document from localStorage');

    return jio_instance.remove({"_id":'video'});

  }).then(function (response) {

    log('Done! Refresh the page to see post and putAttachment command.');

Tristan Cavelier's avatar
Tristan Cavelier committed
126
  }, function (err) {
Tristan Cavelier's avatar
Tristan Cavelier committed
127 128 129

    log('Error! ' + err.statusText + ": " + err.reason + ", " + err.message);

Tristan Cavelier's avatar
Tristan Cavelier committed
130 131
  });

Tristan Cavelier's avatar
Tristan Cavelier committed
132 133
});

Tristan Cavelier's avatar
Tristan Cavelier committed
134 135 136 137
        //-->
  </script>
</body>
</html>