1. kai zhu
  2. tinyjs

Commits

kai zhu  committed c6a1cd5

add moduleMisc.js2

  • Participants
  • Parent commits 07c14c7
  • Branches master

Comments (0)

Files changed (1)

File moduleMisc.js2

View file
+/**
+ * miscellaneous code
+ * @module moduleMisc
+ */
+
+
+
+/*FILE_BEG {"actions": ["jslint", "cover"], "name": "/admin/admin.html"}*/
+<html>
+<head>
+<link rel="stylesheet" href="/public/cacheVersion/{{EXPORTS.VERSION}}/externalRollup.css">
+<style>
+#formDbTableEdit .row-fluid {
+  display: none;
+}
+#formDbTableEdit[data-action="aDbRecordCopy"] .aDbRecordCopy {
+  display: block;
+}
+#formDbTableEdit[data-action="aDbRecordEdit"] .aDbRecordEdit {
+  display: block;
+}
+#formDbTableEdit[data-action="aDbRecordRename"] .aDbRecordRename {
+  display: block;
+}
+#formDbTableEdit[data-action="aDbTableCreate"] .aDbTableCreate {
+  display: block;
+}
+#formDbTableEdit[data-action="aDbTableRename"] .aDbTableRename {
+  display: block;
+}
+.imgAjaxLoader {
+  background-color: white;
+  border: 2px solid;
+  border-radius: 1em;
+  display: none;
+  height: 64px;
+  left: 49%;
+  position: absolute;
+  top: 49%;
+  width: 64px;
+  z-index: 99;
+}
+.marginBottom {
+  margin-bottom: 20px;
+}
+.marginLeft {
+  margin-left: 2em;
+}
+.marginTop {
+  margin-top: 20px;
+}
+</style>
+</head>
+<body>
+
+<span class="imgAjaxLoader" id="imgAjaxLoader"></span>
+
+<form class="fade form hide modal" id="formDbTableEdit"><div class="container-fluid">
+  <div class="marginBottom"></div>
+  <div class="aDbTableRename row-fluid">
+    <label>current table name</label>
+    <input class="span12" id="formDbTableEditNameOld" readonly type="text"/>
+  </div>
+  <div class="aDbTableCreate aDbTableRename row-fluid">
+    <label>new table name</label>
+    <input class="span12" id="formDbTableEditNameNew" placeholder="newTable" type="text"/>
+  </div>
+  <div class="aDbRecordCopy aDbRecordEdit aDbRecordRename row-fluid">
+    <label>current record key</label>
+    <input class="span12" id="formDbRecordEditKeyOld" readonly type="text"/>
+  </div>
+  <div class="aDbRecordCopy aDbRecordRename row-fluid">
+    <label>new record key</label>
+    <input class="span12" id="formDbRecordEditKeyNew" type="text"/>
+  </div>
+  <div class="aDbRecordCopy aDbRecordEdit row-fluid">
+    <label>record value</label>
+    <textarea class="span12" id="formDbRecordEditValue" rows="8"></textarea>
+  </div>
+  <div class="aDbRecordCopy aDbRecordEdit aDbRecordRename aDbTableCreate aDbTableRename controls row-fluid">
+    <button class="btn btn-success" data-dismiss="modal" id="formDbTableEditSubmit">submit</button>
+    <button class="btn btn-danger marginLeft" data-dismiss="modal">cancel</button>
+  </div>
+  <div class="marginBottom"></div>
+</div></form>
+
+<div class="hide" id="fragmentDbRecordEdit">
+  <div class="btn-group">
+    <button class="btn btn-mini inputDbRecordSelect" data-toggle="button">select</button>
+    <button class="btn btn-mini dropdown-toggle" data-toggle="dropdown">edit <span class="caret"></span></button>
+    <ul class="dropdown-menu" data-key="{{key}}">
+      <li><a class="aDbRecordCopy" href="#">copy</a></li>
+      <li><a class="aDbRecordEdit" href="#">edit</a></li>
+      <li><a class="aDbRecordRename" href="#">rename</a></li>
+    </ul>
+  </div>
+</div>
+
+<div class="container-fluid marginTop">
+  <label class="control-label">table</label>
+  <div class="btn-group">
+    <button class="btn dropdown-toggle" data-toggle="dropdown" id="btnDbTableSelect">select table <span class="caret"></span></button>
+    <ul class="dropdown-menu" id="ulDbTables">
+    </ul>
+  </div>
+<div class="control-group row-fluid">
+  <div class="btn-group form-inline span12">
+    <button class="btn dropdown-toggle span2" data-toggle="dropdown" id="btnDbTableAction">table action <span class="caret"></span></button>
+    <ul class="dropdown-menu">
+      <li><a href="#" id="aDbTableCreate">create table</a></li>
+      <li><a id="aDbTableRemove" href="#">remove table</a></li>
+      <li><a href="#" id="aDbTableRename">rename table</a></li>
+      <li><a href="#" id="aDbTableSelect">select table</a></li>
+    </ul>
+    <input class="span10" id="inputDbTableId" readonly type="text"/>
+  </div>
+</div>
+
+<div class="marginTop"></div>
+<table class="dataTable" id="tableDbTable"></table>
+</div>
+
+<script src="/public/cacheVersion/{{EXPORTS.VERSION}}/externalRollup.js"></script>
+<script>
+/*global jQuery: false*/
+/*jslint bitwise: true, browser: true, indent: 2, nomen: true, regexp: true*/
+jQuery(document).ready(function () {
+  'use strict';
+
+  var dom = {}, local;
+
+  //// debug
+  window[String('p' + 'rintd')] = console.log;
+
+  local = {
+
+    adminEval: function (code, callback) {
+      local.ajax({
+        callback: callback,
+        contentType: 'application/octet-stream',
+        data: code,
+        method: 'POST',
+        url: '/admin/eval'
+      });
+    },
+
+    ajax: function (options) {
+      options.datatype = options.datatype || 'text';
+      dom.imgAjaxLoader.style.display = 'inline';
+      jQuery.ajax(options).done(function (data, statusCode, xhr) {
+        dom.imgAjaxLoader.style.display = 'none';
+        if (options.callback) {
+          options.callback(data, statusCode, xhr);
+          return;
+        }
+        window.AJAX_DATA = data;
+        try {
+          window.AJAX_DATA_JSON = JSON.parse(data);
+        } catch (errParse) {
+        }
+        console.log(data);
+      });
+    },
+
+    //// example usage - local.dbAction('/get/namespace1/table1')
+    dbAction: function (url, data, callback) {
+      if (!callback && typeof data === 'function') {
+        callback = data;
+        data = null;
+      }
+      local.ajax({
+        callback: callback,
+        data: data,
+        type: 'POST',
+        url: '/admin/db/' + url
+      });
+    },
+
+    _dbGetRecord: function (data) {
+      dom.formDbRecordEditValue.innerHTML = data;
+      jQuery(dom.formDbTableEdit).modal('show');
+    },
+
+    dbGetRecord: function (target, action) {
+      var id = target.parentElement.parentElement.getAttribute('data-key');
+      dom.formDbRecordEditKeyOld.value = id;
+      dom.formDbRecordEditKeyNew.value = local.uuid4();
+      dom.formDbTableEdit.setAttribute('data-action', action);
+      local.dbAction('/get/' + local.DB_TABLE_ID + '/' + id, null, local._dbGetRecord);
+    },
+
+    _dbListTable: function (arr) {
+      var ii;
+      arr = JSON.parse(arr);
+      for (ii = 0; ii < arr.length; ii += 1) {
+        arr[ii] = [
+          dom.fragmentDbRecordEdit.innerHTML.replace('{{key}}',  arr[ii]),
+          arr[ii]
+        ];
+      }
+      jQuery(dom.tableDbTable).dataTable({
+        aaData: arr,
+        aaSorting: [[1, 'asc']],
+        aoColumns: [
+          {bSortable: false, sTitle: 'select / edit', sWidth: '6em'},
+          {sTitle: 'id'}
+        ],
+        bDeferRender: true,
+        bDestroy: true,
+        bProcessing: true,
+        iDisplayLength: 100
+      });
+    },
+
+    dbListTable: function (id) {
+      local.DB_TABLE_ID = local.localStorageSetItemOrClear('DB_TABLE_ID', id);
+      dom.btnDbTableSelect.innerHTML = id + ' <span class="caret"><\/span>';
+      local.dbAction('/list/' + id, null, local._dbListTable);
+    },
+
+    _dbListTables: function (arr) {
+      var ii, html = '';
+      arr = JSON.parse(arr);
+      for (ii = 0; ii < arr.length; ii += 1) {
+        html += '<li><a class="aDbTableSelect" href="#">' + arr[ii] + '<\/a><\/li>';
+      }
+      dom.ulDbTables.innerHTML = html;
+    },
+
+    dbListTables: function () {
+      local.dbAction('/list', null, local._dbListTables);
+    },
+
+    eventHandler: function (evt) {
+      var ii, target = evt.target;
+      //// debug
+      window.evt = evt;
+      console.log([evt, evt.target]);
+      switch (evt.type) {
+      case 'click':
+        switch (target.id) {
+        case 'aDbTableCreate':
+          dom.formDbTableEdit.setAttribute('data-action', target.id);
+          jQuery(dom.formDbTableEdit).modal('show');
+          return;
+        case 'aDbTableRemove':
+          if (!window.confirm('remove table "' + local.DB_TABLE_ID + '"?')) {
+            return;
+          }
+          local.dbAction('/remove/' + local.DB_TABLE_ID, null, local.dbListTables);
+          return;
+        case 'aDbTableRename':
+          dom.formDbTableEditNameOld.value = local.DB_TABLE_ID;
+          dom.formDbTableEdit.setAttribute('data-action', target.id);
+          jQuery(dom.formDbTableEdit).modal('show');
+          return;
+        case 'aDbTableSelect':
+          dom.formDbTableEdit.setAttribute('data-action', target.id);
+          jQuery(dom.formDbTableEdit).modal('show');
+          return;
+        }
+        for (ii = 0; ii < target.classList.length; ii += 1) {
+          switch (target.classList[ii]) {
+          case 'aDbRecordCopy':
+          case 'aDbRecordEdit':
+            local.dbGetRecord(target, target.classList[ii]);
+            return;
+          case 'aDbRecordRename':
+            dom.formDbRecordEditKeyOld.value = target.parentElement.parentElement.getAttribute('data-key');
+            dom.formDbRecordEditKeyNew.value = local.uuid4();
+            dom.formDbTableEdit.setAttribute('data-action', target.classList[ii]);
+            jQuery(dom.formDbTableEdit).modal('show');
+            return;
+          //// select table
+          case 'aDbTableSelect':
+            evt.preventDefault();
+            local.dbListTable(target.innerHTML);
+            return;
+          }
+        }
+        return;
+      }
+    },
+
+    localStorageSetItemOrClear: function (key, val) {
+      //// auto-clear local storage if maximum space exceeded
+      try {
+        localStorage.setItem(key, val);
+      } catch (err) {
+        localStorage.clear();
+        localStorage.setItem(key, val);
+      }
+      return val;
+    },
+
+    uuid4: function () {
+      //// return uuid4 string of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
+      var uuid = '', ii;
+      for (ii = 0; ii < 32; ii += 1) {
+        switch (ii) {
+        case 8:
+        case 20:
+          uuid += '-';
+          uuid += (Math.random() * 16 | 0).toString(16);
+          break;
+        case 12:
+          uuid += '-';
+          uuid += '4';
+          break;
+        case 16:
+          uuid += '-';
+          uuid += (Math.random() * 4 | 8).toString(16);
+          break;
+        default:
+          uuid += (Math.random() * 16 | 0).toString(16);
+        }
+      }
+      return uuid;
+    },
+
+    _init: function () {
+      var arr, ii;
+      //// event handlers
+      document.onclick = local.eventHandler;
+      //// auto index elements by id
+      arr = document.querySelectorAll('[id]');
+      for (ii = 0; ii < arr.length; ii += 1) { dom[arr[ii].id] = arr[ii]; }
+      //// restore state
+      local.DB_TABLE_ID = localStorage.getItem('DB_TABLE_ID');
+      //// refresh
+      local.dbListTables();
+      if (local.DB_TABLE_ID) {
+        local.dbListTable(local.DB_TABLE_ID);
+      }
+      //// EXPORTS
+      window.dom = dom;
+      window.local = local;
+      Object.keys(local).forEach(function (key) {
+        if (!window.hasOwnProperty(key)) { window[key] = local[key]; }
+      });
+    }
+
+  };
+
+  local._init();
+
+});
+</script>
+</body>
+</html>
+/*FILE_END*/
+
+
+
+/*FILE_BEG {"actions": ["write"], "name": "/FS_MODULE/.gitignore"}*/
+.DS_Store
+.gitignore
+node_modules
+yuidoc.json
+/*FILE_END*/
+
+
+
+/*FILE_BEG {"actions": ["jslint", "write"], "name": "/FS_MODULE/package.json"}*/
+{
+  "author": "kai zhu <kaizhu256@gmail.com>",
+  "bin": { "tinyjs": "moduleMain.js" },
+  "dependencies": {
+    "express": "*",
+    "fs-extra": "*",
+    "istanbul": "*",
+    "jslint": "*",
+    "mime": "*",
+    "nopt": "*",
+    "tinyjs-external": "*",
+    "uglify-js": "*",
+    "uglifycss": "*"
+  },
+  "description": "tinyjs is another nodejs micro-framework",
+  "engines": {
+    "node": ">=0.8.0"
+  },
+  "files": [
+    "moduleMain.js",
+    "moduleMisc.js2",
+    "libUnstable.js2",
+    "README.md"
+  ],
+  "homepage": "https://bitbucket.org/kaizhu/tinyjs",
+  "keywords": [
+    "micro framework", "tinyjs", "template", "zero config"
+  ],
+  "license": "BSD",
+  "main": "moduleMain.js",
+  "name": "tinyjs",
+  "os" : [
+    "!win32"
+  ],
+  "repository": {
+    "type": "git",
+    "url": "https://bitbucket.org/kaizhu/tinyjs.git"
+  },
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "version": "{{EXPORTS.VERSION}}"
+}
+/*FILE_END*/
+
+
+
+/*FILE_BEG {"actions": ["jslint", "write"], "name": "/FS_MODULE/yuidoc.json"}*/
+{
+  "options": {
+    "port": "4060",
+    "extension": ".js,.js2",
+    "outdir": "./docs"
+  }
+}
+/*FILE_END*/