jio_dashboard.html 11.5 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 2013, 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
<html lang="en">
<head>
  <meta charset="utf-8" />
24
  <title>jIO Dashboard</title>
Tristan Cavelier's avatar
Tristan Cavelier committed
25 26
</head>
<body>
27
  <table border="1" style="width: 100%;">
28 29 30 31
    <tr>
      <th style="text-align: center;" id="script_injection_space">
      </th>
    </tr>
32
    <tr style="font-style:italic;">
33
      <th>Storage Description</th>
34 35 36
    </tr>
    <tr>
      <th style="width:100%;">
37 38
        <textarea id="storagedescription" rows="10"
                  style="width:98%;"></textarea>
39 40 41
      </th>
    </tr>
    <tr>
42 43 44 45
      <td style="text-align: center;">
        <button onclick="fillMemoryDescription()">Memory</button>
        <button onclick="fillLocalDescription()">Local</button>
        <button onclick="fillDavDescription()">WebDAV</button>
46
        <button onclick="fillDavBasicDescription()">WebDAV Basic</button>
47
        <button onclick="fillERP5Description()">ERP5</button>
48 49
        <button onclick="fillCustomDescription()">Custom</button>
        <br /><button onclick="fillLastDescription()">Last</button>
50 51
        <button onclick="loadDescription()">Load</button>
        <button onclick="saveDescription()">Save</button>
52 53 54 55
      </td>
    </tr>
    <tr>
      <th><button onclick="createJIO()">Create JIO</button></th>
56 57
    </tr>
  </table>
Tristan Cavelier's avatar
Tristan Cavelier committed
58
  <br />
59 60
  <table border="1" style="width: 100%;">
    <tr>
61
      <td colspan="1" style="width: 50%;">
62
        <label for="metadata">Metadata or Parameters:</label>
63 64
        <textarea id="metadata" rows="3" style="width: 98%;">{}</textarea>
      </td>
65
      <td colspan="1" style="text-align: center;">
66
        Options:<br />
67
        <textarea id="options" rows="3" style="width: 98%;">{}</textarea>
68 69 70
      </td>
    </tr>
    <tr>
71
      <td colspan="2" style="text-align: center;">
72 73 74 75 76 77 78 79 80 81
        <button onclick="command('post')">post</button>
        <button onclick="command('put')">put</button>
        <button onclick="command('get')">get</button>
        <button onclick="command('remove')">remove</button>
        - <button onclick="command('putAttachment')">putAttachment</button>
        <button onclick="command('getAttachment')">getAttachment</button>
        <button onclick="command('removeAttachment')">removeAttachment</button>
        - <button onclick="command('allDocs')">allDocs</button>
        - <button onclick="command('check')">check</button>
        <button onclick="command('repair')">repair</button>
82 83 84 85
      </td>
    </tr>
    <tr>
      <td colspan="2" style="text-align: center;">
86 87 88 89
        <label for="times">Times</label>
        <input id="times" type="text" value="1" />
        <input id="times-lock" type="checkbox" checked="1" />
        <label for="times-lock">lock</label>
90 91
      </td>
    </tr>
92 93 94
  </table>
  <br />
  <div style="text-align: center;">
95 96 97 98
    Useful functions:
    <button onclick="scriptLogLocalStorage()">log localStorage</button>
    <button onclick="localStorage.clear()">clear localStorage</button>
    <button onclick="scriptRemoveAllDocs()">removeAllDocs</button>
99
    <button onclick="scriptRepairAllDocs()">repairAllDocs</button>
100
  </div>
Tristan Cavelier's avatar
Tristan Cavelier committed
101
  <hr />
102 103
  <button onclick="clearlog()">Clear Log</button>
  <hr />
Tristan Cavelier's avatar
Tristan Cavelier committed
104 105
  <div id="log">
  </div>
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
  <script type="text/javascript">
    <!--
var log_color = "white";

