jio_dashboard.html 10.7 KB
Newer Older
Tristan Cavelier's avatar
Tristan Cavelier committed
1 2 3 4
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
5
  <title>jIO Dashboard</title>
Tristan Cavelier's avatar
Tristan Cavelier committed
6 7
</head>
<body>
8
  <table border="1" style="width: 100%;">
9 10 11 12
    <tr>
      <th style="text-align: center;" id="script_injection_space">
      </th>
    </tr>
13
    <tr style="font-style:italic;">
Tristan Cavelier's avatar
Tristan Cavelier committed
14
      <th>Storage Description</th>
15 16 17
    </tr>
    <tr>
      <th style="width:100%;">
Tristan Cavelier's avatar
Tristan Cavelier committed
18 19
        <textarea id="storagedescription" rows="10"
                  style="width:98%;"></textarea>
20 21 22
      </th>
    </tr>
    <tr>
Tristan Cavelier's avatar
Tristan Cavelier committed
23 24 25 26
      <td style="text-align: center;">
        <button onclick="fillMemoryDescription()">Memory</button>
        <button onclick="fillLocalDescription()">Local</button>
        <button onclick="fillDavDescription()">WebDAV</button>
27
        <button onclick="fillDavBasicDescription()">WebDAV Basic</button>
Tristan Cavelier's avatar
Tristan Cavelier committed
28
        <button onclick="fillERP5Description()">ERP5</button>
Tristan Cavelier's avatar
Tristan Cavelier committed
29 30
        <button onclick="fillCustomDescription()">Custom</button>
        <br /><button onclick="fillLastDescription()">Last</button>
31 32
        <button onclick="loadDescription()">Load</button>
        <button onclick="saveDescription()">Save</button>
Tristan Cavelier's avatar
Tristan Cavelier committed
33 34 35 36
      </td>
    </tr>
    <tr>
      <th><button onclick="createJIO()">Create JIO</button></th>
37 38
    </tr>
  </table>
Tristan Cavelier's avatar
Tristan Cavelier committed
39
  <br />
40 41
  <table border="1" style="width: 100%;">
    <tr>
Tristan Cavelier's avatar
Tristan Cavelier committed
42
      <td colspan="1" style="width: 50%;">
Tristan Cavelier's avatar
Tristan Cavelier committed
43
        <label for="metadata">Metadata or Parameters:</label>
44 45
        <textarea id="metadata" rows="3" style="width: 98%;">{}</textarea>
      </td>
Tristan Cavelier's avatar
Tristan Cavelier committed
46
      <td colspan="1" style="text-align: center;">
47
        Options:<br />
Tristan Cavelier's avatar
Tristan Cavelier committed
48
        <textarea id="options" rows="3" style="width: 98%;">{}</textarea>
49 50 51
      </td>
    </tr>
    <tr>
Tristan Cavelier's avatar
Tristan Cavelier committed
52
      <td colspan="2" style="text-align: center;">
53 54 55 56 57 58 59 60 61 62
        <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>
63 64 65 66
      </td>
    </tr>
    <tr>
      <td colspan="2" style="text-align: center;">
Tristan Cavelier's avatar
Tristan Cavelier committed
67 68 69 70
        <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>
71 72
      </td>
    </tr>
73 74 75
  </table>
  <br />
  <div style="text-align: center;">
Tristan Cavelier's avatar
Tristan Cavelier committed
76 77 78 79
    Useful functions:
    <button onclick="scriptLogLocalStorage()">log localStorage</button>
    <button onclick="localStorage.clear()">clear localStorage</button>
    <button onclick="scriptRemoveAllDocs()">removeAllDocs</button>
80
    <button onclick="scriptRepairAllDocs()">repairAllDocs</button>
81
  </div>
Tristan Cavelier's avatar
Tristan Cavelier committed
82
  <hr />
Tristan Cavelier's avatar
Tristan Cavelier committed
83 84
  <button onclick="clearlog()">Clear Log</button>
  <hr />
Tristan Cavelier's avatar
Tristan Cavelier committed
85 86
  <div id="log">
  </div>
Tristan Cavelier's avatar
Tristan Cavelier committed
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 128 129 130 131 132
  <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 = "";
}
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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

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);
}
Tristan Cavelier's avatar
Tristan Cavelier committed
183 184 185 186 187 188 189 190
// clear log on Alt+L
document.addEventListener("keypress", function (event) {
  if (event.altKey === true && event.charCode === 108) {
    clearlog();
  }
});
        //-->
  </script>
191
  <script src="../lib/rsvp/rsvp-custom.js"></script>
192 193 194
  <script src="../src/sha256.amd.js"></script>
  <script src="../jio.js"></script>
  <script src="../src/jio.storage/localstorage.js"></script>
195
  <script src="../src/jio.storage/davstorage.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
196
  <script src="http://git.erp5.org/gitweb/uritemplate-js.git/blob_plain/HEAD:/bin/uritemplate-min.js"></script>
