Skip to content

Commit

Permalink
added the restoration method
Browse files Browse the repository at this point in the history
  • Loading branch information
louisameline committed Nov 8, 2014
1 parent 5520c68 commit 28fa841
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ offsetY
onlyOne
position
positionTracker
restoration
speed
timer
theme
Expand All @@ -55,6 +56,8 @@ $(...).tooltipster('enable')
$(...).tooltipster('destroy')
$(...).tooltipster('content')
$(...).tooltipster('content', myNewContent)
$(...).tooltipster('option', optionName)
$(...).tooltipster('option', optionName, optionValue)
$(...).tooltipster('reposition')
$(...).tooltipster('elementTooltip')
$(...).tooltipster('elementIcon')
11 changes: 11 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ <h2>Options</h2>
<td>function</td>
<td>Called after the tooltip has been repositioned by the position tracker (if enabled). <strong>Default:</strong> A function that will close the tooltip if the trigger is 'hover' and autoClose is false.</td>
</tr>
<tr>
<td><h4>restoration</h4></td>
<td>'none', 'previous' or 'current'</td>
<td>Specify if a TITLE attribute should be restored on the HTML element after a call to the 'destroy' method. This attribute may be omitted, or be restored with the value that existed before Tooltipster was initialized, or be restored with the stringified value of the current content. Note: in case of multiple tooltips on a single element, only the last destroyed tooltip may trigger a restoration. <strong>Default: 'current'</strong></td>
</tr>
<tr>
<td><h4>speed</h4></td>
<td>integer</td>
Expand Down Expand Up @@ -483,6 +488,12 @@ <h3>The awesomesauce Tooltipster API</h3>
// update tooltip content
$(...).tooltipster('content', myNewContent);

// get the value of an option
$(...).tooltipster('option', optionName);

// set the value of an option (use at your own risk, we do not provide support for issues you may encounter when using this method)
$(...).tooltipster('option', optionName, optionValue);

// reposition and resize the tooltip
$(...).tooltipster('reposition');

Expand Down
54 changes: 41 additions & 13 deletions js/jquery.tooltipster.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Tooltipster 3.3.0 | 2014-09-28
Tooltipster 3.3.0 | 2014-11-08
A rockin' custom tooltip jQuery plugin
Developed by Caleb Jacob under the MIT license http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -51,6 +51,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
this.hide();
}
},
restoration: 'current',
speed: 350,
timer: 0,
theme: 'tooltipster-default',
Expand Down Expand Up @@ -110,16 +111,27 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

// note : the content is null (empty) by default and can stay that way if the plugin remains initialized but not fed any content. The tooltip will just not appear.

// if content is provided in the options, its has precedence over the title attribute. Remark : an empty string is considered content, only 'null' represents the absence of content.
// let's save the initial value of the title attribute for later restoration if need be.
var initialTitle = null;
// it will already have been saved in case of multiple tooltips
if (self.$el.data('tooltipster-initialTitle') === undefined) {

initialTitle = self.$el.attr('title');

// we do not want initialTitle to have the value "undefined" because of how jQuery's .data() method works
if (initialTitle === undefined) initialTitle = null;

self.$el.data('tooltipster-initialTitle', initialTitle);
}

// if content is provided in the options, its has precedence over the title attribute.
// Note : an empty string is considered content, only 'null' represents the absence of content.
// Also, an existing title="" attribute will result in an empty string content
if (self.options.content !== null){
self._content_set(self.options.content);
}
else {
// the same remark as above applies : empty strings (like title="") are considered content and will be shown. Do not define any attribute at all if you want to initialize the plugin without content at start.
var t = self.$el.attr('title');
if(typeof t === 'undefined') t = null;

self._content_set(t);
self._content_set(initialTitle);
}

var c = self.options.functionInit.call(self.$el, self.$el, self.Content);
Expand Down Expand Up @@ -1074,7 +1086,9 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
self.hide();

// remove the icon, if any
if(self.$el[0] !== self.$elProxy[0]) self.$elProxy.remove();
if (self.$el[0] !== self.$elProxy[0]) {
self.$elProxy.remove();
}

self.$el
.removeData(self.namespace)
Expand All @@ -1085,15 +1099,29 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
// if there are no more tooltips on this element
if(ns.length === 1){

// old school technique when outerHTML is not supported
var stringifiedContent = (typeof self.Content === 'string') ? self.Content : $('<div></div>').append(self.Content).html();
// optional restoration of a title attribute
var title = null;
if (self.options.restoration === 'previous'){
title = self.$el.data('tooltipster-initialTitle');
}
else if(self.options.restoration === 'current'){

// old school technique to stringify when outerHTML is not supported
title =
(typeof self.Content === 'string') ?
self.Content :
$('<div></div>').append(self.Content).html();
}

if (title) {
self.$el.attr('title', title);
}

// final cleaning
self.$el
.removeClass('tooltipstered')
.attr('title', stringifiedContent)
.removeData(self.namespace)
.removeData('tooltipster-ns')
.off('.'+ self.namespace);
.removeData('tooltipster-initialTitle');
}
else {
// remove the instance namespace from the list of namespaces of tooltips present on the element
Expand Down
Loading

0 comments on commit 28fa841

Please sign in to comment.