function select(string) {
  return document.querySelector(string);
}
function logGetColor() {
  if (log_color === "white") {
    log_color = "cyan";
  } else {
    log_color = "white";
  }
  return log_color;
}
function log(o) {
  var node = document.createElement("pre");
  node.setAttribute(
    "style",
    "background-color:" + logGetColor() +
      ";margin:0;padding:0;"
  );
  if (typeof o === "string") {
    node.textContent = o;
  } else {
    node.textContent = JSON.stringify(o, null, "  ");
  }
  select("#log").appendChild(node);
}
function error(o) {
  var node = document.createElement("pre");
  node.setAttribute(
    "style",
    "background-color:" + logGetColor() +
      ";margin:0;padding:0;"
  );
  if (typeof o === "string") {
    node.textContent = o;
  } else {
    node.textContent = JSON.stringify(o);
  }
  select("#log").appendChild(node);
}
function clearlog() {
  select("#log").innerHTML = "";
}
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

function injectScript(url) {
  var script = document.createElement("script");
  script.setAttribute("src", url);
  document.body.appendChild(script);
}
function injectLastScripts() {
  var i, scripts = JSON.parse(localStorage.getItem("jio_dashboard_injected_scripts") || "{}");
  for (i in scripts) {
    if (i) {
      injectScript(i);
    }
  }
}
function saveScripts() {
  var scripts = {};
  [].forEach.call(document.querySelectorAll("#script_injection_space input[type=\"text\"]"), function (input) {
    return scripts[input.value] = true;
  });

  localStorage.setItem("jio_dashboard_injected_scripts", JSON.stringify(scripts));
  location.href = location.href;
}
function buildScriptFields() {
  var space, el, i, count = 0, scripts;
  function createInput(value) {
    var e = document.createElement("input");
    e.setAttribute("type", "text");
    e.setAttribute("style", "width: 98%;");
    if (value) { e.value = value; }
    count += 1;
    return e;
  }
  scripts = JSON.parse(localStorage.getItem("jio_dashboard_injected_scripts") || "{}");
  space = select("#script_injection_space");
  el = document.createElement("div");
  el.textContent = "Additional scripts:";
  space.appendChild(el);
  for (i in scripts) {
    if (i) {
      space.appendChild(createInput(i));
    }
  }
  space.appendChild(createInput());
  el = document.createElement("input");
  el.setAttribute("type", "button");
  el.value = "Save scripts and refresh page";
  el.onclick = saveScripts;
  space.appendChild(el);
}
202 203 204 205 206 207 208 209
// clear log on Alt+L
document.addEventListener("keypress", function (event) {
  if (event.altKey === true && event.charCode === 108) {
    clearlog();
  }
});
        //-->
  </script>
210
  <script src="../lib/rsvp/rsvp-custom.js"></script>
211 212 213
  <script src="../src/sha256.amd.js"></script>
  <script src="../jio.js"></script>
  <script src="../src/jio.storage/localstorage.js"></script>
214
  <script src="../src/jio.storage/davstorage.js"></script>
215
  <script src="http://git.erp5.org/gitweb/uritemplate-js.git/blob_plain/HEAD:/bin/uritemplate-min.js"></script>
216
  <script src="../lib/uri/URI.js"></script>
217
  <script src="../src/jio.storage/erp5storage.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
218 219
  <script type="text/javascript">
    <!--
220

Tristan Cavelier's avatar
Tristan Cavelier committed
221
var my_jio = null;
222

223 224 225
injectLastScripts();
buildScriptFields();

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
function fillMemoryDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "local",
    "username": "<username>",
    "application_name": "<app_name>",
    "mode": "memory"
  }, null, "  ")
}
function fillLocalDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "local",
    "username": "<username>",
    "application_name": "<app_name>"
  }, null, "  ")
}
function fillDavDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "dav",
244 245 246 247 248 249 250 251
    "url": "<url>"
  }, null, "  ")
}
function fillDavBasicDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "dav",
    "url": "<url>",
    "basic_login": "<btoa(username + ':' + password)>"