197
  <script src="../lib/uri/URI.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
198
  <script src="../src/jio.storage/erp5storage.js"></script>
Tristan Cavelier's avatar
Tristan Cavelier committed
199 200
  <script type="text/javascript">
    <!--
Tristan Cavelier's avatar
Tristan Cavelier committed
201

Tristan Cavelier's avatar
Tristan Cavelier committed
202
var my_jio = null;
Tristan Cavelier's avatar
Tristan Cavelier committed
203

204 205 206
injectLastScripts();
buildScriptFields();

Tristan Cavelier's avatar
Tristan Cavelier committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
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",
225 226 227 228 229 230 231 232
    "url": "<url>"
  }, null, "  ")
}
function fillDavBasicDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "dav",
    "url": "<url>",
    "basic_login": "<btoa(username + ':' + password)>"
Tristan Cavelier's avatar
Tristan Cavelier committed
233 234 235 236 237
  }, null, "  ")
}
function fillERP5Description() {
  select("#storagedescription").value = JSON.stringify({
    "type": "erp5",
Tristan Cavelier's avatar
Tristan Cavelier committed
238
    "url": "<url to hateoas web site>"
Tristan Cavelier's avatar
Tristan Cavelier committed
239 240 241 242 243 244 245 246 247 248 249
  }, null, "  ")
}
function fillCustomDescription() {
  select("#storagedescription").value = JSON.stringify({
    "type": "<type>"
  }, null, "  ")
}
function fillLastDescription() {
  select("#storagedescription").value =
    localStorage.getItem("last_jio_description") || "{}";
}
250 251 252 253 254 255 256 257 258 259
function saveDescription() {
  localStorage.setItem(
    "saved_jio_description",
    select("#storagedescription").value
  );
}
function loadDescription() {
  select("#storagedescription").value =
    localStorage.getItem("saved_jio_description") || "{}";
}
Tristan Cavelier's avatar
Tristan Cavelier committed
260 261 262 263 264 265 266 267 268 269 270 271
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");
272
  }
Tristan Cavelier's avatar
Tristan Cavelier committed
273 274
}

275 276 277 278 279 280 281
function logError(begin_date, err) {
  log('time : ' + (Date.now() - begin_date));
  error('return :' + JSON.stringify(err, null, "  "));
  throw err;
}

function logAnswer(begin_date, val) {
282
  log('time : ' + (Date.now() - begin_date));
Tristan Cavelier's avatar
Tristan Cavelier committed
283
  log('return : ' + JSON.stringify(val, null, "  "));
284
  return val;
Tristan Cavelier's avatar
Tristan Cavelier committed
285 286
}

287 288
function command(method) {
  var doc = {}, opts = {}, lock, n, jio;
Tristan Cavelier's avatar
Tristan Cavelier committed
289

290
  if (!my_jio) {
291 292
    error('no jio set');
    return;
293
  }
Tristan Cavelier's avatar
Tristan Cavelier committed
294

295
  jio = my_jio;
296

Tristan Cavelier's avatar
Tristan Cavelier committed
297
  n = parseInt(select("#times").value, 10);
298

Tristan Cavelier's avatar
Tristan Cavelier committed
299
  lock = select("#times-lock").checked;
300

Tristan Cavelier's avatar
Tristan Cavelier committed
301 302 303
  if (!lock) {
    select("#times").value = "1";
  }
304

Tristan Cavelier's avatar
Tristan Cavelier committed
305 306 307
  if (!isFinite(n)) {
    n = 1;
  }
Tristan Cavelier's avatar
Tristan Cavelier committed
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  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);
    }

330 331 332 333 334 335
    return promise.then(function (answer) {
      n = answer;
      logAnswer(begin, answer);
      return answer;
    }, logError.bind(null, begin));
  }).then(function () { return n; }, function (e) {
336 337 338
    if (e instanceof Error) {
      error(e.toString());
    }
339
  }, console.info);
Tristan Cavelier's avatar
Tristan Cavelier committed
340
}
341 342 343 344 345 346 347 348 349 350 351

//////////////////////////////////////////////////////////////////////
// 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) {
352
    return jIO.util.forEach(answer.data.rows, function (row) {
353
      select("#metadata").value = JSON.stringify({"_id": row.id});
354 355 356
      var ret = command("remove");
      select('#metadata').value = original_metadata_value;
      return ret;
357
    });
358 359
  });
}
360 361 362 363

function scriptRepairAllDocs() {
  var original_metadata_value = select('#metadata').value;
  return command("allDocs").then(function (answer) {
364
    return jIO.util.forEach(answer.data.rows, function (row) {
365 366
      select("#metadata").value = JSON.stringify({"_id": row.id});
      var ret = command("repair");
367
      select("#metadata").value = original_metadata_value;
368
      return ret;
369
    });
370 371
  });
}
Tristan Cavelier's avatar
Tristan Cavelier committed
372 373 374 375
        //-->
  </script>
</body>
</html>