Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 3/24/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular.module('xos.uiComponents') |
| 13 | |
| 14 | /** |
| 15 | * @ngdoc directive |
| 16 | * @name xos.uiComponents.directive:xosSmartTable |
| 17 | * @restrict E |
| 18 | * @description The xos-table directive |
| 19 | * @param {Object} config The configuration for the component. |
| 20 | * @scope |
| 21 | * @example |
| 22 | */ |
| 23 | |
| 24 | .directive('xosSmartTable', function(){ |
| 25 | return { |
| 26 | restrict: 'E', |
| 27 | scope: { |
| 28 | config: '=' |
| 29 | }, |
| 30 | template: ` |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 31 | <div class="row" ng-show="vm.data.length > 0"> |
| 32 | <div class="col-xs-12 text-right"> |
| 33 | <a href="" class="btn btn-success" ng-click="vm.createItem()"> |
| 34 | Add |
| 35 | </a> |
| 36 | </div> |
| 37 | </div> |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 38 | <xos-table config="vm.tableConfig" data="vm.data"></xos-table> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 39 | <div class="panel panel-default" ng-show="vm.detailedItem"> |
| 40 | <div class="panel-heading"> |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 41 | <div class="row"> |
| 42 | <div class="col-xs-11"> |
| 43 | <h3 class="panel-title">Update {{vm.config.resource}} {{vm.detailedItem.id}}</h3> |
| 44 | </div> |
| 45 | <div class="col-xs-1"> |
| 46 | <a href="" ng-click="vm.cleanForm()"> |
| 47 | <i class="glyphicon glyphicon-remove pull-right"></i> |
| 48 | </a> |
| 49 | </div> |
| 50 | </div> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 51 | </div> |
| 52 | <div class="panel-body"> |
| 53 | <xos-form config="vm.formConfig" ng-model="vm.detailedItem"></xos-form> |
| 54 | </div> |
| 55 | </div> |
| 56 | <xos-alert config="{type: 'success', closeBtn: true}" show="vm.responseMsg">{{vm.responseMsg}}</xos-alert> |
| 57 | <xos-alert config="{type: 'danger', closeBtn: true}" show="vm.responseErr">{{vm.responseErr}}</xos-alert> |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 58 | `, |
| 59 | bindToController: true, |
| 60 | controllerAs: 'vm', |
| 61 | controller: function($injector, LabelFormatter, _){ |
| 62 | |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 63 | // NOTE |
| 64 | // Corner case |
| 65 | // - if response is empty, how can we generate a form ? |
| 66 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 67 | this.responseMsg = false; |
| 68 | this.responseErr = false; |
| 69 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 70 | this.tableConfig = { |
| 71 | columns: [ |
| 72 | ], |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 73 | actions: [ |
| 74 | { |
| 75 | label: 'delete', |
| 76 | icon: 'remove', |
| 77 | cb: (item) => { |
| 78 | this.Resource.delete({id: item.id}).$promise |
| 79 | .then(() => { |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 80 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully deleted`; |
| 81 | }) |
| 82 | .catch(err => { |
| 83 | this.responseErr = err.data.detail || `Error while deleting ${this.config.resource} with id ${item.id}`; |
| 84 | }); |
| 85 | }, |
| 86 | color: 'red' |
| 87 | }, |
| 88 | { |
| 89 | label: 'details', |
| 90 | icon: 'search', |
| 91 | cb: (item) => { |
| 92 | this.detailedItem = item; |
| 93 | } |
| 94 | } |
| 95 | ], |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 96 | classes: 'table table-striped table-bordered table-responsive', |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 97 | filter: 'field', |
| 98 | order: true, |
| 99 | pagination: { |
| 100 | pageSize: 10 |
| 101 | } |
| 102 | }; |
| 103 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 104 | this.formConfig = { |
| 105 | exclude: this.config.hiddenFields, |
| 106 | formName: `${this.config.resource}Form`, |
| 107 | actions: [ |
| 108 | { |
| 109 | label: 'Save', |
| 110 | icon: 'ok', |
| 111 | cb: (item) => { |
| 112 | item.$save() |
| 113 | .then(() => { |
| 114 | this.responseMsg = `${this.config.resource} with id ${item.id} successfully saved`; |
| 115 | }) |
| 116 | .catch((err) => { |
| 117 | this.responseErr = err.data.detail || `Error while saving ${this.config.resource} with id ${item.id}`; |
| 118 | }) |
| 119 | }, |
| 120 | class: 'success' |
| 121 | } |
| 122 | ] |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | this.cleanForm = () => { |
| 126 | delete this.detailedItem; |
Matteo Scandolo | c3c697a | 2016-04-26 15:56:05 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | this.createItem = () => { |
| 130 | this.detailedItem = new this.Resource(); |
| 131 | console.log(this.detailedItem); |
| 132 | }; |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 133 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 134 | this.Resource = $injector.get(this.config.resource); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 136 | this.Resource.query().$promise |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 137 | .then((res) => { |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 138 | |
| 139 | if(!res[0]){ |
| 140 | return; |
| 141 | } |
| 142 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 143 | let props = Object.keys(res[0]); |
| 144 | |
| 145 | _.remove(props, p => { |
| 146 | return p == 'id' || p == 'password' || p == 'validators' |
| 147 | }); |
| 148 | |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 149 | // TODO move out cb |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 150 | if(angular.isArray(this.config.hiddenFields)){ |
| 151 | props = _.difference(props, this.config.hiddenFields) |
| 152 | } |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 153 | |
Matteo Scandolo | ba1d35c | 2016-04-26 10:10:54 -0700 | [diff] [blame] | 154 | let labels = props.map(p => LabelFormatter.format(p)); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 155 | |
| 156 | props.forEach((p, i) => { |
| 157 | this.tableConfig.columns.push({ |
| 158 | label: labels[i], |
| 159 | prop: p |
| 160 | }); |
| 161 | }); |
| 162 | |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 163 | this.data = res; |
Matteo Scandolo | f644535 | 2016-04-26 12:15:23 -0700 | [diff] [blame] | 164 | }); |
Matteo Scandolo | 8b55d9f | 2016-04-25 17:50:28 -0700 | [diff] [blame] | 165 | } |
| 166 | }; |
| 167 | }); |
| 168 | })(); |