Rizwan Haider | 8e5f477 | 2016-08-17 18:04:35 -0400 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 6/27/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | angular.module('xos.ecordTopology') |
| 12 | .directive('elineForm', function(){ |
| 13 | return { |
| 14 | restrict: 'E', |
| 15 | scope: { |
| 16 | uni: '=' |
| 17 | }, |
| 18 | bindToController: true, |
| 19 | controllerAs: 'vm', |
| 20 | templateUrl: 'templates/eline-form.tpl.html', |
| 21 | controller: function ($scope, $timeout, $location, _, Uni, Eline) { |
| 22 | // FORM HELPERS |
| 23 | const isUniSelected = (uni, list) => { |
| 24 | return _.findIndex(list, {id: uni.id, selected: true}) === -1 ? false : true; |
| 25 | }; |
| 26 | |
| 27 | const deselectOtherUni = (uni, list) => { |
| 28 | list.map(u => { |
| 29 | if(u.id !== uni.id){ |
| 30 | u.selected = false; |
| 31 | u.alreadySelected = false; |
| 32 | } |
| 33 | return u; |
| 34 | }) |
| 35 | }; |
| 36 | |
| 37 | this.selectUni = (uni, position) => { |
| 38 | if(uni.selected){ |
| 39 | |
| 40 | deselectOtherUni(uni, this[`${position}Unis`]); |
| 41 | |
| 42 | // need to check if is selected in the other list |
| 43 | const list = position === 'start' ? 'end':'start'; |
| 44 | |
| 45 | if(isUniSelected(uni, this[`${list}Unis`])){ |
| 46 | return uni.alreadySelected = true; |
| 47 | } |
| 48 | this.formErrors[`${position}Error`] = null; |
| 49 | return this.el[position] = uni; |
| 50 | } |
| 51 | |
| 52 | this.el[position] = null; |
| 53 | return uni.alreadySelected = false; |
| 54 | }; |
| 55 | // END FORM HELPERS |
| 56 | |
| 57 | this.el = {}; |
| 58 | |
| 59 | Uni.query().$promise |
| 60 | .then((unis) => { |
| 61 | // TODO we were mapping UNIS to name, location. Bring that back to life! |
| 62 | this.startUnis = angular.copy(unis); |
| 63 | this.endUnis = angular.copy(unis); |
| 64 | this.infrastructureUnis = angular.copy(unis); |
| 65 | }); |
| 66 | |
| 67 | const createEline = (el) => { |
| 68 | |
| 69 | // NOTE: |
| 70 | // name and latlng have been added to request, will XOS manage them? |
| 71 | |
| 72 | let formatted = { |
| 73 | adminstate : 'activationrequested', |
| 74 | operstate : 'active', |
| 75 | uni1 : {'id' : el.start.id}, |
| 76 | uni2 : {'id' : el.end.id}, |
| 77 | sid: el.evcCfgidentifier, |
| 78 | type : 'Point_To_Point', |
| 79 | }; |
| 80 | |
| 81 | return formatted; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | this.formErrors = {}; |
| 86 | this.createEline = (el, form) => { |
| 87 | |
| 88 | if(!el.start){ |
| 89 | this.formErrors.startError = 'Select a starting point' |
| 90 | } |
| 91 | |
| 92 | if(!el.end){ |
| 93 | this.formErrors.endError = 'Select an ending point' |
| 94 | } |
| 95 | |
| 96 | if(!el.start || !el.end){ |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | let eline = createEline(el); |
| 101 | |
| 102 | Eline.save(eline).$promise |
| 103 | .then((res) => { |
| 104 | form.$saved = true; |
| 105 | $scope.$emit('elan.created', res); |
| 106 | //cordConfig.pages.push(res); |
| 107 | |
| 108 | $timeout(() => { |
| 109 | $location.path('/'); |
| 110 | }, 1000); |
| 111 | }) |
| 112 | .catch(e => { |
| 113 | throw new Error(e); |
| 114 | }); |
| 115 | }; |
| 116 | |
| 117 | $scope.$watch(() => this.el, (val, oldval) => { |
| 118 | if(val !== oldval && this.eline.$saved){ |
| 119 | this.eline.$saved = false; |
| 120 | } |
| 121 | }, true); |
| 122 | |
| 123 | this.prepareInfrastructure = (el_prefix, unis) => { |
| 124 | |
| 125 | let i = 0; |
| 126 | |
| 127 | const builElines = (elements) => { |
| 128 | |
| 129 | let elines = []; |
| 130 | |
| 131 | let firstEl = elements.shift(); |
| 132 | |
| 133 | let newElines = elements.reduce((list, end) => { |
| 134 | let el = {}; |
| 135 | // prepare e-line as the form |
| 136 | el.evcCfgidentifier = `${el_prefix}-${++i}`; |
| 137 | el.start = firstEl; |
| 138 | el.end = end; |
| 139 | list.push(createEline(el)) |
| 140 | return list; |
| 141 | }, []); |
| 142 | |
| 143 | elines = elines.concat(...newElines); |
| 144 | |
| 145 | if(elements.length === 1){ |
| 146 | |
| 147 | return elines; |
| 148 | } |
| 149 | else { |
| 150 | return elines.concat(...builElines(elements)); |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | return builElines(unis); |
| 156 | } |
| 157 | |
| 158 | this.createInfrastructure = (unis) => { |
| 159 | |
| 160 | unis = _.filter(unis, {selected: true}); |
| 161 | $log.info('Send request to MARC!!! - Decide the format!', unis); |
| 162 | |
| 163 | let promises = []; |
| 164 | |
| 165 | this.prepareInfrastructure('test', unis).forEach((eline) => { |
| 166 | promises.push(Eline().save(eline).$promise) |
| 167 | }); |
| 168 | |
| 169 | $q.all(promises) |
| 170 | .then((res) => { |
| 171 | res.forEach(eline => { |
| 172 | $scope.$emit('elan.created', eline); |
| 173 | cordConfig.pages.push(eline); |
| 174 | }); |
| 175 | |
| 176 | $timeout(() => { |
| 177 | $location.path('/home'); |
| 178 | }, 1000); |
| 179 | }); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | }) |
| 184 | })(); |
| 185 | |