all repos — litestore @ 8b44ecc4b7de1f2e39aad779190d1391ded9782c

A minimalist nosql document store.

admin/js/components/uploader.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
/*
 * Dependencies:
 * - models.js
 * - utils.js
 */
(function(){
  'use strict';  
  var app = window.LS || (window.LS = {});
  var u = app.utils;
  var w = app.widgets;
  
  /**
   * @param {Object} obj
   * @param {string} obj.docid
   * @param {string} obj.id
   * @param {Function} obj.onSuccess
   * @param {Function} obj.onFailure
   */
  app.uploader = function(obj){

    var modalId = "#upload-"+obj.id+"-modal";
    var uploader = {};
    
    uploader.config = function(obj){
      return function(element, isInitialized, context){
        $(element).change(function(event){
          obj.file(element.files[0]);
          if (obj.reader.readyState != 1) {
            obj.reader.readAsDataURL(obj.file()); 
          }
        });
      };
    };
    
    uploader.controller = function(args) {
      var vm = this;

      vm.docid = m.prop(args.docid);
      vm.file = m.prop();
      vm.id = args.id; 
      vm.btnId = "#upload-"+vm.id+"-btn";
      vm.reader = new FileReader();
      vm.contents = m.prop();
      vm.isText = m.prop(false);

      vm.reader.onloadstart = function() {
        vm.contents("");
        $(modalId).find(".btn-primary").attr("disabled", true);
      };

      vm.reader.onloadend = function() {
        vm.contents(vm.reader.result);
        $(modalId).find(".btn-primary").removeAttr("disabled");
      };

      vm.save = function() {
        var doc = {id: vm.docid()};
        doc.data = vm.contents().split(',')[1];
        if (vm.isText()) {
          doc.data = window.atob(doc.data);
        }
        return Doc.put(doc, vm.file().type).then(args.onSuccess, args.onFailure);
      };
      return vm;
    }
    
    uploader.view = function(ctrl, args){
      var config = {
        title: "Upload Document",
        id: "upload-"+ctrl.id+"-modal",
        action: ctrl.save,
        actionText: "Upload",
        content: m("div", [
          m(".form-group", [
          m("label", "Document ID"),
            m("input.form-control", {
              placeholder: "Enter document ID",
              onchange: m.withAttr("value", ctrl.docid),
              size: 35,
              disabled: (ctrl.docid() === "") ? false : true,
              value: ctrl.docid()
            })
          ]),
          m(".form-group", [
            m("label", "File"),
            m("input.form-control#upload-"+ctrl.id+"-btn", {type:"file", config: uploader.config(ctrl)}),
            m("p.help-block", "Select a file to upload as document.")
          ]),
          m(".checkbox", [
            m("label", [
              m("input", {type: "checkbox", value: ctrl.isText(), onchange: m.withAttr("value", ctrl.isText)}), 
              "Text File"
            ]),
            m("p.help-block", "Select if the file to upload contains textual content.")
          ])
        ])
      };
      return w.modal(config);
    };

    var instance = m.component(uploader, obj);
    instance.show = function() {
      return u.showModal(modalId);
    };
    return instance;
  };
}());