blob: 23097d2bc8911b924164f7e803345ab53a56ae1a [file] [log] [blame]
Matteo Scandolo686547a2017-08-08 13:05:25 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloa5d03d52016-07-21 11:35:46 -070019/**
20 * © OpenCORD
21 *
22 * Visit http://guide.xosproject.org/devguide/addview/ for more information
23 *
24 * Created by teone on 4/15/16.
25 */
26
27(function () {
28 'use strict';
29
30 angular.module('xos.uiComponents')
31
32 /**
33 * @ngdoc directive
34 * @name xos.uiComponents.directive:xosAlert
35 * @restrict E
36 * @description The xos-alert directive
37 * @param {Object} config The configuration object
38 * ```
39 * {
40 * type: 'danger', //info, success, warning
41 * closeBtn: true, //default false
42 * autoHide: 3000 //delay to automatically hide the alert
43 * }
44 * ```
45 * @param {Boolean=} show Binding to show and hide the alert, default to true
46 * @element ANY
47 * @scope
48 * @example
49 <example module="sampleAlert1">
50 <file name="index.html">
51 <div ng-controller="SampleCtrl1 as vm">
52 <xos-alert config="vm.config1">
53 A sample alert message
54 </xos-alert>
55 <xos-alert config="vm.config2">
56 A sample alert message (with close button)
57 </xos-alert>
58 <xos-alert config="vm.config3">
59 A sample info message
60 </xos-alert>
61 <xos-alert config="vm.config4">
62 A sample success message
63 </xos-alert>
64 <xos-alert config="vm.config5">
65 A sample warning message
66 </xos-alert>
67 </div>
68 </file>
69 <file name="script.js">
70 angular.module('sampleAlert1', ['xos.uiComponents'])
71 .controller('SampleCtrl1', function(){
72 this.config1 = {
73 type: 'danger'
74 };
75
76 this.config2 = {
77 type: 'danger',
78 closeBtn: true
79 };
80
81 this.config3 = {
82 type: 'info'
83 };
84
85 this.config4 = {
86 type: 'success'
87 };
88
89 this.config5 = {
90 type: 'warning'
91 };
92 });
93 </file>
94 </example>
95
96 <example module="sampleAlert2" animations="true">
97 <file name="index.html">
98 <div ng-controller="SampleCtrl as vm" class="row">
99 <div class="col-sm-4">
100 <a class="btn btn-default btn-block" ng-show="!vm.show" ng-click="vm.show = true">Show Alert</a>
101 <a class="btn btn-default btn-block" ng-show="vm.show" ng-click="vm.show = false">Hide Alert</a>
102 </div>
103 <div class="col-sm-8">
104 <xos-alert config="vm.config1" show="vm.show">
105 A sample alert message, not displayed by default.
106 </xos-alert>
107 </div>
108 </div>
109 </file>
110 <file name="script.js">
111 angular.module('sampleAlert2', ['xos.uiComponents', 'ngAnimate'])
112 .controller('SampleCtrl', function(){
113 this.config1 = {
114 type: 'success'
115 };
116
117 this.show = false;
118 });
119 </file>
120 </example>
121 **/
122
Arpit Agarwal34b63832016-08-08 11:59:45 -0700123 .component('xosAlert', {
124 restrict: 'E',
125 bindings: {
126 config: '=',
127 show: '=?'
128 },
129 template: `
130 <div ng-cloak class="alert alert-{{vm.config.type}}" ng-hide="!vm.show">
131 <button type="button" class="close" ng-if="vm.config.closeBtn" ng-click="vm.dismiss()">
132 <span aria-hidden="true">&times;</span>
133 </button>
134 <p ng-transclude></p>
135 </div>
136 `,
137 transclude: true,
138 bindToController: true,
139 controllerAs: 'vm',
140 controller: function($timeout){
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700141
Arpit Agarwal34b63832016-08-08 11:59:45 -0700142 if(!this.config){
143 throw new Error('[xosAlert] Please provide a configuration via the "config" attribute');
144 }
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700145
Arpit Agarwal34b63832016-08-08 11:59:45 -0700146 // default the value to true
147 this.show = this.show !== false;
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700148
Arpit Agarwal34b63832016-08-08 11:59:45 -0700149 this.dismiss = () => {
150 this.show = false;
151 }
152
153 if(this.config.autoHide){
154 let to = $timeout(() => {
155 this.dismiss();
156 $timeout.cancel(to);
157 }, this.config.autoHide);
Matteo Scandoloa5d03d52016-07-21 11:35:46 -0700158 }
159 }
160 })
161})();