cms/app-client/app/routes/application.js
author ymh <ymh.work@gmail.com>
Sat, 10 Jun 2017 08:33:03 +0200
changeset 532 1190ea937f2d
parent 414 5c6c526a7fc1
permissions -rw-r--r--
make things work after node 8, npm 5 migration. Migrate to lodash 4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     1
import Ember from 'ember';
532
1190ea937f2d make things work after node 8, npm 5 migration. Migrate to lodash 4
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
     2
import _ from 'lodash';
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
     3
import RSVP from 'rsvp';
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
     4
import ENV from 'app-client/config/environment';
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
     5
import URI from 'urijs';
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     6
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
     7
export default Ember.Route.extend({
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
     8
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
     9
    filter: Ember.inject.service(),
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    10
    constants: Ember.inject.service(),
344
70451a4dc9ae Pagination playlist
Chloe Laisne <chloe.laisne@gmail.com>
parents: 204
diff changeset
    11
414
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    12
    model: function(params, transition) {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    13
        if(transition.targetName === 'document') {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    14
            let documentId = transition.params['document']['doc_id'];
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    15
            return this.store.query('document', {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    16
              id: documentId,
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    17
              page: 1,
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    18
              perpage: this.get('constants').DOCUMENTS_PAGINATION_PERPAGE
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    19
            });
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    20
        } else {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    21
            let filterQueryArgs = _.clone(this.get('filter').get('queryParamsValues'));
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    22
            return this.store.query('document', _.merge(filterQueryArgs, {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    23
                page: params['page'],
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    24
                perpage: this.get('constants').DOCUMENTS_PAGINATION_PERPAGE
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    25
            }));
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    26
        }
401
9ff56cc0c656 reset pagination when filter change, translate player button
ymh <ymh.work@gmail.com>
parents: 394
diff changeset
    27
    },
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    28
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    29
    /**
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    30
      Serializes value of the query parameter based on defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    31
      @method serializeQueryParam
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    32
      @param {Object} value
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    33
      @param {String} urlKey
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    34
      @param {String} defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    35
      @private
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    36
    */
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    37
    serializeQueryParam(value, urlKey, defaultValueType) {
532
1190ea937f2d make things work after node 8, npm 5 migration. Migrate to lodash 4
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    38
        if(_.includes(this.get('filter').get('queryParams'), urlKey)) {
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    39
            return this.get('filter').serializeQueryParam(value, urlKey, defaultValueType);
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    40
        }
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    41
        return this._super(value, urlKey, defaultValueType);
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    42
    },
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    43
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    44
    /**
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    45
      Deserializes value of the query parameter based on defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    46
      @method deserializeQueryParam
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    47
      @param {Object} value
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    48
      @param {String} urlKey
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    49
      @param {String} defaultValueType
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    50
      @private
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    51
    */
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    52
    deserializeQueryParam(value, urlKey, defaultValueType) {
532
1190ea937f2d make things work after node 8, npm 5 migration. Migrate to lodash 4
ymh <ymh.work@gmail.com>
parents: 414
diff changeset
    53
        if(_.includes(this.get('filter').get('queryParams'), urlKey)) {
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    54
            return this.get('filter').deserializeQueryParam(value, urlKey, defaultValueType);
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    55
        }
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    56
        return this._super(value, urlKey, defaultValueType);
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    57
    },
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    58
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    59
    beforeModel: function(transition) {
414
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    60
        return new RSVP.Promise((resolve) => {
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    61
          // succeed
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    62
          this.get('filter').setProperties(transition['queryParams']);
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    63
          resolve();
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    64
        });
414
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    65
        //return this._super(...arguments);
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    66
    },
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    67
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    68
    queryParams: Ember.computed('filter', function() {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    69
        var res = this.get('filter').getRouteQueryParams();
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    70
        res['page'] = { refreshModel: true };
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    71
        res['notice'] = { refreshModel: false };
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    72
        return res;
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    73
    }),
392
4fbe94af93e8 Improve filter service. Centralize filter management in a single point
ymh <ymh.work@gmail.com>
parents: 344
diff changeset
    74
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    75
    actions: {
404
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    76
        loading(transition) {
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    77
          let controller = this.controllerFor('application');
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    78
          controller.set('isLoading', true);
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    79
          transition.promise.finally(function() {
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    80
              controller.set('isLoading', false);
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    81
          });
0a5eef6ad2fe correction, change colors for discourse categories, add loding spinner
ymh <ymh.work@gmail.com>
parents: 401
diff changeset
    82
        },
414
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    83
        willTransition: function(transition) {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    84
            if(this.controller.currentPath === 'document' || transition.targetName === 'document') {
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    85
                this.refresh();
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    86
            }
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    87
            return true;
5c6c526a7fc1 Display for single notice, preparation for share link
ymh <ymh.work@gmail.com>
parents: 404
diff changeset
    88
        },
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    89
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    90
        didTransition: function() {
199
b7c691c6179d Set location filter label
Chloe Laisne <chloe.laisne@gmail.com>
parents: 194
diff changeset
    91
            // Append body classname depending on the route
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    92
            Ember.$('body').removeClass((this.controller.get('currentPath') || '').replace(/\//g, '-').dasherize());
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    93
            Ember.run.once(this, function() {
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    94
                Ember.$('body').addClass((this.controller.get('currentPath') ||'').replace(/\//g, '-').dasherize());
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
    95
            });
394
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    96
            // change external navigations links
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    97
            if(ENV.APP.navigationLinksSelector) {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    98
                return new RSVP.Promise((resolve) => {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
    99
                  setTimeout(() => {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   100
                    let hash = window.location.hash;
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   101
                    if(!hash) {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   102
                        return true;
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   103
                    }
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   104
                    let uriHash = URI(hash.substr(1));
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   105
                    Ember.$(ENV.APP.navigationLinksSelector).each((i, l) => {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   106
                        let uri = URI(Ember.$(l).prop('href'));
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   107
                        if(uri.fragment()) {
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   108
                          let urifragment = URI(uri.fragment()).search(uriHash.search());
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   109
                          Ember.$(l).prop('href', uri.fragment(urifragment.href()).href());
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   110
                        }
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   111
                    });
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   112
                    resolve(true);
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   113
                  }, 10);
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   114
                });
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   115
            }
48458e099b05 make dynamic filters for all route and do some code pruning and cleaning
ymh <ymh.work@gmail.com>
parents: 392
diff changeset
   116
            return true;
194
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
   117
        }
02c6aa9a99d7 Linting errors / Parenthesis missing / Body classname
Chloe Laisne <chloe.laisne@gmail.com>
parents: 92
diff changeset
   118
81
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
   119
    }
848e4a5ad4d9 update date params to be an array of date instead of a string
nowmad@nowmads-macbook-pro.local
parents: 74
diff changeset
   120
42
7d091abf82fd add application adapter to simulate REST, using fixtures
nowmad@nowmads-macbook-pro.local
parents:
diff changeset
   121
});