Skip to content

Commit

Permalink
Cleanup Link code
Browse files Browse the repository at this point in the history
  • Loading branch information
leongersen committed Apr 4, 2014
1 parent 845d03f commit 071e3c2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 100 deletions.
126 changes: 58 additions & 68 deletions Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,48 @@ var
return new Link.prototype.init( entry['target']||function(){}, entry['method'], entry['format']||{}, update );
}

// Initialisor
Link.prototype.init = function ( target, method, format, update ) {
Link.prototype.setTooltip = function ( target, method ) {

// By default, use the 'html' method.
this.method = method || 'html';

// Use jQuery to create the element
this.el = $( target.replace('-tooltip-', '') || '<div/>' )[0];
};

Link.prototype.setHidden = function ( target ) {

this.method = 'val';

this.el = document.createElement('input');
this.el.name = target;
this.el.type = 'hidden';
};

Link.prototype.setField = function ( target ) {

// Returns nulled array.
function at(a,b,c){
return [c?a:b, c?b:a];
}

// In IE < 9, .bind() isn't available, need this link in .change().
var that = this;
var that = this;

// Default to .val if this is an input element.
this.method = 'val';
// Set the slider to a new value on change.
this.target = target.on('change', function( e ){
that.obj.val(
at(null, $(e.target).val(), that.N),
{ 'link': that }
);
});
};


// Initialisor
Link.prototype.init = function ( target, method, format, update ) {

// Write all formatting to this object.
// No validation needed, as we'll merge these with the parent
Expand All @@ -246,93 +283,50 @@ var
// Store the update option.
this.update = !update;

var
// Find the type of this link.
isTooltip = ( typeof target === 'string' && target.indexOf('-tooltip-') === 0 ),
isHidden = ( typeof target === 'string' && target.indexOf('-') !== 0 ),
isMethod = ( typeof target === 'function' ),
is$ = ( isInstance(target) ),
isInput = ( is$ && target.is('input, select, textarea') ),
methodIsFunction = ( is$ && typeof method === 'function' ),
methodIsName = ( is$ && typeof method === 'string' && target[method] );

// If target is a string, a new hidden input will be created.
if ( isTooltip ) {

// By default, use the 'html' method.
this.method = method || 'html';

// Use jQuery to create the element
this.el = $( target.replace('-tooltip-', '') || '<div/>' )[0];

if ( typeof target === 'string' && target.indexOf('-tooltip-') === 0 ) {
this.setTooltip( target, method );
return;
}

// If the string doesn't begin with '-', which is reserved, add a new hidden input.
if ( isHidden ) {

this.method = 'val';

this.el = document.createElement('input');
this.el.name = target;
this.el.type = 'hidden';

if ( typeof target === 'string' && target.indexOf('-') !== 0 ) {
this.setHidden( target, method );
return;
}

// The target can also be a function, which will be called.
if ( isMethod ) {
if ( typeof target === 'function' ) {
this.target = false;
this.method = target;
return;
}

// If the target is and $ element.
if ( is$ ) {

// The method must exist on the element.
if ( method && ( methodIsFunction || methodIsName ) ) {
this.target = target;
this.method = method;
return;
}

if ( isInstance(target) ) {
// If a jQuery/Zepto input element is provided, but no method is set,
// the element can assume it needs to respond to 'change'...
if ( !method && isInput ) {

// Default to .val if this is an input element.
this.method = 'val';
this.target = target;

// Set the slider to a new value on change.
this.target.on('change', function( e ){

// Returns null array.
function at(a,b,c){
return [c?a:b, c?b:a];
}

var output = at(null, $(e.target).val(), that.N);
if ( !method ) {

that.obj.val(output, { 'link': that });
});
if ( target.is('input, select, textarea') ) {
this.setField( target );
return;
}

return;
// If no method is set, and we are not auto-binding an input, default to 'html'.
method = 'html';
}

// ... or not.
if ( !method && !isInput ) {

// Default arbitrarily to 'html'.
this.method = 'html';
// The method must exist on the element.
if ( typeof method === 'function' || (typeof method === 'string' && target[method]) ) {
this.method = method;
this.target = target;

return;
}
}

throw new RangeError("Link: Invalid Link.");
// Nothing matched, throw error.
throw new RangeError("(Link) Invalid Link.");
}

// Provides external items with the slider value.
Expand Down Expand Up @@ -367,10 +361,6 @@ var
this.formatting = new Format( $.extend({}, options, this.formatting ));
};

// Link.prototype.setScope = function ( scope ) {
// this.scope = scope;
// }

Link.prototype.setObject = function ( obj ) {
this.obj = obj;
}
Expand Down
12 changes: 9 additions & 3 deletions jquery.nouislider.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ $.fn.noUiSlider - WTFPL - refreshless.com/nouislider/ */
} else if ( entry === false ) {
parsed.connect = 0;
} else {
throwError("'connect' option was doesn't match handle count.");
throwError("'connect' option doesn't match handle count.");
}
}

Expand Down Expand Up @@ -1066,7 +1066,7 @@ function closure ( target, options, originalOptions ){

var args = Array.prototype.slice.call( arguments, 0 ),
callback, link, update, animate,
i, actual, to, values = asArray( args[0] );
i, count, actual, to, values = asArray( args[0] );

// Extract modifiers for value method.
if ( typeof args[1] === 'object' ) {
Expand All @@ -1091,10 +1091,16 @@ function closure ( target, options, originalOptions ){
addClassFor( $Target, Classes[14], 300 );
}

// Determine how often to set the handles.
count = $Handles.length > 1 ? 3 : 1;
if ( values.length === 1 ) {
count = 1;
}

// If there are multiple handles to be set run the setting
// mechanism twice for the first handle, to make sure it
// can be bounced of the second one properly.
for ( i = 0; i < ( $Handles.length > 1 ? 3 : 1 ); i++ ) {
for ( i = 0; i < count; i++ ) {

to = link || $Serialization[i%2][0];
to = to.getValue( values[i%2] );
Expand Down
34 changes: 5 additions & 29 deletions tests/run.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,25 @@
height: 150px;
}

section {
border-bottom: 2px solid #888;
margin-bottom: 80px;
padding-bottom: 40px;
}

.tooltip {
display: none;
position: absolute;
border: 1px solid #D9D9D9;
font: 400 12px/12px Arial;
border-radius: 3px;
top: 40px;
padding: 5px;
left: 17px;
}
.tooltip.top {
top: auto;
bottom: 40px;
}
.noUi-active .tooltip {
display: block;
}

</style>

<link href="http://code.jquery.com/qunit/qunit-1.12.0.css" rel="stylesheet">
<link href="../jquery.nouislider.css" rel="stylesheet">

<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<script src="../../classVal/jquery.classval.js"></script>
<script src="../Link.js"></script>
<script src="../jquery.nouislider.js"></script>
<script src="../../compile/result/jquery.nouislider.min.js"></script>
<script>var Link = $.noUiSlider.Link;</script>

<!-- Shameless in-test debugging
<script>ok = deepEqual = equal = throws = $.noop; test = function(a,b){$(b)};</script>
<style>#qunit-fixture { position: static; }</style> -->

</head>

<body>

<div id="qunit"></div>
<div id="qunit-fixture"></div>

<style>/* #qunit-fixture { position: static; } *//* Shameless in-test debugging */</style>

<script>
var Q = $("#qunit-fixture");
Expand Down

0 comments on commit 071e3c2

Please sign in to comment.