Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
243a2093
Commit
243a2093
authored
Aug 15, 2018
by
Paul Slaughter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create TooltipOnTruncate component to show tooltip only when needed
parent
97572d42
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
229 additions
and
0 deletions
+229
-0
app/assets/javascripts/vue_shared/components/tooltip_on_truncate.vue
...javascripts/vue_shared/components/tooltip_on_truncate.vue
+67
-0
spec/javascripts/vue_shared/components/tooltip_on_truncate_spec.js
...scripts/vue_shared/components/tooltip_on_truncate_spec.js
+162
-0
No files found.
app/assets/javascripts/vue_shared/components/tooltip_on_truncate.vue
0 → 100644
View file @
243a2093
<
script
>
import
_
from
'
underscore
'
;
import
tooltip
from
'
../directives/tooltip
'
;
export
default
{
directives
:
{
tooltip
,
},
props
:
{
title
:
{
type
:
String
,
required
:
false
,
default
:
''
,
},
placement
:
{
type
:
String
,
required
:
false
,
default
:
'
top
'
,
},
truncateTarget
:
{
type
:
[
String
,
Function
],
required
:
false
,
default
:
''
,
},
},
data
()
{
return
{
showTooltip
:
false
,
};
},
mounted
()
{
const
target
=
this
.
selectTarget
();
if
(
target
&&
target
.
scrollWidth
>
target
.
offsetWidth
)
{
this
.
showTooltip
=
true
;
}
},
methods
:
{
selectTarget
()
{
if
(
_
.
isFunction
(
this
.
truncateTarget
))
{
return
this
.
truncateTarget
(
this
.
$el
);
}
else
if
(
this
.
truncateTarget
===
'
child
'
)
{
return
this
.
$el
.
childNodes
[
0
];
}
return
this
.
$el
;
},
},
};
</
script
>
<
template
>
<span
v-tooltip
v-if=
"showTooltip"
:title=
"title"
:data-placement=
"placement"
class=
"js-show-tooltip"
>
<slot></slot>
</span>
<span
v-else
>
<slot></slot>
</span>
</
template
>
spec/javascripts/vue_shared/components/tooltip_on_truncate_spec.js
0 → 100644
View file @
243a2093
import
{
mountComponentWithRender
}
from
'
spec/helpers/vue_mount_component_helper
'
;
import
TooltipOnTruncate
from
'
~/vue_shared/components/tooltip_on_truncate.vue
'
;
const
TEST_TITLE
=
'
lorem-ipsum-dolar-sit-amit-consectur-adipiscing-elit-sed-do
'
;
const
CLASS_SHOW_TOOLTIP
=
'
js-show-tooltip
'
;
const
STYLE_TRUNCATED
=
{
display
:
'
inline-block
'
,
'
max-width
'
:
'
20px
'
,
};
const
STYLE_NORMAL
=
{
display
:
'
inline-block
'
,
'
max-width
'
:
'
1000px
'
,
};
function
mountTooltipOnTruncate
(
options
,
createChildren
)
{
return
mountComponentWithRender
(
h
=>
h
(
TooltipOnTruncate
,
options
,
createChildren
(
h
)),
'
#app
'
);
}
describe
(
'
TooltipOnTruncate component
'
,
()
=>
{
let
vm
;
beforeEach
(()
=>
{
const
el
=
document
.
createElement
(
'
div
'
);
el
.
id
=
'
app
'
;
document
.
body
.
appendChild
(
el
);
});
afterEach
(()
=>
{
vm
.
$destroy
();
});
describe
(
'
with default target
'
,
()
=>
{
it
(
'
renders tooltip if truncated
'
,
done
=>
{
const
options
=
{
style
:
STYLE_TRUNCATED
,
props
:
{
title
:
TEST_TITLE
,
},
};
vm
=
mountTooltipOnTruncate
(
options
,
()
=>
[
TEST_TITLE
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
expect
(
vm
.
$el
).
toHaveData
(
'
original-title
'
,
TEST_TITLE
);
expect
(
vm
.
$el
).
toHaveData
(
'
placement
'
,
'
top
'
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
it
(
'
does not render tooltip if normal
'
,
done
=>
{
const
options
=
{
style
:
STYLE_NORMAL
,
props
:
{
title
:
TEST_TITLE
,
},
};
vm
=
mountTooltipOnTruncate
(
options
,
()
=>
[
TEST_TITLE
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
not
.
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
});
describe
(
'
with child target
'
,
()
=>
{
it
(
'
renders tooltip if truncated
'
,
done
=>
{
const
options
=
{
style
:
STYLE_NORMAL
,
props
:
{
title
:
TEST_TITLE
,
truncateTarget
:
'
child
'
,
},
};
vm
=
mountTooltipOnTruncate
(
options
,
(
h
)
=>
[
h
(
'
a
'
,
{
style
:
STYLE_TRUNCATED
},
TEST_TITLE
),
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
it
(
'
does not render tooltip if normal
'
,
done
=>
{
const
options
=
{
props
:
{
title
:
TEST_TITLE
,
truncateTarget
:
'
child
'
,
},
};
vm
=
mountTooltipOnTruncate
(
options
,
(
h
)
=>
[
h
(
'
a
'
,
{
style
:
STYLE_NORMAL
},
TEST_TITLE
),
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
not
.
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
});
describe
(
'
with fn target
'
,
()
=>
{
it
(
'
renders tooltip if truncated
'
,
done
=>
{
const
options
=
{
style
:
STYLE_NORMAL
,
props
:
{
title
:
TEST_TITLE
,
truncateTarget
:
(
el
)
=>
el
.
childNodes
[
1
],
},
};
vm
=
mountTooltipOnTruncate
(
options
,
(
h
)
=>
[
h
(
'
a
'
,
{
style
:
STYLE_NORMAL
},
TEST_TITLE
),
h
(
'
span
'
,
{
style
:
STYLE_TRUNCATED
},
TEST_TITLE
),
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
});
describe
(
'
placement
'
,
()
=>
{
it
(
'
sets data-placement when tooltip is rendered
'
,
done
=>
{
const
options
=
{
props
:
{
title
:
TEST_TITLE
,
truncateTarget
:
'
child
'
,
placement
:
'
bottom
'
,
},
};
vm
=
mountTooltipOnTruncate
(
options
,
(
h
)
=>
[
h
(
'
a
'
,
{
style
:
STYLE_TRUNCATED
},
TEST_TITLE
),
]);
vm
.
$nextTick
()
.
then
(()
=>
{
expect
(
vm
.
$el
).
toHaveClass
(
CLASS_SHOW_TOOLTIP
);
expect
(
vm
.
$el
).
toHaveData
(
'
placement
'
,
options
.
props
.
placement
);
})
.
then
(
done
)
.
catch
(
done
.
fail
);
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment