blob: 9c356eb272113643a53fce8beb083ef05932e6f5 [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
25 var $log, $timeout, fs;
26
27 // constants
28 var hoverHeight = 35,
29 hoverDelay = 500,
30 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 = {
46 display: 'inline-block'
47 };
48
49 if (mouseX <= (winWidth / 2)) {
50 style.left = mouseX + 'px';
51 } else {
52 style.right = (winWidth - mouseX) + 'px';
53 }
54
55 if (mouseY <= (winHeight / 2)) {
56 style.top = (mouseY + (hoverHeight - 10)) + 'px';
57 } else {
58 style.top = (mouseY - hoverHeight) + 'px';
59 }
60
61 return style;
62 }
63
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070064 // === API functions ------------------------------------------------
65
66 function showTooltip(el, msg) {
67 if (!el || !msg) {
68 return;
69 }
70 var elem = d3.select(el),
71 mouseX = d3.event.pageX,
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070072 mouseY = d3.event.pageY,
73 style = tipStyle(mouseX, mouseY);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070074 currElemId = elem.attr('id');
75
76 tooltip.transition()
77 .delay(hoverDelay)
78 .each('start', function () {
79 d3.select(this).style('display', 'none');
80 })
81 .each('end', function () {
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070082 d3.select(this).style(style)
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070083 .text(msg);
84 });
85 }
86
87 function cancelTooltip(el) {
88 if (!el) {
89 return;
90 }
91 var elem = d3.select(el);
92
93 if (elem.attr('id') === currElemId) {
94 tooltip.transition()
95 .delay(exitDelay)
96 .style({
97 display: 'none'
98 })
99 .text('');
100 }
101 }
102
103 angular.module('onosWidget')
104 .factory('TooltipService', ['$log', '$timeout', 'FnService',
105
106 function (_$log_, _$timeout_, _fs_) {
107 $log = _$log_;
108 $timeout = _$timeout_;
109 fs = _fs_;
110
111 init();
112
113 return {
114 showTooltip: showTooltip,
115 cancelTooltip: cancelTooltip
116 };
117 }]);
118}());