cms/app-client/app/components/discourses-component.js
author ymh <ymh.work@gmail.com>
Sun, 04 Dec 2016 00:23:52 +0100
changeset 465 9df336529b2f
parent 404 0a5eef6ad2fe
child 466 a8effb60ccb6
permissions -rw-r--r--
make filter and theme visualisatiion change on filter modification
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     1
import Ember from 'ember';
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     2
import d3 from 'd3';
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
     3
import _ from 'lodash/lodash';
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     4
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     5
export default Ember.Component.extend({
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
     6
206
e038d5782d06 Discourses component: Modify label styles / Styling to SCSS / Cursor pointer
Chloe Laisne <chloe.laisne@gmail.com>
parents: 198
diff changeset
     7
    classNames: ['discourses-component'],
e038d5782d06 Discourses component: Modify label styles / Styling to SCSS / Cursor pointer
Chloe Laisne <chloe.laisne@gmail.com>
parents: 198
diff changeset
     8
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
     9
    constants: Ember.inject.service(),
197
7b266ccf6d3d Setup filter service
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    10
    filter: Ember.inject.service(),
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    11
292
df030ddd4eca Convert discourses pack circles to div + Highlight select filter
Chloe Laisne <chloe.laisne@gmail.com>
parents: 286
diff changeset
    12
    discourseObserver: Ember.observer('filter.discourse', function() {
df030ddd4eca Convert discourses pack circles to div + Highlight select filter
Chloe Laisne <chloe.laisne@gmail.com>
parents: 286
diff changeset
    13
        Ember.$('.item').removeClass("selected");
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    14
        if(this.get('filter').get('discourse') !== null) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    15
            this.get('filter').get('discourse').forEach(function(e) {
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    16
                Ember.$('.item[data-id="' + e + '"]').addClass("selected");
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    17
            });
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    18
        }
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 362
diff changeset
    19
292
df030ddd4eca Convert discourses pack circles to div + Highlight select filter
Chloe Laisne <chloe.laisne@gmail.com>
parents: 286
diff changeset
    20
    }).on('init'),
df030ddd4eca Convert discourses pack circles to div + Highlight select filter
Chloe Laisne <chloe.laisne@gmail.com>
parents: 286
diff changeset
    21
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    22
    didRender: function() {
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    23
        var self = this;
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 216
diff changeset
    24
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    25
        var discourses = this.get('discourses');
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    26
        var array = discourses.map(function (d) { return d.get('count'); });
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    27
        var oldMin = Math.min(...array),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    28
            oldMax = Math.max(...array);
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    29
        var sum = array.reduce(function(a, b) { return a + b; });
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    30
        var average = sum / array.length;
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    31
        var newMin = Math.floor((average - oldMin)),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    32
            newMax = Math.floor((oldMax - average));
265
5995d360e6ac Discourse bubbles radius range + font size
Chloe Laisne <chloe.laisne@gmail.com>
parents: 261
diff changeset
    33
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    34
        var width = self.$().parent().width();
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    35
        var height = self.$().parent().height() - self.$().siblings().outerHeight(true);
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    36
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    37
        var bubble = d3.layout.pack()
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    38
            .sort(function comparator(a, b) { return a.value + b.value; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    39
            .size([width, height])
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    40
            .value(function(d){
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    41
                return Math.floor((((d.value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin);
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    42
            })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    43
            .padding(10);
181
b8a783ca6c4a Bubble paddings, sizes, and captions
Chloe Laisne <chloe.laisne@gmail.com>
parents: 178
diff changeset
    44
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    45
        var element = d3.select('#' + self.get('elementId'));
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    46
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    47
        var bubbles = bubble.nodes(self.createNodes());
181
b8a783ca6c4a Bubble paddings, sizes, and captions
Chloe Laisne <chloe.laisne@gmail.com>
parents: 178
diff changeset
    48
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    49
        var nodes = element
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    50
            .selectAll()
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    51
            .data(bubbles);
181
b8a783ca6c4a Bubble paddings, sizes, and captions
Chloe Laisne <chloe.laisne@gmail.com>
parents: 178
diff changeset
    52
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    53
        nodes.enter().append("div")
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    54
            .attr("class", function(d) { return ( d.children ? "category": "item" ) + ( (self.get('filter').get('discourse') !== null && _.contains(self.get('filter').get('discourse'), d.id)) ? " selected" : "" ) ; });
183
cc8b022088c8 Bubble click area + Linting
Chloe Laisne <chloe.laisne@gmail.com>
parents: 181
diff changeset
    55
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    56
        var item = element.selectAll(".item")
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    57
            .attr("data-id", function(d) { return d.id; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    58
            .style("left", function(d) { return ( d.x - d.r)  + "px"; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    59
            .style("top", function(d) { return ( d.y - d.r)  + "px"; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    60
            .style("width", function(d) { return (d.r * 2) + "px"; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    61
            .style("height", function(d) { return (d.r * 2) + "px"; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    62
            .style("background-color", function(d) { return d.fill; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    63
            .style("border-color", function(d) { return d.stroke; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    64
            .style("font-size", function(d) { return Math.floor((((d.value - oldMin) * (13 - 10)) / (oldMax - oldMin)) + 10) + 'px'; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    65
            .on('click', function(d) {
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    66
                self.get('filter').setFilter('discourse', d.id);
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    67
            });
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 216
diff changeset
    68
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    69
        item.append("span")
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    70
            .html(function(d) { return d.name + ' <span class="count">(' + d.count + ')</span>'; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    71
            .style("margin-left", function() { return ( Ember.$(this).width() > Ember.$(this).parent().width() ? - ( Ember.$(this).width() / 2 ) + ( Ember.$(this).parent().width() / 2 ) : 0 ) + 'px'; })
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    72
            .style("margin-top", function() { return Ember.$(this).parent().height() / 2 - Ember.$(this).height() / 2 + 'px'; });
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    73
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    74
        this._super(...arguments);
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    75
    },
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    76
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    77
    createNodes: function() {
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    78
        var self = this;
310
b1f9bcfda379 Change discourse layout to spread bubbles accross
Chloe Laisne <chloe.laisne@gmail.com>
parents: 292
diff changeset
    79
        var children = [];
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    80
        var discourses = this.get('discourses');
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    81
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    82
        discourses.forEach(function(discourse) {
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    83
            var category_key = self.get('constants').DISCOURSE_CATEGORY_KEYS[discourse.get('id')];
185
191883bd16a6 Set ID as discourse querystring value
Chloe Laisne <chloe.laisne@gmail.com>
parents: 184
diff changeset
    84
            var category_value = self.get('constants').DISCOURSE_CATEGORY_VALUES[category_key];
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    85
310
b1f9bcfda379 Change discourse layout to spread bubbles accross
Chloe Laisne <chloe.laisne@gmail.com>
parents: 292
diff changeset
    86
            children.push({
465
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    87
                id: discourse.get('id'),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    88
                name: discourse.get('label'),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    89
                value: discourse.get('count'),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    90
                count: discourse.get('count'),
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    91
                fill: category_value.fill,
9df336529b2f make filter and theme visualisatiion change on filter modification
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    92
                stroke: category_value.stroke
265
5995d360e6ac Discourse bubbles radius range + font size
Chloe Laisne <chloe.laisne@gmail.com>
parents: 261
diff changeset
    93
            });
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    94
        });
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    95
310
b1f9bcfda379 Change discourse layout to spread bubbles accross
Chloe Laisne <chloe.laisne@gmail.com>
parents: 292
diff changeset
    96
        return { 'children': children };
176
d1baf7ccecc8 Add `thematiques` components
Chloe Laisne <chloe.laisne@gmail.com>
parents: 175
diff changeset
    97
    }
261
02e2396bcbbc Migrate to ember 2.7 + correct jquery null context error + declare shim for popcorn (instead of silencing the JSHint error)
ymh <ymh.work@gmail.com>
parents: 216
diff changeset
    98
175
7a7cfcba5bfe Add Discourses Component files
Chloe Laisne <chloe.laisne@gmail.com>
parents:
diff changeset
    99
});