|
1 | 1 | var View = require('ampersand-view');
|
2 |
| -var TypeListItem = require('./type-list-item'); |
| 2 | +var format = require('util').format; |
| 3 | +var numeral = require('numeral'); |
| 4 | +var tooltipMixin = require('../tooltip-mixin'); |
| 5 | +var _ = require('lodash'); |
| 6 | +// var debug = require('debug')('scout:field-list:type-list'); |
3 | 7 |
|
4 |
| -module.exports = View.extend({ |
| 8 | +var TypeListView; |
| 9 | + |
| 10 | +var TypeListItem = View.extend(tooltipMixin, { |
| 11 | + template: require('./type-list-item.jade'), |
| 12 | + namespace: 'TypeListItem', |
| 13 | + bindings: { |
| 14 | + 'model.name': [ |
| 15 | + { |
| 16 | + hook: 'name' |
| 17 | + }, |
| 18 | + { |
| 19 | + hook: 'bar', |
| 20 | + type: function(el) { |
| 21 | + el.classList.add('schema-field-type-' + this.model.getId().toLowerCase()); |
| 22 | + } |
| 23 | + } |
| 24 | + ], |
| 25 | + probability_percentage: { |
| 26 | + hook: 'bar', |
| 27 | + type: function(el) { |
| 28 | + el.style.width = this.probability_percentage; |
| 29 | + } |
| 30 | + }, |
| 31 | + tooltip_message: { |
| 32 | + type: function() { |
| 33 | + // need to set `title` and `data-original-title` due to bug in bootstrap's tooltip |
| 34 | + // @see https://github.com/twbs/bootstrap/issues/14769 |
| 35 | + this.tooltip({ |
| 36 | + title: this.tooltip_message, |
| 37 | + placement: this.hasSubtype ? 'bottom' : 'top' |
| 38 | + }).attr('data-original-title', this.tooltip_message); |
| 39 | + } |
| 40 | + } |
| 41 | + }, |
| 42 | + props: { |
| 43 | + parent: 'state' |
| 44 | + }, |
| 45 | + derived: { |
| 46 | + probability_percentage: { |
| 47 | + deps: ['model.probability'], |
| 48 | + fn: function() { |
| 49 | + return numeral(this.model.probability).format('0.00%'); |
| 50 | + } |
| 51 | + }, |
| 52 | + tooltip_message: { |
| 53 | + deps: ['model.probability'], |
| 54 | + fn: function() { |
| 55 | + return format('%s (%s)', this.model.getId(), numeral(this.model.probability).format('%')); |
| 56 | + } |
| 57 | + }, |
| 58 | + hasSubtype: { |
| 59 | + deps: ['parent'], |
| 60 | + fn: function() { |
| 61 | + return this.parent.hasSubtype; |
| 62 | + } |
| 63 | + } |
| 64 | + }, |
| 65 | + events: { |
| 66 | + 'click .schema-field-wrapper': 'typeClicked' |
| 67 | + }, |
| 68 | + subviews: { |
| 69 | + subtypes: { |
| 70 | + hook: 'array-subtype-subview', |
| 71 | + waitFor: 'model.types', |
| 72 | + prepareView: function(el) { |
| 73 | + return new TypeListView({ |
| 74 | + el: el, |
| 75 | + parent: this, |
| 76 | + hasSubtype: true, |
| 77 | + collection: this.model.types |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + typeClicked: function(evt) { |
| 83 | + evt.stopPropagation(); |
| 84 | + |
| 85 | + // no clicks on Undefined allowed |
| 86 | + if (this.model.getId() === 'Undefined') return; |
| 87 | + |
| 88 | + // find the field view, at most 2 levels up |
| 89 | + var fieldView = this.parent.parent; |
| 90 | + if (fieldView.getType() !== 'FieldView') { |
| 91 | + fieldView = fieldView.parent.parent; |
| 92 | + } |
| 93 | + |
| 94 | + // if type model has changed, render its minichart |
| 95 | + if (fieldView.type_model !== this.model) { |
| 96 | + fieldView.type_model = this.model; |
| 97 | + fieldView.renderMinicharts(); |
| 98 | + } |
| 99 | + }, |
| 100 | + render: function() { |
| 101 | + this.renderWithTemplate(this); |
| 102 | + } |
| 103 | +}); |
| 104 | + |
| 105 | + |
| 106 | +TypeListView = module.exports = View.extend({ |
| 107 | + props: { |
| 108 | + hasSubtype: { |
| 109 | + type: 'boolean', |
| 110 | + default: false |
| 111 | + } |
| 112 | + }, |
5 | 113 | template: require('./type-list.jade'),
|
6 | 114 | render: function() {
|
7 |
| - this.renderWithTemplate(); |
8 |
| - this.renderCollection(this.collection, TypeListItem, this.queryByHook('types')); |
| 115 | + if (!_.get(this, 'parent.hasSubtype')) { |
| 116 | + this.renderWithTemplate(this); |
| 117 | + this.renderCollection(this.collection, TypeListItem, this.queryByHook('types')); |
| 118 | + } |
9 | 119 | }
|
10 | 120 | });
|
0 commit comments