aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/selectable-ordered-input/selectable-ordered-input.js
blob: 15c46fe10a16a0cff6eba0783b5bc9f0c99ed523 (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
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
/*
 * Template Component: SelectableOrderedInput 
 */
    
//import { Meteor } from 'meteor/meteor'; 
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import * as R from 'ramda';
        
import './selectable-ordered-input.html';     
    
/*  
 * Lifecycles
 */   
  
Template.SelectableOrderedInput.onCreated(function() {
  let instance = this;
  instance.state = new ReactiveDict();
  instance.state.setDefault({
    choices: [],
    currentChoices: [],
    currentProduct: [],
    selectedChoice: null,
    selectedProductOption: null,
  });

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

    new SimpleSchema({
      choices: { type: [Object], blackbox: true },
      product: { type: [Object], blackbox: true },
      onProductChange: { type: Function },
    }).validate(data);

    instance.state.set('choices', data.choices);
    instance.state.set('product', data.product);
    instance.onProductChange = R.defaultTo((_x) => {}, data.onProductChange);
  });

  instance.autorun(function () {
    let choices = instance.state.get('choices');
    let product = instance.state.get('product');

    let currentChoices = R.differenceWith((choice, product) => choice.value === product.value, 
      choices, product);

    instance.state.set('currentChoices', currentChoices);
    instance.state.set('currentProduct', product);
    instance.state.set('selectedChoice', null);
    instance.state.set('selectedProductOption', null);
  });

  let lastCurrentProduct = null;
  instance.autorun(function () {
    let currentProduct = instance.state.get('currentProduct');
    if (R.isNil(lastCurrentProduct)) {
      lastCurrentProduct = currentProduct;
      return;
    }

    if (R.equals(lastCurrentProduct, currentProduct)) {
      return; 
    }

    lastCurrentProduct = currentProduct;
    instance.onProductChange(currentProduct);
  });

});  

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

/*
 * Events
 */

Template.SelectableOrderedInput.events({
  'click .sm-add-choice-btn': function (event, instance) {
    event.preventDefault();

    moveSelectedChoiceToProduct(instance);
  },

  'click .sm-remove-choice-btn': function (event, instance) {
    event.preventDefault();

    moveSelectedProductOptionToChoices(instance);
  },

  'click .sm-move-up-product-btn': function (event, instance) {
    event.preventDefault();

    moveSelectedProductOptionUp(instance); 
  },

  'click .sm-move-down-product-btn': function (event, instance) {
    event.preventDefault();

    moveSelectedProductOptionDown(instance); 
  },
});
   
/*  
 * Helpers
 */

Template.SelectableOrderedInput.helpers({    
  argsChoicesSelect: function (choices, selectedValue) {
    let instance = Template.instance();

    return {
      classStr: 'cl-input',
      //isDisabled:,
      selectedValue: selectedValue,
      size: 7,
      options: choices,
      onInput: {
        fn: function (choice) {
          let choices = instance.state.get('currentChoices');
          let fullChoice = R.find(R.propEq('value', choice), choices); 
          instance.state.set('selectedChoice', fullChoice);
        }
      },
    };
  },

  currentChoices: function () {
    let instance = Template.instance();
    return instance.state.get('currentChoices');
  },

  argsProductSelect: function (currentProduct, selectedProductOptValue) {
    let instance = Template.instance();

    return {
      classStr: 'cl-input',
      //isDisabled:,
      selectedValue: selectedProductOptValue,
      size: 7,
      options: currentProduct,
      onInput: {
        fn: function (productOption) {
          let product = instance.state.get('currentProduct');
          let fullProductOption = R.find(R.propEq('value', productOption), product); 
          instance.state.set('selectedProductOption', fullProductOption);
        }
      },
    };
  },

  currentProduct: function () {
    let instance = Template.instance();
    return instance.state.get('currentProduct');
  },

  selectedChoiceValue: function () {
    let instance = Template.instance();
    return R.path(['value'], instance.state.get('selectedChoice'));
  },

  selectedProductOptValue: function () {
    let instance = Template.instance();
    return R.path(['value'], instance.state.get('selectedProductOption'));
  },
}); // end: helpers

function moveSelectedChoiceToProduct(instance) {
  let selectedChoice = instance.state.get('selectedChoice');
  if (R.isNil(selectedChoice)) { return; }

  // remove selected choice from choices.
  let choices = instance.state.get('currentChoices');
  choices = R.reject(R.propEq('value', selectedChoice.value), choices);
  instance.state.set('currentChoices', choices);
  
  // add selected choice to product.
  let product = instance.state.get('currentProduct');
  product = R.append(selectedChoice, product);
  instance.state.set('currentProduct', product);

  // clear selected choice
  instance.state.set('selectedChoice', null);
}

function moveSelectedProductOptionToChoices(instance) {
  let selectedProductOption = instance.state.get('selectedProductOption');
  if (R.isNil(selectedProductOption)) { return; }

  // remove selected option from product
  let product = instance.state.get('currentProduct');
  product = R.reject(R.propEq('value', selectedProductOption.value), product);
  instance.state.set('currentProduct', product);

  // add selected option to choices
  let choices = instance.state.get('currentChoices');
  choices = R.append(selectedProductOption, choices);
  instance.state.set('currentChoices', choices);

  // clear selection product option
  instance.state.set('selectedProductOption', null);
}

function moveSelectedProductOptionUp(instance) {
  // get selected product option. exit if null.
  let selectedProductOption = instance.state.get('selectedProductOption');
  if (R.isNil(selectedProductOption)) { return; }

  // move product option up.
  let product = instance.state.get('currentProduct');
  let index = R.findIndex(R.propEq('value', selectedProductOption.value), product);
  if (index === 0) { return; }
  product = R.remove(index, 1, product);
  product = R.insert(index - 1, selectedProductOption, product);
  instance.state.set('currentProduct', product);
}

function moveSelectedProductOptionDown(instance) {
  // get selected product option. exit if null.
  let selectedProductOption = instance.state.get('selectedProductOption');
  if (R.isNil(selectedProductOption)) { return; }

  // move product option down.
  let product = instance.state.get('currentProduct');
  let index = R.findIndex(R.propEq('value', selectedProductOption.value), product);
  if (index === product.length - 1) { return; }

  product = R.remove(index, 1, product);
  product = R.insert(index + 1, selectedProductOption, product);
  instance.state.set('currentProduct', product);
}