all repos — litestore @ 5533c4d97b9d81537d7a923b76ae11691566e4c3

A minimalist nosql document store.

admin/js/utils.js

 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
 128
 129
 130
 131
 132
 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
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
(function(){
  'use strict';
  var app = window.LS || (window.LS = {});
  var u = app.utils = {};
  
  // http://byronsalau.com/blog/how-to-create-a-guid-uuid-in-javascript/
  u.guid = function(){
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
      return v.toString(16);
    });
  };
  
  u.fixHeadings = function(html, maxheading){
    var $content = $(html);
    var n = maxheading;
    if ($content.find("h"+n).length > 0) {
      return $content.html();
    } else {
      for (var i=n+1; i<=6; i++){
        var j = i-1;
        $content.find("h"+i).each(function(){
          $(this).replaceWith("<h"+j+">"+$(this).html()+"</h"+j+">");
        });
      }
      return u.fixHeadings($content, maxheading);
    }
  };
  
  u.setContentType = function(doc, contentType){
    var type = "";
    var subtype = "";
    if (doc.tags && doc.tags.length > 0) {
      doc.tags.forEach(function(tag){
        var t = tag.match(/^\$type:(.+)/);
        var s = tag.match(/^\$subtype:(.+)/);
        if (t) type = t[1];
        if (s) subtype = s[1];
      });
      contentType = type+"/"+subtype;
    }
    return function(xhr) {
      xhr.setRequestHeader("Content-Type", contentType);
    };
  };

  u.markdown = function(s) {
    var hs = new marked.Renderer();
    var md = new marked.Renderer();
    hs.blockquote = function(q){
      var lines = q.split("\n");
      if (lines[0]){
        var r = /^<p>\s*%(.+?)%\s*/;
        var matches = lines[0].match(r);
        if (matches) {
          var klass = matches[1];
          if (klass){
            return "<div class=\""+klass+"\">\n"+q.replace(r, "<p>")+"\n</div>";
          }
        }
      }
      return md.blockquote(q);
    };
    hs.link = function(href, title, text){
      var components = href.match(/^([a-z]+):(.+)$/);
      if (components && components.length > 2){
        var protocol = components[1];
        var value = components[2];
        if (protocol === "class"){
          return "<span class=\""+value+"\">"+text+"</span>";
        } else if (protocol === "id"){
          return "<a id=\""+value+"\">"+text+"</a>";
        } else if (protocol === "abbr"){
          return "<abbr title=\""+value+"\">"+text+"</abbr>";
        }
      }
      return md.link(href, title, text);
    };
    var html = marked(s, {renderer: hs});
    var $html = $('<div>').append($(html).clone());
    return u.fixHeadings($html, 2);
  };
  
  /**
   * mod object:
   * @param {Object} mod
   * @param {Object} mod.vm
   * @param {Function} mod.vm.init
   * @param {Function} mod.main
   */
  u.layout = function(mod) {
    mod.controller = mod.controller || function(args){
      mod.vm.init();
      // Display flash if set on previous route
      mod.vm.flash = m.prop(u.flash());
      LS.flash = m.prop();
      mod.vm.showFlash = function(obj){
        LS.flash(obj);
        mod.vm.flash(u.flash());
        LS.flash = m.prop();
      };
      mod.vm.flashError = function(obj){
        mod.vm.showFlash({type: "warning", content: obj.error});
      };
    };
    mod.view = function(ctrl){
      return m("div", [
        m(".container", [
            m.component(app.navbar),
            m("main", [mod.vm.flash(), mod.main()]),
            m("footer.footer.container.center", [
              m("p")])
          ])
      ]);
    };
  };

  u.flash = function(){
    if (LS.flash()){
      return m(".row.alert.alert-dismissible.alert-"+LS.flash().type, [
        m("button.close[data-dismiss='alert']", m.trust("&times;")),
        LS.flash().content]);
    } else {
      return "";
    }
  };

  u.date = function(date) {
    return (date) ? new Date(Date.parse(date)).toUTCString() : "n/a";
  };

  
  u.showModal = function(sel){
    return function(){
      $(sel).modal();
    };
  };


  /* Component Factories */

  /**
   * @param {Object} obj
   * @param {string} obj.docid
   * @param {string} obj.id
   * @param {Function} obj.onSuccess
   * @param {Function} obj.onFailure
   */
  u.uploader = function(obj){
    var modalId = "#upload-"+obj.id+"-modal";
    var instance = m.component(app.uploader, obj);
    instance.show = function() {
      return u.showModal(modalId);
    };
    return instance;
  };

  /**
   * Creates a Panel component.
   * @param {Object} obj
   * @param {string} obj.title
   * @param {string} obj.footer
   * @param {string} obj.content
   */
  u.panel = function(obj){
    return m.component(app.widgets.panel, obj);
  };
    
  /**
   * @typedef {Object} PaginatorConfig
   * @prop {string} baseurl
   * @prop {int} total
   * @prop {int} limit
   * @prop {int} offset
   *
   * Creates a Paginator component.
   * @param {PaginatorConfig} obj
   */
  u.paginator = function(obj) {
    return m.component(app.widgets.paginator, obj);
  };

  /**
   * @typedef {Object} DropdownLink
   * @prop {string} path
   * @prop {string} title
   *
   * Creates a Dropdown component.
   * @param {Object} obj
   * @param {string} obj.icon
   * @param {string} obj.title
   * @param {string} obj.active
   * @param {array.DropdownLink} obj.links
   */
  u.dropdown = function(obj) {
    return m.component(app.widgets.dropdown, obj);
  };

  /**
   * Creates a TagLink component.
   * @param {Object} obj
   * @param {string} obj.name
   */
  u.taglink = function(obj){
    return m.component(app.widgets.taglink, obj);
  };
  
  /**
   * Creates a DocLink component.
   * @param {Object} obj
   * @param {string} obj.id
   */
  u.doclink = function(obj) {
    return m.component(app.widgets.doclink, obj);
  };

  /**
   * Creates a TagButton component.
   * @param {Object} obj
   * @param {string} obj.name
   * @param {int} obj.n
   */
  u.tagbutton = function(obj) {
    return m.component(app.widgets.tagbutton, obj);
  };

  /**
   * @typedef {Object} ToolbarLink
   * @prop {Function} action
   * @prop {Function} config
   * @prop {string} title
   * @prop {string} icon
   *
   * Creates a ToolBar component.
   * @param {Object} obj
   * @param {array.<ToolbarLink>} obj.links
   */
  u.toolbar = function(obj) {
    return m.component(app.widgets.toolbar, obj);
  };

  /**
   * Creates a Modal component.
   * @param {Object} obj
   * @param {string} obj.id
   * @param {string} obj.content
   * @param {string} obj.title
   * @param {Function} obj.action
   * @param {string} obj.actionText
   */
  u.modal = function(obj) {
    return m.component(app.widgets.modal, obj);
  };

}());