aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/mt-input/mt-input.js
blob: 729adb9a81d2a6a0de203016fa2bbd90a5fb6bb1 (plain)
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
/*
 * Template Component: MtInput 
 */
    
//import { Meteor } from 'meteor/meteor'; 
import { Template } from 'meteor/templating';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import * as R from 'ramda';
//import { ReactiveDict } from 'meteor/reactive-dict';
        
import './mt-input.html';     
    
/*  
 * Lifecycles
 */   
  
Template.MtInput.onCreated(function() {
  let instance = this;

  instance.autorun(function () {
    let data = Template.currentData();

    //simple schema does not support input value type of: number or string together.
    data = R.dissoc('inputValue', data);
    
    instance.autorun(function () {
      new SimpleSchema({
        inputType: { type: String }, 
        classStr: { type: String, optional: true },
        placeholder: { type: String, optional: true },
        isDisabled: { type: Boolean, optional: true },
        onInput: { type: Object, blackbox: true },
      }).validate(data);
    });
  });

  instance.autorun(function () {
    let data = Template.currentData();

    instance.onInput = function (value) {
      R.when(R.pipe(R.isNil, R.not), x => x(value))(R.path(['onInput', 'fn'], data));    
    };
  });
});  

/*
Template.MtInput.rendered = function() {
};  
*/

/*
 * Events
 */

Template.MtInput.events({
  'input .input-field': function (event, instance) {
    if (event.target.type === 'checkbox') { return; }
    
    let value = R.cond([
      [R.equals('number'), R.always(event.target.valueAsNumber)],
      [R.T, R.always(event.target.value)],
    ])(event.target.type);
    
    instance.onInput(value);
  },

  'click .input-field': function (event, instance) {
    if (event.target.type !== 'checkbox') { return; }

    let element = instance.$('.input-field')[0];
    instance.onInput(element.checked);
  }
});
   
/*  
 * Helpers
 */

Template.MtInput.helpers({    
  attrsInput: function (inputType, placeholder, isDisabled) {
    let attrs = {};

    if (hasPlaceholder(inputType, placeholder)) {
      attrs = R.assoc('placeholder', placeholder, attrs);
    }

    if (isDisabled) {
      attrs = R.assoc('disabled', 'disabled', attrs);
    }

    return attrs;
  },

}); // end: helpers

function hasPlaceholder(inputType, placeholder) {
  if (R.contains(inputType, ['checkbox', 'select'])) {
    return false;
  }

  if (R.isNil(placeholder)) {
    return false;
  }

  return true;
}