blob: 4d8169ce6099fa2ac541366002ced7d3f90e0082 [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 */
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070020(function () {
21 'use strict';
22
23 // injected references
Steven Burrows46c5f102017-07-14 16:52:46 +010024 var $rootScope, fs;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070025
26 // constants
27 var hoverHeight = 35,
Bri Prebilic Cole9a5e0062015-05-11 17:20:57 -070028 hoverDelay = 150,
29 exitDelay = 150;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070030
31 // internal state
32 var tooltip, currElemId;
33
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070034 // === Helper functions ---------------------------------------------
35
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070036 function init() {
37 tooltip = d3.select('#tooltip');
Simon Hunt239f09e2017-05-18 13:10:09 -070038 tooltip.text('');
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070039 }
40
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070041 function tipStyle(mouseX, mouseY) {
42 var winWidth = fs.windowSize().width,
43 winHeight = fs.windowSize().height,
44 style = {
Bri Prebilic Colef5e48b12015-04-21 14:52:36 -070045 display: 'inline-block',
46 left: 'auto',
Steven Burrows46c5f102017-07-14 16:52:46 +010047 right: 'auto',
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070048 };
49
50 if (mouseX <= (winWidth / 2)) {
51 style.left = mouseX + 'px';
52 } else {
53 style.right = (winWidth - mouseX) + 'px';
54 }
55
56 if (mouseY <= (winHeight / 2)) {
57 style.top = (mouseY + (hoverHeight - 10)) + 'px';
58 } else {
59 style.top = (mouseY - hoverHeight) + 'px';
60 }
61
62 return style;
63 }
64
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070065 // === API functions ------------------------------------------------
66
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -070067 function addTooltip(elem, tooltip) {
68 elem.on('mouseover', function () { showTooltip(this, tooltip); });
69 elem.on('mouseout', function () { cancelTooltip(this); });
70 $rootScope.$on('$routeChangeStart', function () {
71 cancelTooltip(elem.node());
72 });
73 }
74
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070075 function showTooltip(el, msg) {
Simon Huntd552ee92015-04-02 17:06:35 -070076 // tooltips don't make sense on mobile devices
77 if (!el || !msg || fs.isMobile()) {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070078 return;
79 }
Simon Huntd552ee92015-04-02 17:06:35 -070080
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070081 var elem = d3.select(el),
82 mouseX = d3.event.pageX,
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070083 mouseY = d3.event.pageY,
84 style = tipStyle(mouseX, mouseY);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070085 currElemId = elem.attr('id');
86
87 tooltip.transition()
88 .delay(hoverDelay)
89 .each('start', function () {
90 d3.select(this).style('display', 'none');
91 })
92 .each('end', function () {
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070093 d3.select(this).style(style)
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070094 .text(msg);
95 });
96 }
97
98 function cancelTooltip(el) {
99 if (!el) {
100 return;
101 }
102 var elem = d3.select(el);
103
104 if (elem.attr('id') === currElemId) {
105 tooltip.transition()
106 .delay(exitDelay)
107 .style({
Steven Burrows46c5f102017-07-14 16:52:46 +0100108 display: 'none',
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700109 })
110 .text('');
111 }
112 }
113
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700114 angular.module('onosWidget')
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700115
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700116 .directive('tooltip', ['$rootScope', 'FnService',
117 function (_$rootScope_, _fs_) {
118 $rootScope = _$rootScope_;
119 fs = _fs_;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700120
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700121 init();
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700122
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700123 return {
124 restrict: 'A',
125 link: function (scope, elem, attrs) {
126 addTooltip(d3.select(elem[0]), scope[attrs.ttMsg]);
Steven Burrows46c5f102017-07-14 16:52:46 +0100127 },
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700128 };
129 }])
130
Steven Burrows46c5f102017-07-14 16:52:46 +0100131 .factory('TooltipService', ['$rootScope', 'FnService',
132 function (_$rootScope_, _fs_) {
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700133 $rootScope = _$rootScope_;
134 fs = _fs_;
135
136 init();
137
138 return {
139 addTooltip: addTooltip,
140 showTooltip: showTooltip,
Steven Burrows46c5f102017-07-14 16:52:46 +0100141 cancelTooltip: cancelTooltip,
Bri Prebilic Coleeef67ae2015-07-01 16:26:59 -0700142 };
143 }]);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700144}());