blob: d08393c27f9b12a7ee871397e4203cd0fcd838ab [file] [log] [blame]
Bri Prebilic Cole54d09382015-03-19 18:40:27 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Cole8f07f0d2015-04-23 13:28:43 -070025 var $log, fs;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070026
27 // constants
28 var hoverHeight = 35,
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -070029 hoverDelay = 100,
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070030 exitDelay = 100;
31
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');
39 tooltip.html('');
40 }
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
68 function showTooltip(el, msg) {
Simon Huntd552ee92015-04-02 17:06:35 -070069 // tooltips don't make sense on mobile devices
70 if (!el || !msg || fs.isMobile()) {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070071 return;
72 }
Simon Huntd552ee92015-04-02 17:06:35 -070073
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070074 var elem = d3.select(el),
75 mouseX = d3.event.pageX,
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070076 mouseY = d3.event.pageY,
77 style = tipStyle(mouseX, mouseY);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070078 currElemId = elem.attr('id');
79
80 tooltip.transition()
81 .delay(hoverDelay)
82 .each('start', function () {
83 d3.select(this).style('display', 'none');
84 })
85 .each('end', function () {
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070086 d3.select(this).style(style)
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070087 .text(msg);
88 });
89 }
90
91 function cancelTooltip(el) {
92 if (!el) {
93 return;
94 }
95 var elem = d3.select(el);
96
97 if (elem.attr('id') === currElemId) {
98 tooltip.transition()
99 .delay(exitDelay)
100 .style({
101 display: 'none'
102 })
103 .text('');
104 }
105 }
106
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700107 function resetTooltip() {
108 tooltip.style('display', 'none').text('');
109 }
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700110
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700111 angular.module('onosWidget')
112 .factory('TooltipService', ['$log', 'FnService',
113
114 function (_$log_, _fs_) {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700115 $log = _$log_;
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700116 fs = _fs_;
117
118 init();
119
120 return {
121 showTooltip: showTooltip,
Bri Prebilic Cole8f07f0d2015-04-23 13:28:43 -0700122 cancelTooltip: cancelTooltip,
123 resetTooltip: resetTooltip
Bri Prebilic Cole54d09382015-03-19 18:40:27 -0700124 };
125 }]);
126}());