blob: 948c851e147fb1a84624a18fd80a5ed96d79fb2d [file] [log] [blame]
Matteo Scandoloa5d03d52016-07-21 11:35:46 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 4/15/16.
7 */
8
9(function () {
10 'use strict';
11
12 angular.module('xos.uiComponents')
13
14 /**
15 * @ngdoc directive
16 * @name xos.uiComponents.directive:xosAlert
17 * @restrict E
18 * @description The xos-alert directive
19 * @param {Object} config The configuration object
20 * ```
21 * {
22 * type: 'danger', //info, success, warning
23 * closeBtn: true, //default false
24 * autoHide: 3000 //delay to automatically hide the alert
25 * }
26 * ```
27 * @param {Boolean=} show Binding to show and hide the alert, default to true
28 * @element ANY
29 * @scope
30 * @example
31 <example module="sampleAlert1">
32 <file name="index.html">
33 <div ng-controller="SampleCtrl1 as vm">
34 <xos-alert config="vm.config1">
35 A sample alert message
36 </xos-alert>
37 <xos-alert config="vm.config2">
38 A sample alert message (with close button)
39 </xos-alert>
40 <xos-alert config="vm.config3">
41 A sample info message
42 </xos-alert>
43 <xos-alert config="vm.config4">
44 A sample success message
45 </xos-alert>
46 <xos-alert config="vm.config5">
47 A sample warning message
48 </xos-alert>
49 </div>
50 </file>
51 <file name="script.js">
52 angular.module('sampleAlert1', ['xos.uiComponents'])
53 .controller('SampleCtrl1', function(){
54 this.config1 = {
55 type: 'danger'
56 };
57
58 this.config2 = {
59 type: 'danger',
60 closeBtn: true
61 };
62
63 this.config3 = {
64 type: 'info'
65 };
66
67 this.config4 = {
68 type: 'success'
69 };
70
71 this.config5 = {
72 type: 'warning'
73 };
74 });
75 </file>
76 </example>
77
78 <example module="sampleAlert2" animations="true">
79 <file name="index.html">
80 <div ng-controller="SampleCtrl as vm" class="row">
81 <div class="col-sm-4">
82 <a class="btn btn-default btn-block" ng-show="!vm.show" ng-click="vm.show = true">Show Alert</a>
83 <a class="btn btn-default btn-block" ng-show="vm.show" ng-click="vm.show = false">Hide Alert</a>
84 </div>
85 <div class="col-sm-8">
86 <xos-alert config="vm.config1" show="vm.show">
87 A sample alert message, not displayed by default.
88 </xos-alert>
89 </div>
90 </div>
91 </file>
92 <file name="script.js">
93 angular.module('sampleAlert2', ['xos.uiComponents', 'ngAnimate'])
94 .controller('SampleCtrl', function(){
95 this.config1 = {
96 type: 'success'
97 };
98
99 this.show = false;
100 });
101 </file>
102 </example>
103 **/
104
Arpit Agarwal34b63832016-08-08 11:59:45 -0700105 .component('xosAlert', {
106 restrict: 'E',
107 bindings: {
108 config: '=',
109 show: '=?'
110 },
111 template: `
112 <div ng-cloak class="alert alert-{{vm.config.type}}" ng-hide="!vm.show">
113 <button type="button" class="close" ng-if="vm.config.closeBtn" ng-click="vm.dismiss()">
114 <span aria-hidden="true">&times;</span>
115 </button>
116 <p ng-transclude></p>
117 </div>
118 `,
119 transclude: true,
120 bindToController: true,
121 controllerAs: 'vm',
122 controller: function($timeout){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700123
Arpit Agarwal34b63832016-08-08 11:59:45 -0700124 if(!this.config){
125 throw new Error('[xosAlert] Please provide a configuration via the "config" attribute');
126 }
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700127
Arpit Agarwal34b63832016-08-08 11:59:45 -0700128 // default the value to true
129 this.show = this.show !== false;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700130
Arpit Agarwal34b63832016-08-08 11:59:45 -0700131 this.dismiss = () => {
132 this.show = false;
133 }
134
135 if(this.config.autoHide){
136 let to = $timeout(() => {
137 this.dismiss();
138 $timeout.cancel(to);
139 }, this.config.autoHide);
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700140 }
141 }
142 })
143})();