all repos — litestore @ 62b7d01b1c14644d771cab35732f9f99ea83e50b

A minimalist nosql document store.

app/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
(function(){
  'use strict';
  var app = window.LS || (window.LS = {});
  var u = app.utils = {};
  
  /**
   * mod object:
   * @property vm a view-model (with init function)
   * @property main the main view to load
   */
  u.layout = function(mod) {
  
    mod.controller = mod.controller || function(){
      this.navbar = new app.navbar.controller();
      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", [
            app.navbar.view(ctrl.navbar),
            m("main", [mod.vm.flash(), mod.main()]),
            m("footer.footer.container.center", [
              m("p")])
          ])
      ]);
    };
  };

  u.panel = function(obj){
    return m(".panel.panel-default", [
      m(".panel-heading", [
        m("h2.panel-title", [obj.title])  
      ]),
      m(".panel-body", [
        obj.content
      ])
    ]);
  };
  u.dropdown = function(obj) {
    var el = "li.dropdown";
    var icon = (obj.icon) ? m("i.fa."+obj.icon) : "";
    if (obj.active.length > 0) {
      el += "."+obj.active;
    }
    return m(el, [
      m("a.dropdown-toggle[href='#'][data-toggle='dropdown'][role='button'][aria-expanded='false']",
      [icon, m("span", " "+obj.title+" "), m("span.caret")]),
      m("ul.dropdown-menu[role='menu']", 
      obj.links.map(function(e){
        return m("li", 
      [m("a", {href: e.path, config: m.route}, e.title)]);}))
    ]);
  };

  u.taglink = function(tag) {
    return m("span.label.label-primary", [m("a", {href: "/tags/"+tag, config:m.route}, tag)]);
  };

  u.doclink = function(id) {
    return m("a", {href: "/document/view/"+id, config: m.route}, id);
  };

  u.date = function(date) {
    if (date === ""){
      return "";
    } else {
      return new Date(Date.parse(date)).toUTCString();
    }
  };

  u.toolbar = function(obj){
    return m("nav.toolbar.btn-group[role='group'][aria-label='...'].pull-right", obj.links.map(function(l){
      return m("a.btn.btn-default", {onclick:l.action}, [m("i.fa.fa-"+l.icon), " "+l.title]);
      })  
    );
  };
  
  u.flash = function(){
    if (LS.flash()){
      return m(".row.alert.alert-dismissible.alert-"+LS.flash().type, [
        m("button.close[data-dismiss='alert']", m.trust("×")),
        LS.flash().content]);
    } else {
      return "";
    }
  };

  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);
    };
  };
}());