blob: beca6f8b7024dd27fca505f4a7d2df3853d71cb2 [file] [log] [blame]
Bri Prebilic Cole54d09382015-03-19 18:40:27 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole54d09382015-03-19 18:40:27 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 ONOS GUI -- Widget -- Tooltip Service
19 */
20
21(function () {
22 'use strict';
23
24 // injected references
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070025 var $log, $rootScope, fs;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070026
27 // constants
28 var hoverHeight = 35,
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -070029 hoverDelay = 150,
30 exitDelay = 150;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070031
32 // internal state
33 var tooltip, currElemId;
34
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070035 // === Helper functions ---------------------------------------------
36
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070037 function init() {
38 tooltip = d3.select('#tooltip');
Simon Hunt239f09e2017-05-18 13:10:09 -070039 tooltip.text('');
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070040 }
41
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070042 function tipStyle(mouseX, mouseY) {
43 var winWidth = fs.windowSize().width,
44 winHeight = fs.windowSize().height,
45 style = {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -070046 display: 'inline-block',
47 left: 'auto',
48 right: 'auto'
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070049 };
50
51 if (mouseX <= (winWidth / 2)) {
52 style.left = mouseX + 'px';
53 } else {
54 style.right = (winWidth - mouseX) + 'px';
55 }
56
57 if (mouseY <= (winHeight / 2)) {
58 style.top = (mouseY + (hoverHeight - 10)) + 'px';
59 } else {
60 style.top = (mouseY - hoverHeight) + 'px';
61 }
62
63 return style;
64 }
65
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070066 // === API functions ------------------------------------------------
67
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070068 function addTooltip(elem, tooltip) {
69 elem.on('mouseover', function () { showTooltip(this, tooltip); });
70 elem.on('mouseout', function () { cancelTooltip(this); });
71 $rootScope.$on('$routeChangeStart', function () {
72 cancelTooltip(elem.node());
73 });
74 }
75
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070076 function showTooltip(el, msg) {
Simon Huntd552ee92015-04-02 17:06:35 -070077 // tooltips don't make sense on mobile devices
78 if (!el || !msg || fs.isMobile()) {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070079 return;
80 }
Simon Huntd552ee92015-04-02 17:06:35 -070081
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070082 var elem = d3.select(el),
83 mouseX = d3.event.pageX,
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070084 mouseY = d3.event.pageY,
85 style = tipStyle(mouseX, mouseY);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070086 currElemId = elem.attr('id');
87
88 tooltip.transition()
89 .delay(hoverDelay)
90 .each('start', function () {
91 d3.select(this).style('display', 'none');
92 })
93 .each('end', function () {
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070094 d3.select(this).style(style)
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070095 .text(msg);
96 });
97 }
98
99 function cancelTooltip(el) {
100 if (!el) {
101 return;
102 }
103 var elem = d3.select(el);
104
105 if (elem.attr('id') === currElemId) {
106 tooltip.transition()
107 .delay(exitDelay)
108 .style({
109 display: 'none'
110 })
111 .text('');
112 }
113 }
114
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700115 angular.module('onosWidget')
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700116
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700117 .directive('tooltip', ['$rootScope', 'FnService',
118 function (_$rootScope_, _fs_) {
119 $rootScope = _$rootScope_;
120 fs = _fs_;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700121
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700122 init();
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700123
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700124 return {
125 restrict: 'A',
126 link: function (scope, elem, attrs) {
127 addTooltip(d3.select(elem[0]), scope[attrs.ttMsg]);
128 }
129 };
130 }])
131
132 .factory('TooltipService', ['$log', '$rootScope', 'FnService',
133 function (_$log_, _$rootScope_, _fs_) {
134 $log = _$log_;
135 $rootScope = _$rootScope_;
136 fs = _fs_;
137
138 init();
139
140 return {
141 addTooltip: addTooltip,
142 showTooltip: showTooltip,
143 cancelTooltip: cancelTooltip
144 };
145 }]);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700146}());