Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -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 | |
Matteo Scandolo | cc6954e | 2016-04-15 12:20:14 -0700 | [diff] [blame] | 12 | angular.module('xos.uiComponents') |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * @ngdoc directive |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 16 | * @name xos.uiComponents.directive:xosTable |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 17 | * @restrict E |
| 18 | * @description The xos-table directive |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 19 | * @param {Object} config The configuration for the component. |
| 20 | * ``` |
| 21 | * { |
| 22 | * columns: [ |
| 23 | * { |
| 24 | * label: 'Human readable name', |
Matteo Scandolo | 83369f3 | 2016-05-13 10:39:38 -0700 | [diff] [blame] | 25 | * prop: 'Property to read in the model object', |
| 26 | * type: 'boolean'| 'array'| 'object'| 'custom'| 'date' | 'icon' // see examples for more details |
| 27 | formatter: fn(), // receive the whole item if tipe is custom and return a string |
| 28 | link: fn() // receive the whole item and return an url |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 29 | * } |
| 30 | * ], |
| 31 | * classes: 'table table-striped table-bordered', |
| 32 | * actions: [ // if defined add an action column |
| 33 | { |
| 34 | label: 'delete', |
| 35 | icon: 'remove', // refers to bootstraps glyphicon |
| 36 | cb: (user) => { // receive the model |
| 37 | console.log(user); |
| 38 | }, |
| 39 | color: 'red' |
| 40 | } |
Matteo Scandolo | 5677bb1 | 2016-04-14 17:21:45 -0700 | [diff] [blame] | 41 | ], |
| 42 | filter: 'field', // can be by `field` or `fulltext` |
Matteo Scandolo | 83369f3 | 2016-05-13 10:39:38 -0700 | [diff] [blame] | 43 | order: true | {field: 'property name', reverse: true | false} // whether to show ordering arrows, or a configuration for a default ordering |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 44 | * } |
| 45 | * ``` |
| 46 | * @param {Array} data The data that should be rendered |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 47 | * @element ANY |
| 48 | * @scope |
| 49 | * @example |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 50 | |
| 51 | # Basic usage |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 52 | <example module="sampleTable1"> |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 53 | <file name="index.html"> |
| 54 | <div ng-controller="SampleCtrl1 as vm"> |
| 55 | <xos-table data="vm.data" config="vm.config"></xos-table> |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 56 | </div> |
| 57 | </file> |
| 58 | <file name="script.js"> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 59 | angular.module('sampleTable1', ['xos.uiComponents']) |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 60 | .factory('_', function($window){ |
| 61 | return $window._; |
| 62 | }) |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 63 | .controller('SampleCtrl1', function(){ |
| 64 | this.config = { |
| 65 | columns: [ |
| 66 | { |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 67 | label: 'First Name', // column title |
| 68 | prop: 'name' // property to read in the data array |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 69 | }, |
| 70 | { |
| 71 | label: 'Last Name', |
| 72 | prop: 'lastname' |
| 73 | } |
| 74 | ] |
| 75 | }; |
| 76 | |
| 77 | this.data = [ |
| 78 | { |
| 79 | name: 'John', |
| 80 | lastname: 'Doe' |
| 81 | }, |
| 82 | { |
| 83 | name: 'Gili', |
| 84 | lastname: 'Fereydoun' |
| 85 | } |
| 86 | ] |
| 87 | }); |
| 88 | </file> |
| 89 | </example> |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 90 | |
| 91 | # Filtering |
Matteo Scandolo | 88e1846 | 2016-04-25 14:24:18 -0700 | [diff] [blame] | 92 | <example module="sampleTable2" animations="true"> |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 93 | <file name="index.html"> |
| 94 | <div ng-controller="SampleCtrl2 as vm"> |
| 95 | <xos-table data="vm.data" config="vm.config"></xos-table> |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 96 | </div> |
| 97 | </file> |
| 98 | <file name="script.js"> |
Matteo Scandolo | 88e1846 | 2016-04-25 14:24:18 -0700 | [diff] [blame] | 99 | angular.module('sampleTable2', ['xos.uiComponents', 'ngAnimate']) |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 100 | .factory('_', function($window){ |
| 101 | return $window._; |
| 102 | }) |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 103 | .controller('SampleCtrl2', function(){ |
| 104 | this.config = { |
| 105 | columns: [ |
| 106 | { |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 107 | label: 'First Name', // column title |
| 108 | prop: 'name' // property to read in the data array |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 109 | }, |
| 110 | { |
| 111 | label: 'Last Name', |
| 112 | prop: 'lastname' |
| 113 | } |
| 114 | ], |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 115 | classes: 'table table-striped table-condensed', // table classes, default to `table table-striped table-bordered` |
| 116 | actions: [ // if defined add an action column |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 117 | { |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 118 | label: 'delete', // label |
| 119 | icon: 'remove', // icons, refers to bootstraps glyphicon |
| 120 | cb: (user) => { // callback, get feeded with the full object |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 121 | console.log(user); |
| 122 | }, |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 123 | color: 'red' // icon color |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 124 | } |
Matteo Scandolo | 5677bb1 | 2016-04-14 17:21:45 -0700 | [diff] [blame] | 125 | ], |
| 126 | filter: 'field', // can be by `field` or `fulltext` |
| 127 | order: true |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | this.data = [ |
| 131 | { |
| 132 | name: 'John', |
| 133 | lastname: 'Doe' |
| 134 | }, |
| 135 | { |
| 136 | name: 'Gili', |
| 137 | lastname: 'Fereydoun' |
| 138 | } |
| 139 | ] |
| 140 | }); |
| 141 | </file> |
| 142 | </example> |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 143 | |
| 144 | # Pagination |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 145 | <example module="sampleTable3"> |
| 146 | <file name="index.html"> |
| 147 | <div ng-controller="SampleCtrl3 as vm"> |
| 148 | <xos-table data="vm.data" config="vm.config"></xos-table> |
| 149 | </div> |
| 150 | </file> |
| 151 | <file name="script.js"> |
| 152 | angular.module('sampleTable3', ['xos.uiComponents']) |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 153 | .factory('_', function($window){ |
| 154 | return $window._; |
| 155 | }) |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 156 | .controller('SampleCtrl3', function(){ |
| 157 | this.config = { |
| 158 | columns: [ |
| 159 | { |
| 160 | label: 'First Name', // column title |
| 161 | prop: 'name' // property to read in the data array |
| 162 | }, |
| 163 | { |
| 164 | label: 'Last Name', |
| 165 | prop: 'lastname' |
| 166 | } |
| 167 | ], |
| 168 | pagination: { |
| 169 | pageSize: 2 |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | this.data = [ |
| 174 | { |
| 175 | name: 'John', |
| 176 | lastname: 'Doe' |
| 177 | }, |
| 178 | { |
| 179 | name: 'Gili', |
| 180 | lastname: 'Fereydoun' |
| 181 | }, |
| 182 | { |
| 183 | name: 'Lucky', |
| 184 | lastname: 'Clarkson' |
| 185 | }, |
| 186 | { |
| 187 | name: 'Tate', |
| 188 | lastname: 'Spalding' |
| 189 | } |
| 190 | ] |
| 191 | }); |
| 192 | </file> |
| 193 | </example> |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 194 | |
| 195 | # Field formatter |
| 196 | <example module="sampleTable4"> |
| 197 | <file name="index.html"> |
| 198 | <div ng-controller="SampleCtrl as vm"> |
| 199 | <xos-table data="vm.data" config="vm.config"></xos-table> |
| 200 | </div> |
| 201 | </file> |
| 202 | <file name="script.js"> |
| 203 | angular.module('sampleTable4', ['xos.uiComponents']) |
| 204 | .factory('_', function($window){ |
| 205 | return $window._; |
| 206 | }) |
| 207 | .controller('SampleCtrl', function(){ |
| 208 | this.config = { |
| 209 | columns: [ |
| 210 | { |
| 211 | label: 'First Name', |
Matteo Scandolo | bee3eaf | 2016-05-06 13:09:19 -0700 | [diff] [blame] | 212 | prop: 'name', |
| 213 | link: item => `https://www.google.it/#q=${item.name}` |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 214 | }, |
| 215 | { |
| 216 | label: 'Enabled', |
| 217 | prop: 'enabled', |
| 218 | type: 'boolean' |
| 219 | }, |
| 220 | { |
| 221 | label: 'Services', |
| 222 | prop: 'services', |
| 223 | type: 'array' |
| 224 | }, |
| 225 | { |
| 226 | label: 'Details', |
| 227 | prop: 'details', |
| 228 | type: 'object' |
Matteo Scandolo | 83369f3 | 2016-05-13 10:39:38 -0700 | [diff] [blame] | 229 | }, |
| 230 | { |
| 231 | label: 'Created', |
| 232 | prop: 'created', |
| 233 | type: 'date' |
| 234 | }, |
| 235 | { |
| 236 | label: 'Icon', |
| 237 | type: 'icon', |
| 238 | formatter: item => item.icon //note that this refer to [Bootstrap Glyphicon](http://getbootstrap.com/components/#glyphicons) |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 239 | } |
| 240 | ] |
| 241 | }; |
| 242 | |
| 243 | this.data = [ |
| 244 | { |
| 245 | name: 'John', |
| 246 | enabled: true, |
| 247 | services: ['Cdn', 'IpTv'], |
| 248 | details: { |
| 249 | c_tag: '243', |
| 250 | s_tag: '444' |
Matteo Scandolo | 83369f3 | 2016-05-13 10:39:38 -0700 | [diff] [blame] | 251 | }, |
| 252 | created: new Date('December 17, 1995 03:24:00'), |
| 253 | icon: 'music' |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 254 | }, |
| 255 | { |
| 256 | name: 'Gili', |
| 257 | enabled: false, |
| 258 | services: ['Cdn', 'IpTv', 'Cache'], |
| 259 | details: { |
| 260 | c_tag: '675', |
| 261 | s_tag: '893' |
Matteo Scandolo | 83369f3 | 2016-05-13 10:39:38 -0700 | [diff] [blame] | 262 | }, |
| 263 | created: new Date(), |
| 264 | icon: 'camera' |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 265 | } |
| 266 | ] |
| 267 | }); |
| 268 | </file> |
| 269 | </example> |
| 270 | |
| 271 | # Custom formatter |
| 272 | <example module="sampleTable5"> |
| 273 | <file name="index.html"> |
| 274 | <div ng-controller="SampleCtrl as vm"> |
| 275 | <xos-table data="vm.data" config="vm.config"></xos-table> |
| 276 | </div> |
| 277 | </file> |
| 278 | <file name="script.js"> |
| 279 | angular.module('sampleTable5', ['xos.uiComponents']) |
| 280 | .factory('_', function($window){ |
| 281 | return $window._; |
| 282 | }) |
| 283 | .controller('SampleCtrl', function(){ |
| 284 | this.config = { |
| 285 | columns: [ |
| 286 | { |
| 287 | label: 'Username', |
| 288 | prop: 'username' |
| 289 | }, |
| 290 | { |
| 291 | label: 'Features', |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 292 | type: 'custom', |
| 293 | formatter: (val) => { |
| 294 | |
Matteo Scandolo | a7ad499 | 2016-05-09 15:27:47 -0700 | [diff] [blame] | 295 | let cdnEnabled = val.features.cdn ? 'enabled' : 'disabled'; |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 296 | return ` |
| 297 | Cdn is ${cdnEnabled}, |
Matteo Scandolo | a7ad499 | 2016-05-09 15:27:47 -0700 | [diff] [blame] | 298 | uplink speed is ${val.features.uplink_speed} |
| 299 | and downlink speed is ${val.features.downlink_speed} |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 300 | `; |
| 301 | } |
| 302 | } |
| 303 | ] |
| 304 | }; |
| 305 | |
| 306 | this.data = [ |
| 307 | { |
| 308 | username: 'John', |
| 309 | features: { |
| 310 | "cdn": false, |
| 311 | "uplink_speed": 1000000000, |
| 312 | "downlink_speed": 1000000000, |
| 313 | "uverse": true, |
| 314 | "status": "enabled" |
| 315 | } |
| 316 | }, |
| 317 | { |
| 318 | username: 'Gili', |
| 319 | features: { |
| 320 | "cdn": true, |
| 321 | "uplink_speed": 3000000000, |
| 322 | "downlink_speed": 2000000000, |
| 323 | "uverse": true, |
| 324 | "status": "enabled" |
| 325 | } |
| 326 | } |
| 327 | ] |
| 328 | }); |
| 329 | </file> |
| 330 | </example> |
Matteo Scandolo | aa683dd | 2016-04-14 15:34:12 -0700 | [diff] [blame] | 331 | **/ |
| 332 | |
Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -0700 | [diff] [blame] | 333 | .directive('xosTable', function(){ |
| 334 | return { |
| 335 | restrict: 'E', |
| 336 | scope: { |
| 337 | data: '=', |
Matteo Scandolo | 18adcb5 | 2016-04-14 12:06:50 -0700 | [diff] [blame] | 338 | config: '=' |
Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -0700 | [diff] [blame] | 339 | }, |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 340 | template: ` |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 341 | <div ng-show="vm.data.length > 0"> |
| 342 | <div class="row" ng-if="vm.config.filter == 'fulltext'"> |
| 343 | <div class="col-xs-12"> |
| 344 | <input |
| 345 | class="form-control" |
| 346 | placeholder="Type to search.." |
| 347 | type="text" |
| 348 | ng-model="vm.query"/> |
| 349 | </div> |
| 350 | </div> |
Matteo Scandolo | b028075 | 2016-04-25 10:31:22 -0700 | [diff] [blame] | 351 | <table ng-class="vm.classes" ng-hide="vm.data.length == 0"> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 352 | <thead> |
| 353 | <tr> |
| 354 | <th ng-repeat="col in vm.columns"> |
| 355 | {{col.label}} |
| 356 | <span ng-if="vm.config.order"> |
| 357 | <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = false"> |
| 358 | <i class="glyphicon glyphicon-chevron-up"></i> |
| 359 | </a> |
| 360 | <a href="" ng-click="vm.orderBy = col.prop; vm.reverse = true"> |
| 361 | <i class="glyphicon glyphicon-chevron-down"></i> |
| 362 | </a> |
| 363 | </span> |
| 364 | </th> |
Matteo Scandolo | dc249eb | 2016-04-26 11:44:36 -0700 | [diff] [blame] | 365 | <th ng-if="vm.config.actions">Actions:</th> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 366 | </tr> |
| 367 | </thead> |
| 368 | <tbody ng-if="vm.config.filter == 'field'"> |
| 369 | <tr> |
| 370 | <td ng-repeat="col in vm.columns"> |
| 371 | <input |
Matteo Scandolo | c647c9b | 2016-05-18 14:57:56 -0700 | [diff] [blame] | 372 | ng-if="col.type !== 'boolean'" |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 373 | class="form-control" |
| 374 | placeholder="Type to search by {{col.label}}" |
| 375 | type="text" |
| 376 | ng-model="vm.query[col.prop]"/> |
Matteo Scandolo | c647c9b | 2016-05-18 14:57:56 -0700 | [diff] [blame] | 377 | <select |
| 378 | ng-if="col.type === 'boolean'" |
| 379 | class="form-control" |
| 380 | ng-model="vm.query[col.prop]"> |
| 381 | <option value="">-</option> |
| 382 | <option value="true">True</option> |
| 383 | <option value="false">False</option> |
| 384 | </select> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 385 | </td> |
| 386 | <td ng-if="vm.config.actions"></td> |
| 387 | </tr> |
| 388 | </tbody> |
| 389 | <tbody> |
| 390 | <tr ng-repeat="item in vm.data | filter:vm.query | orderBy:vm.orderBy:vm.reverse | pagination:vm.currentPage * vm.config.pagination.pageSize | limitTo: (vm.config.pagination.pageSize || vm.data.length) track by $index"> |
Matteo Scandolo | bee3eaf | 2016-05-06 13:09:19 -0700 | [diff] [blame] | 391 | <td ng-repeat="col in vm.columns" link-wrapper> |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 392 | <span ng-if="!col.type">{{item[col.prop]}}</span> |
| 393 | <span ng-if="col.type === 'boolean'"> |
| 394 | <i class="glyphicon" |
| 395 | ng-class="{'glyphicon-ok': item[col.prop], 'glyphicon-remove': !item[col.prop]}"> |
| 396 | </i> |
| 397 | </span> |
| 398 | <span ng-if="col.type === 'date'"> |
| 399 | {{item[col.prop] | date:'H:mm MMM d, yyyy'}} |
| 400 | </span> |
| 401 | <span ng-if="col.type === 'array'"> |
| 402 | {{item[col.prop] | arrayToList}} |
| 403 | </span> |
Matteo Scandolo | 58705a4 | 2016-05-06 10:08:34 -0700 | [diff] [blame] | 404 | <span ng-if="col.type === 'object'"> |
Matteo Scandolo | 0cd52c1 | 2016-05-06 11:39:56 -0700 | [diff] [blame] | 405 | <dl class="dl-horizontal"> |
| 406 | <span ng-repeat="(k,v) in item[col.prop]"> |
| 407 | <dt>{{k}}</dt> |
| 408 | <dd>{{v}}</dd> |
| 409 | </span> |
Matteo Scandolo | 58705a4 | 2016-05-06 10:08:34 -0700 | [diff] [blame] | 410 | </dl> |
| 411 | </span> |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 412 | <span ng-if="col.type === 'custom'"> |
Matteo Scandolo | d49ed5f | 2016-05-13 10:12:09 -0700 | [diff] [blame] | 413 | {{col.formatter(item)}} |
| 414 | </span> |
| 415 | <span ng-if="col.type === 'icon'"> |
| 416 | <i class="glyphicon glyphicon-{{col.formatter(item)}}"> |
| 417 | </i> |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 418 | </span> |
| 419 | </td> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 420 | <td ng-if="vm.config.actions"> |
| 421 | <a href="" |
| 422 | ng-repeat="action in vm.config.actions" |
| 423 | ng-click="action.cb(item)" |
| 424 | title="{{action.label}}"> |
| 425 | <i |
| 426 | class="glyphicon glyphicon-{{action.icon}}" |
| 427 | style="color: {{action.color}};"></i> |
| 428 | </a> |
| 429 | </td> |
| 430 | </tr> |
| 431 | </tbody> |
| 432 | </table> |
| 433 | <xos-pagination |
| 434 | ng-if="vm.config.pagination" |
| 435 | page-size="vm.config.pagination.pageSize" |
| 436 | total-elements="vm.data.length" |
| 437 | change="vm.goToPage"> |
| 438 | </xos-pagination> |
| 439 | </div> |
| 440 | <div ng-show="vm.data.length == 0 || !vm.data"> |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 441 | <xos-alert config="{type: 'info'}"> |
Matteo Scandolo | e15a820 | 2016-04-15 14:27:54 -0700 | [diff] [blame] | 442 | No data to show. |
Matteo Scandolo | 7bc39c4 | 2016-04-20 11:38:42 -0700 | [diff] [blame] | 443 | </xos-alert> |
Matteo Scandolo | a6a9e61 | 2016-04-14 16:52:13 -0700 | [diff] [blame] | 444 | </div> |
Matteo Scandolo | b7a86dc | 2016-04-14 11:46:30 -0700 | [diff] [blame] | 445 | `, |
Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -0700 | [diff] [blame] | 446 | bindToController: true, |
| 447 | controllerAs: 'vm', |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 448 | controller: function(_){ |
Matteo Scandolo | 18adcb5 | 2016-04-14 12:06:50 -0700 | [diff] [blame] | 449 | |
| 450 | if(!this.config){ |
| 451 | throw new Error('[xosTable] Please provide a configuration via the "config" attribute'); |
| 452 | } |
| 453 | |
| 454 | if(!this.config.columns){ |
| 455 | throw new Error('[xosTable] Please provide a columns list in the configuration'); |
| 456 | } |
| 457 | |
Matteo Scandolo | d49ed5f | 2016-05-13 10:12:09 -0700 | [diff] [blame] | 458 | // handle default ordering |
| 459 | if(this.config.order && angular.isObject(this.config.order)){ |
| 460 | this.reverse = this.config.order.reverse || false; |
| 461 | this.orderBy = this.config.order.field || 'id'; |
| 462 | } |
| 463 | |
| 464 | // if columns with type 'custom' are provided |
| 465 | // check that a custom formatte3 is provided too |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 466 | let customCols = _.filter(this.config.columns, {type: 'custom'}); |
| 467 | if(angular.isArray(customCols) && customCols.length > 0){ |
| 468 | _.forEach(customCols, (col) => { |
Matteo Scandolo | bee3eaf | 2016-05-06 13:09:19 -0700 | [diff] [blame] | 469 | if(!col.formatter || !angular.isFunction(col.formatter)){ |
Matteo Scandolo | f9700a3 | 2016-05-06 09:42:45 -0700 | [diff] [blame] | 470 | throw new Error('[xosTable] You have provided a custom field type, a formatter function should provided too.'); |
| 471 | } |
| 472 | }) |
| 473 | } |
| 474 | |
Matteo Scandolo | d49ed5f | 2016-05-13 10:12:09 -0700 | [diff] [blame] | 475 | // if columns with type 'icon' are provided |
| 476 | // check that a custom formatte3 is provided too |
| 477 | let iconCols = _.filter(this.config.columns, {type: 'icon'}); |
| 478 | if(angular.isArray(iconCols) && iconCols.length > 0){ |
| 479 | _.forEach(iconCols, (col) => { |
| 480 | if(!col.formatter || !angular.isFunction(col.formatter)){ |
| 481 | throw new Error('[xosTable] You have provided an icon field type, a formatter function should provided too.'); |
| 482 | } |
| 483 | }) |
| 484 | } |
| 485 | |
Matteo Scandolo | bee3eaf | 2016-05-06 13:09:19 -0700 | [diff] [blame] | 486 | // if a link property is passed, |
| 487 | // it should be a function |
| 488 | let linkedColumns = _.filter(this.config.columns, col => angular.isDefined(col.link)); |
| 489 | if(angular.isArray(linkedColumns) && linkedColumns.length > 0){ |
| 490 | _.forEach(linkedColumns, (col) => { |
| 491 | if(!angular.isFunction(col.link)){ |
| 492 | throw new Error('[xosTable] The link property should be a function.'); |
| 493 | } |
| 494 | }) |
| 495 | } |
| 496 | |
Matteo Scandolo | 18adcb5 | 2016-04-14 12:06:50 -0700 | [diff] [blame] | 497 | this.columns = this.config.columns; |
| 498 | this.classes = this.config.classes || 'table table-striped table-bordered'; |
| 499 | |
Matteo Scandolo | 9e6c6fc | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 500 | if(this.config.actions){ |
Matteo Scandolo | cc6954e | 2016-04-15 12:20:14 -0700 | [diff] [blame] | 501 | // TODO validate action format |
| 502 | } |
| 503 | if(this.config.pagination){ |
| 504 | this.currentPage = 0; |
| 505 | this.goToPage = (n) => { |
| 506 | this.currentPage = n; |
| 507 | }; |
Matteo Scandolo | 9e6c6fc | 2016-04-14 14:59:09 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | }) |
Matteo Scandolo | bee3eaf | 2016-05-06 13:09:19 -0700 | [diff] [blame] | 513 | // TODO move in separate files |
| 514 | // TODO test |
| 515 | .filter('arrayToList', function(){ |
| 516 | return (input) => { |
| 517 | if(!angular.isArray(input)){ |
| 518 | return input; |
| 519 | } |
| 520 | return input.join(', '); |
| 521 | } |
| 522 | }) |
| 523 | // TODO test |
| 524 | .directive('linkWrapper', function() { |
| 525 | return { |
| 526 | restrict: 'A', |
| 527 | transclude: true, |
| 528 | template: ` |
| 529 | <a ng-if="col.link" href="{{col.link(item)}}"> |
| 530 | <div ng-transclude></div> |
| 531 | </a> |
| 532 | <div ng-transclude ng-if="!col.link"></div> |
| 533 | ` |
| 534 | }; |
| 535 | }); |
Matteo Scandolo | 91fe03d | 2016-03-24 15:29:52 -0700 | [diff] [blame] | 536 | })(); |