2013-10-12 16:05:13 +00:00
/ * \
2013-11-08 08:47:00 +00:00
title : $ : / c o r e / m o d u l e s / w i d g e t s / l i n k . j s
2013-10-12 16:05:13 +00:00
type : application / javascript
2013-11-08 08:47:00 +00:00
module - type : widget
2013-10-12 16:05:13 +00:00
Link widget
\ * /
( function ( ) {
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict" ;
2013-11-08 08:47:00 +00:00
var Widget = require ( "$:/core/modules/widgets/widget.js" ) . widget ;
2013-10-12 16:05:13 +00:00
var LinkWidget = function ( parseTreeNode , options ) {
this . initialise ( parseTreeNode , options ) ;
} ;
/ *
Inherit from the base widget class
* /
LinkWidget . prototype = new Widget ( ) ;
/ *
Render this widget into the DOM
* /
LinkWidget . prototype . render = function ( parent , nextSibling ) {
// Save the parent dom node
this . parentDomNode = parent ;
// Compute our attributes
this . computeAttributes ( ) ;
// Execute our logic
this . execute ( ) ;
2014-08-28 21:28:02 +00:00
// Get the value of the tv-wikilinks configuration macro
var wikiLinksMacro = this . getVariable ( "tv-wikilinks" ) ,
2014-08-30 19:44:26 +00:00
useWikiLinks = wikiLinksMacro ? ( wikiLinksMacro . trim ( ) !== "no" ) : true ;
2013-10-21 17:31:04 +00:00
// Render the link if required
if ( useWikiLinks ) {
this . renderLink ( parent , nextSibling ) ;
} else {
// Just insert the link text
var domNode = this . document . createElement ( "span" ) ;
parent . insertBefore ( domNode , nextSibling ) ;
this . renderChildren ( domNode , null ) ;
this . domNodes . push ( domNode ) ;
}
} ;
/ *
Render this widget into the DOM
* /
LinkWidget . prototype . renderLink = function ( parent , nextSibling ) {
2013-10-21 19:20:32 +00:00
var self = this ;
2013-10-12 16:05:13 +00:00
// Create our element
var domNode = this . document . createElement ( "a" ) ;
// Assign classes
2014-08-08 16:19:48 +00:00
var classes = [ ] ;
if ( this . linkClasses ) {
classes . push ( this . linkClasses ) ;
}
2014-08-28 17:13:46 +00:00
classes . push ( "tc-tiddlylink" ) ;
2013-10-12 16:05:13 +00:00
if ( this . isShadow ) {
2014-08-28 17:13:46 +00:00
classes . push ( "tc-tiddlylink-shadow" ) ;
2013-10-12 16:05:13 +00:00
}
if ( this . isMissing && ! this . isShadow ) {
2014-08-28 17:13:46 +00:00
classes . push ( "tc-tiddlylink-missing" ) ;
2013-10-12 16:05:13 +00:00
} else {
if ( ! this . isMissing ) {
2014-08-28 17:13:46 +00:00
classes . push ( "tc-tiddlylink-resolves" ) ;
2013-10-12 16:05:13 +00:00
}
}
2014-04-05 16:37:58 +00:00
domNode . setAttribute ( "class" , classes . join ( " " ) ) ;
2013-10-12 16:05:13 +00:00
// Set an href
2014-08-28 21:28:02 +00:00
var wikiLinkTemplateMacro = this . getVariable ( "tv-wikilink-template" ) ,
2013-10-21 17:31:04 +00:00
wikiLinkTemplate = wikiLinkTemplateMacro ? wikiLinkTemplateMacro . trim ( ) : "#$uri_encoded$" ,
wikiLinkText = wikiLinkTemplate . replace ( "$uri_encoded$" , encodeURIComponent ( this . to ) ) ;
wikiLinkText = wikiLinkText . replace ( "$uri_doubleencoded$" , encodeURIComponent ( encodeURIComponent ( this . to ) ) ) ;
domNode . setAttribute ( "href" , wikiLinkText ) ;
2014-03-08 16:06:57 +00:00
// Set the tooltip
2014-04-05 16:37:58 +00:00
// HACK: Performance issues with re-parsing the tooltip prevent us defaulting the tooltip to "<$transclude field='tooltip'><$transclude field='title'/></$transclude>"
2014-08-28 21:28:02 +00:00
var tooltipWikiText = this . tooltip || this . getVariable ( "tv-wikilink-tooltip" ) ;
2014-03-08 16:06:57 +00:00
if ( tooltipWikiText ) {
var tooltipText = this . wiki . renderText ( "text/plain" , "text/vnd.tiddlywiki" , tooltipWikiText , {
parseAsInline : true ,
variables : {
currentTiddler : this . to
} ,
parentWidget : this
} ) ;
domNode . setAttribute ( "title" , tooltipText ) ;
2014-03-06 12:38:47 +00:00
}
2014-06-17 06:54:10 +00:00
if ( this [ "aria-label" ] ) {
domNode . setAttribute ( "aria-label" , this [ "aria-label" ] ) ;
}
2013-10-12 16:05:13 +00:00
// Add a click event handler
2013-10-25 22:22:46 +00:00
$tw . utils . addEventListeners ( domNode , [
2013-10-26 07:36:43 +00:00
{ name : "click" , handlerObject : this , handlerMethod : "handleClickEvent" } ,
{ name : "dragstart" , handlerObject : this , handlerMethod : "handleDragStartEvent" } ,
{ name : "dragend" , handlerObject : this , handlerMethod : "handleDragEndEvent" }
2013-10-25 22:22:46 +00:00
] ) ;
2013-10-12 16:05:13 +00:00
// Insert the link into the DOM and render any children
parent . insertBefore ( domNode , nextSibling ) ;
this . renderChildren ( domNode , null ) ;
this . domNodes . push ( domNode ) ;
} ;
2013-10-25 22:22:46 +00:00
LinkWidget . prototype . handleClickEvent = function ( event ) {
// Send the click on it's way as a navigate event
var bounds = this . domNodes [ 0 ] . getBoundingClientRect ( ) ;
this . dispatchEvent ( {
2014-08-28 20:43:44 +00:00
type : "tm-navigate" ,
2013-10-25 22:22:46 +00:00
navigateTo : this . to ,
2013-12-17 15:42:53 +00:00
navigateFromTitle : this . getVariable ( "storyTiddler" ) ,
2013-10-25 22:22:46 +00:00
navigateFromNode : this ,
navigateFromClientRect : { top : bounds . top , left : bounds . left , width : bounds . width , right : bounds . right , bottom : bounds . bottom , height : bounds . height
2014-02-12 22:00:12 +00:00
} ,
2014-03-03 09:09:13 +00:00
navigateSuppressNavigation : event . metaKey || event . ctrlKey || ( event . button === 1 )
2013-10-25 22:22:46 +00:00
} ) ;
event . preventDefault ( ) ;
event . stopPropagation ( ) ;
return false ;
} ;
2013-10-26 07:36:43 +00:00
LinkWidget . prototype . handleDragStartEvent = function ( event ) {
if ( this . to ) {
// Set the dragging class on the element being dragged
2014-08-28 17:13:46 +00:00
$tw . utils . addClass ( event . target , "tc-tiddlylink-dragging" ) ;
2013-10-26 07:36:43 +00:00
// Create the drag image elements
this . dragImage = this . document . createElement ( "div" ) ;
2014-08-28 17:59:35 +00:00
this . dragImage . className = "tc-tiddler-dragger" ;
2013-10-26 07:36:43 +00:00
var inner = this . document . createElement ( "div" ) ;
2014-08-28 17:59:35 +00:00
inner . className = "tc-tiddler-dragger-inner" ;
2013-10-26 07:36:43 +00:00
inner . appendChild ( this . document . createTextNode ( this . to ) ) ;
this . dragImage . appendChild ( inner ) ;
this . document . body . appendChild ( this . dragImage ) ;
// Astoundingly, we need to cover the dragger up: http://www.kryogenix.org/code/browser/custom-drag-image.html
2013-12-17 13:39:46 +00:00
var cover = this . document . createElement ( "div" ) ;
2014-08-28 17:59:35 +00:00
cover . className = "tc-tiddler-dragger-cover" ;
2013-12-17 13:39:46 +00:00
cover . style . left = ( inner . offsetLeft - 16 ) + "px" ;
cover . style . top = ( inner . offsetTop - 16 ) + "px" ;
cover . style . width = ( inner . offsetWidth + 32 ) + "px" ;
cover . style . height = ( inner . offsetHeight + 32 ) + "px" ;
2013-10-26 07:36:43 +00:00
this . dragImage . appendChild ( cover ) ;
// Set the data transfer properties
var dataTransfer = event . dataTransfer ;
2013-12-17 13:32:15 +00:00
// First the image
2013-10-26 07:36:43 +00:00
dataTransfer . effectAllowed = "copy" ;
2013-12-13 01:32:06 +00:00
if ( dataTransfer . setDragImage ) {
dataTransfer . setDragImage ( this . dragImage . firstChild , - 16 , - 16 ) ;
}
2013-12-17 13:32:15 +00:00
// Then the data
2013-10-26 07:36:43 +00:00
dataTransfer . clearData ( ) ;
2013-12-17 13:32:15 +00:00
var jsonData = this . wiki . getTiddlerAsJson ( this . to ) ,
textData = this . wiki . getTiddlerText ( this . to , "" ) ;
// IE doesn't like these content types
2013-12-21 03:57:05 +00:00
if ( ! $tw . browser . isIE ) {
2013-12-17 13:32:15 +00:00
dataTransfer . setData ( "text/vnd.tiddler" , jsonData ) ;
dataTransfer . setData ( "text/plain" , textData ) ;
2013-12-21 03:57:05 +00:00
dataTransfer . setData ( "text/x-moz-url" , "data:text/vnd.tiddler," + encodeURI ( jsonData ) ) ;
2013-12-13 11:12:56 +00:00
}
2013-12-17 13:32:15 +00:00
dataTransfer . setData ( "URL" , "data:text/vnd.tiddler," + encodeURI ( jsonData ) ) ;
dataTransfer . setData ( "Text" , textData ) ;
2013-10-26 07:36:43 +00:00
event . stopPropagation ( ) ;
} else {
event . preventDefault ( ) ;
}
} ;
LinkWidget . prototype . handleDragEndEvent = function ( event ) {
// Remove the dragging class on the element being dragged
2014-08-28 17:13:46 +00:00
$tw . utils . removeClass ( event . target , "tc-tiddlylink-dragging" ) ;
2013-10-26 07:36:43 +00:00
// Delete the drag image element
if ( this . dragImage ) {
this . dragImage . parentNode . removeChild ( this . dragImage ) ;
}
} ;
2013-10-12 16:05:13 +00:00
/ *
Compute the internal state of the widget
* /
LinkWidget . prototype . execute = function ( ) {
// Get the target tiddler title
2013-10-28 23:40:45 +00:00
this . to = this . getAttribute ( "to" , this . getVariable ( "currentTiddler" ) ) ;
2014-06-17 06:54:10 +00:00
// Get the link title and aria label
2014-03-06 12:38:47 +00:00
this . tooltip = this . getAttribute ( "tooltip" ) ;
2014-06-17 06:54:10 +00:00
this [ "aria-label" ] = this . getAttribute ( "aria-label" ) ;
2014-08-08 16:19:48 +00:00
// Get the link classes
this . linkClasses = this . getAttribute ( "class" ) ;
2013-10-12 16:05:13 +00:00
// Determine the link characteristics
this . isMissing = ! this . wiki . tiddlerExists ( this . to ) ;
this . isShadow = this . wiki . isShadowTiddler ( this . to ) ;
// Make the child widgets
this . makeChildWidgets ( ) ;
} ;
/ *
Selectively refreshes the widget if needed . Returns true if the widget or any of its children needed re - rendering
* /
LinkWidget . prototype . refresh = function ( changedTiddlers ) {
var changedAttributes = this . computeAttributes ( ) ;
2014-06-17 06:54:10 +00:00
if ( changedAttributes . to || changedTiddlers [ this . to ] || changedAttributes [ "aria-label" ] || changedAttributes . tooltip ) {
2013-10-12 16:05:13 +00:00
this . refreshSelf ( ) ;
return true ;
}
return this . refreshChildren ( changedTiddlers ) ;
} ;
exports . link = LinkWidget ;
} ) ( ) ;