252 253 254 255 256
  }, null, "  ")
}
function fillERP5Description() {
  select("#storagedescription").value = JSON.stringify({
    "type": "erp5",
257
    "url": "<url to hateoas web site>"
258 259 260 261 262 263 264 265 266 267 268
  }, null, "  ")
}
function fillCustomDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "<type>"
  }, null, "  ")
}
function fillLastDescription() {
  select("#storagedescription").value =
    localStorage.getItem("last_jio_description") || "{}";
}
269 270 271 272 273 274 275 276 277 278
function saveDescription() {
  localStorage.setItem(
    "saved_jio_description",
    select("#storagedescription").value
  );
}
function loadDescription() {
  select("#storagedescription").value =
    localStorage.getItem("saved_jio_description") || "{}";
}
279 280 281 282 283 284 285 286 287 288 289 290
fillLastDescription();

function createJIO() {
  var description;
  try {
    description = JSON.parse(select("#storagedescription").value);
    my_jio = jIO.createJIO(description);
    description = JSON.stringify(description, null, "  ");
    log("JIO created\n" + description);
    localStorage.setItem("last_jio_description", description);
  } catch (e) {
    error("Storage description is not JSON parsable");
291
  }
292 293
}

294 295 296 297 298 299 300
function logError(begin_date, err) {
  log('time : ' + (Date.now() - begin_date));
  error('return :' + JSON.stringify(err, null, "  "));
  throw err;
}

function logAnswer(begin_date, val) {
301
  log('time : ' + (Date.now() - begin_date));
302
  log('return : ' + JSON.stringify(val, null, "  "));
303
  return val;
304 305
}

306 307
function command(method) {
  var doc = {}, opts = {}, lock, n, jio;
308

309
  if (!my_jio) {
310 311
    error('no jio set');
    return;
312
  }
Tristan Cavelier's avatar
Tristan Cavelier committed
313

314
  jio = my_jio;
315

316
  n = parseInt(select("#times").value, 10);
317

318
  lock = select("#times-lock").checked;
319

320 321 322
  if (!lock) {
    select("#times").value = "1";
  }
323

324 325 326
  if (!isFinite(n)) {
    n = 1;
  }
Tristan Cavelier's avatar
Tristan Cavelier committed
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  doc = select("#metadata").value;
  opts = select("#options").value;

  return jIO.util.range(n, function (index) {
    var param = doc, options = opts, begin = Date.now(), promise;

    param = param.replace(/\\u0000/g, index);
    options = options.replace(/\\u0000/g, index);

    param = JSON.parse(param);
    options = JSON.parse(options);

    if (method === "allDocs") {
      log(method + "\nopts: " + JSON.stringify(options, null, "  "));
      promise = jio.allDocs(options);
    } else {
      log(method + "\ndoc: " + JSON.stringify(param, null, "  ") +
          "\nopts: " + JSON.stringify(options, null, "  "));
      promise = jio[method](param, options);
    }

349 350 351 352 353 354
    return promise.then(function (answer) {
      n = answer;
      logAnswer(begin, answer);
      return answer;
    }, logError.bind(null, begin));
  }).then(function () { return n; }, function (e) {
355 356 357
    if (e instanceof Error) {
      error(e.toString());
    }
358
  }, console.info);
359
}
360 361 362 363 364 365 366 367 368 369 370

//////////////////////////////////////////////////////////////////////
// scripts

function scriptLogLocalStorage() {
  log("localStorage content\n" + JSON.stringify(localStorage, null, "  "));
}

function scriptRemoveAllDocs() {
  var original_metadata_value = select('#metadata').value;
  return command("allDocs").then(function (answer) {
371
    return jIO.util.forEach(answer.data.rows, function (row) {
372
      select("#metadata").value = JSON.stringify({"_id": row.id});
373 374 375
      var ret = command("remove");
      select('#metadata').value = original_metadata_value;
      return ret;
376
    });
377 378
  });
}
379 380 381 382

function scriptRepairAllDocs() {
  var original_metadata_value = select('#metadata').value;
  return command("allDocs").then(function (answer) {
383
    return jIO.util.forEach(answer.data.rows, function (row) {
384 385
      select("#metadata").value = JSON.stringify({"_id": row.id});
      var ret = command("repair");
386
      select("#metadata").value = original_metadata_value;
387
      return ret;
388
    });
389 390
  });
}
Tristan Cavelier's avatar
Tristan Cavelier committed
391 392 393 394
        //-->
  </script>
</body>
</html>