blob: 8996ec8b24d971b720954d9163cacdf57ede7ac1 [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) {
Simon Huntd552ee92015-04-02 17:06:35 -070067 // tooltips don't make sense on mobile devices
68 if (!el || !msg || fs.isMobile()) {
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070069 return;
70 }
Simon Huntd552ee92015-04-02 17:06:35 -070071
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070072 var elem = d3.select(el),
73 mouseX = d3.event.pageX,
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070074 mouseY = d3.event.pageY,
75 style = tipStyle(mouseX, mouseY);
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070076 currElemId = elem.attr('id');
77
78 tooltip.transition()
79 .delay(hoverDelay)
80 .each('start', function () {
81 d3.select(this).style('display', 'none');
82 })
83 .each('end', function () {
Bri Prebilic Cole6ffea6b2015-03-23 13:46:14 -070084 d3.select(this).style(style)
Bri Prebilic Cole54d09382015-03-19 18:40:27 -070085 .text(msg);
86 });
87 }
88
89 function cancelTooltip(el) {
90 if (!el) {
91 return;
92 }
93 var elem = d3.select(el);
94
95 if (elem.attr('id') === currElemId) {
96 tooltip.transition()
97 .delay(exitDelay)
98 .style({
99 display: 'none'
100 })
101 .text('');
102 }
103 }
104
105 angular.module('onosWidget')
106 .factory('TooltipService', ['$log', '$timeout', 'FnService',
107
108 function (_$log_, _$timeout_, _fs_) {
109 $log = _$log_;
110 $timeout = _$timeout_;
111 fs = _fs_;
112
113 init();
114
115 return {
116 showTooltip: showTooltip,
117 cancelTooltip: cancelTooltip
118 };
119 }]);
120}());