aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/select-model/select-model.js
blob: 01fca9c9c336426006e4fd99d3f64a4bfe3b35bf (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
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
//                                                                                      /
// All rights reserved. This program and the accompanying materials                     /
// are made available under the terms of the Apache License, Version 2.0                /
// which accompanies this distribution, and is available at                             /
// http://www.apache.org/licenses/LICENSE-2.0                                           /
/////////////////////////////////////////////////////////////////////////////////////////
/*
 * Template Component: SelectModel 
 */
    
//import { Meteor } from 'meteor/meteor'; 
import { Template } from 'meteor/templating';
import * as R from 'ramda';
//import { ReactiveDict } from 'meteor/reactive-dict';
        
import './select-model.html';     
    
/*  
 * Lifecycles
 */   
  
Template.SelectModel.onCreated(function() {
});  

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

/*
 * Events
 */

Template.SelectModel.events({
  'change .js-select': function (event) {
    event.stopPropagation();
    event.preventDefault();

    let instance = Template.instance();
    // Extract string values from select element's attribute.
    let elementSelectedValues = R.map(function (optionEl) {
      return optionEl.value;
    }, event.target.selectedOptions);

    let selectedValues = instance.data.multi ? elementSelectedValues : 
      elementSelectedValues[0];

    if (instance.data.setModel) {
      instance.data.setModel(selectedValues);
    }
  }
});
   
/*  
 * Helpers
 */

Template.SelectModel.helpers({    
  isSelected: function (optionValue) {
    let instance = Template.instance();
    let selectedValues = instance.data.values;

    if (R.isNil(selectedValues)) { return false; }
    return R.contains(optionValue, selectedValues);
  },
  markIfDisabled: function () {
    let instance = Template.instance();
    let attrs = {};
    if (instance.data.disabled) {
      attrs = R.assoc('disabled', true, attrs);
    }

    return attrs;
  }
});