Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | angular.module('xos.UITutorial') |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 4 | .service('ResponseHandler', function(TemplateHandler){ |
| 5 | |
| 6 | const exclude = [ |
| 7 | 'deleted', |
| 8 | 'enabled', |
| 9 | 'enacted', |
| 10 | 'exposed_ports', |
| 11 | 'lazy_blocked', |
| 12 | 'created', |
| 13 | 'validators', |
| 14 | 'controllers', |
| 15 | 'backend_status', |
| 16 | 'backend_register', |
| 17 | 'policed', |
| 18 | 'no_policy', |
| 19 | 'write_protect', |
| 20 | 'no_sync', |
| 21 | 'updated' |
| 22 | ]; |
| 23 | |
| 24 | this.parseObject = (obj, comma = '') => { |
| 25 | obj = _.omit(obj, exclude); |
| 26 | return TemplateHandler.jsonObject({'obj': obj, comma: comma}); |
| 27 | }; |
| 28 | |
| 29 | this.parseCollection = (array) => { |
| 30 | array = array.map((o, i) => `${this.parseObject(o, i === (array.length - 1) ? '':',')}`); |
| 31 | return TemplateHandler.jsonCollection({'collection': array}); |
| 32 | }; |
| 33 | |
| 34 | this.parse = (res, jsCode, done) => { |
| 35 | if(_.isArray(res)) { |
| 36 | res = this.parseCollection(res); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 37 | } |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 38 | else{ |
| 39 | res = this.parseObject(res); |
| 40 | } |
| 41 | done(TemplateHandler.resourcesResponse({ |
| 42 | jsCode: jsCode, |
| 43 | res: res |
| 44 | })); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 45 | }; |
| 46 | }); |
| 47 | })(); |