all repos — litestore @ 9b46f23f05fbbaf1b065efd3c029ca2698bf0a53

A minimalist nosql document store.

admin/js/components/widgets.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
(function(){
  'use strict';
  var app = window.LS || (window.LS = {});
  
  app.widgets = {};
    
  // Panel
  app.widgets.panel = function(obj){
    var panel = {
      controller: function(args){
        return {
          title: args.title,
          footer: args.footer,
          content: args.content,
        };
      },
      view: function(ctrl){
        var title = "";
        var footer = "";
        if (ctrl.title){
          title = m(".panel-heading", [
            m("h2.panel-title", [ctrl.title])  
          ]);
        }
        if (ctrl.footer){
          footer = m(".panel-footer", ctrl.footer);
        }
        return m(".panel.panel-default", [
          title,
          m(".panel-body", [
            ctrl.content
          ]),
          footer
        ]);
      }
    };
    return m.component(panel, obj);
  };
    
  // Paginator
  app.widgets.paginator = function(obj) {
    var paginator = {
      controller: function(args){
        var max_page = Math.min(14, Math.ceil(args.total/args.limit)-1);
        var c_page = Math.ceil(args.offset/args.limit);
        return {
          baseurl: args.baseurl,
          total: args.total,
          limit: args.limit,
          offset: args.offset,
          max_page: max_page,
          c_page: c_page
        };
      },
      view: function(ctrl){
        var page = function(n, sign, disabled){
          var klass;
          if (disabled) {
            klass = "disabled";
          } else {
            klass = (n === ctrl.c_page) ? "active" : "inactive";
          }
          var first = (n === 0);
          var last = (n == ctrl.max_page);
          var offset = ctrl.limit * n;
          sign = sign || n+1;  
          return m("li", {class: klass},
              [m("a", {
                        href: ctrl.baseurl +(n+1), // assuming 10 elements per page //+"/"+obj.limit,
                        config: m.route
                      }, [m.trust(sign)]
              )]
            );
        };
        
        var pages = [];
        var prev;
        var next;
        for (var i=0; i<=ctrl.max_page; i++){
          var p;
          switch(i){
            case ctrl.c_page-1:
              prev = page(i, "&laquo;");
              break;
            case ctrl.c_page+1:
              next = page(i, "&raquo;");
              break;
          }
          if (ctrl.c_page === 0){
              prev = page(0, "&laquo;", true);
          }
          if (ctrl.c_page === ctrl.max_page){
              next = page(ctrl.max_page, "&raquo;", true);
          }
          pages.push(page(i));
        }
        pages.unshift(prev);
        pages.push(next);
        return m("nav", [m("ul.pagination", pages)]);
        }
    };
    return m.component(paginator, obj);
  };
  
  // Dropdown
  app.widgets.dropdown = function(obj) {
    var dropdown = {
      controller: function(args){
        return {
          icon: args.icon,
          active: args.active,
          title: args.title,
          links: args.links
        };
      },
      view: function(ctrl){
        var el = "li.dropdown";
        var icon = (ctrl.icon) ? m("i.fa."+ctrl.icon) : "";
        if (ctrl.active.length > 0) {
          el += "."+ctrl.active;
        }
        return m(el, [
          m("a.dropdown-toggle[href='#'][data-toggle='dropdown'][role='button'][aria-expanded='false']",
          [icon, m("span", " "+ctrl.title+" "), m("span.caret")]),
          m("ul.dropdown-menu[role='menu']", 
          ctrl.links.map(function(e){
            return m("li", 
          [m("a", {href: e.path, config: m.route}, m.trust(e.title))]);}))
        ]);
      }
    };
    return m.component(dropdown, obj);
  };

  // TagLink
  app.widgets.taglink = function(obj){
    var taglink = {
      controller: function(args) {
        return {
          color: /^\$/.test(args.name) ? "warning" : "primary",
          name: args.name
        };
      },
      view: function(ctrl) {
        return m("span.tag-label.label.label-"+ctrl.color, 
          [m("i.fa.fa-tag"), " ", m("a", {href: "/tags/"+ctrl.name, config:m.route}, ctrl.name)]);
      }
    };
    return m.component(taglink, obj);
  };
  
  // DocLink (warning: API change!)
  app.widgets.doclink = function(obj) {
    var doclink = {
      controller: function(args){
        return {id: args.id};
      },
      view: function(ctrl) {
        return m("a", {href: "/document/view/"+ctrl.id, config: m.route}, id);
      }
    };
    return m.component(doclink, obj);
  };
  
  // TagButton (warning: API change!)
  app.widgets.tagbutton = function(obj) {
    var tagbutton = {
      controller: function(args){
        return {
          name: args.tag,
          n: args.n
        };
      },
      view: function(ctrl) {
        return m("a", {href: "/tags/"+ctrl.tag, config:m.route},
          [m("i.fa.fa-tag"), " "+ctrl.name+" ", m("span.badge", ctrl.n)]);
      }
    };
    return m.component(tagbutton, obj);
  };
  
  // Toolbar
  app.widgets.toolbar = function(obj) {
    var toolbar = {
      controller: function(args){
        return {links: args.links};
      },
      view: function(ctrl){
        return m("nav.toolbar.btn-group[role='group'][aria-label='...'].pull-right", 
          ctrl.links.map(function(l){
            return m("a.btn.btn-default", 
                      {onclick:l.action, config: l.config}, 
                      [m("i.fa.fa-"+l.icon), " "+l.title]);
          })  
        );
      }
    };
    return m.component(toolbar, obj);
  };
  
  // Modal
  app.widgets.modal = function(obj) {
    var modal = {
      controller: function(args){
        return {
          id: args.id,
          content: args.content,
          title: args.title,
          action: args.action,
          actionText: args.actionText
        };
      },
      view: function(ctrl){
        return m(".modal.fade", 
          {id: ctrl.id, tabindex: "-1", role: "dialog"},
          [
            m(".modal-dialog", [
              m(".modal-content", [
                m(".modal-header", [
                  m("button", {type: "button", class: "close", "data-dismiss": "modal"}, 
                  [m.trust("&times;")]),
                  m("h4.modal-title", ctrl.title)
                ]),
                m(".modal-body", [ctrl.content]),
                m(".modal-footer", [
                  m("button.btn.btn-default[data-dismiss='modal']", "Close"),
                  m("button.btn.btn-primary[data-dismiss='modal']", {onclick: ctrl.action}, ctrl.actionText)
                ])
              ])
            ])
          ]
        );
      }
    };
    return m.component(modal, obj);
  };

}());