Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to support Ractive 0.7.3 #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Ractive.js sortable decorator plugin
====================================

*Find more Ractive.js plugins at [ractivejs.org/plugins](http://ractivejs.org/plugins)*
*Find more Ractive.js plugins at [docs.ractivejs.org/latest/plugins](http://docs.ractivejs.org/latest/plugins)*

[See the demo here.](http://ractivejs.github.io/Ractive-decorators-sortable/)

Expand Down
32 changes: 22 additions & 10 deletions Ractive-decorators-sortable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Ractive-decorators-sortable
===========================

Version 0.1.0.
Version 0.2.0.

This plugin adds a 'sortable' decorator to Ractive, which enables
elements that correspond to array members to be re-ordered using
Expand Down Expand Up @@ -58,18 +58,18 @@

*/

(function ( global, factory ) {
var sortableDecorator = (function ( global, factory ) {

'use strict';

// Common JS (i.e. browserify) environment
if ( typeof module !== 'undefined' && module.exports && typeof require === 'function' ) {
factory( require( 'Ractive' ) );
factory( require( 'ractive' ) );
}

// AMD?
else if ( typeof define === 'function' && define.amd ) {
define([ 'Ractive' ], factory );
define([ 'ractive' ], factory );
}

// browser global
Expand Down Expand Up @@ -126,7 +126,7 @@
dragstartHandler = function ( event ) {
var storage = this._ractive, lastDotIndex;

sourceKeypath = storage.keypath;
sourceKeypath = storage.keypath.str;

// this decorator only works with array members!
lastDotIndex = sourceKeypath.lastIndexOf( '.' );
Expand Down Expand Up @@ -156,7 +156,7 @@
return;
}

targetKeypath = this._ractive.keypath;
targetKeypath = this._ractive.keypath.str;

// this decorator only works with array members!
lastDotIndex = targetKeypath.lastIndexOf( '.' );
Expand All @@ -173,13 +173,16 @@
return;
}

// add droptarget class to hovered element
this.classList.add( sortable.targetClass );

// if it's the same index, add droptarget class then abort
if ( targetIndex === sourceIndex ) {
this.classList.add( sortable.targetClass );
return;
}

array = ractive.get( sourceArray );
// cloning array avoids issues on some generated arrays (TODO investigate)
array = ractive.get( sourceArray ).slice();

// remove source from array
source = array.splice( sourceIndex, 1 )[0];
Expand All @@ -189,6 +192,9 @@

// add source back to array in new location
array.splice( sourceIndex, 0, source );

// replace source with updated order from cloned-array (due to .slice())
ractive.set( sourceArray, array );
};

removeTargetClass = function () {
Expand All @@ -198,5 +204,11 @@
preventDefault = function ( event ) { event.preventDefault(); };

Ractive.decorators.sortable = sortable;

}));

return sortable;
}));

// Common JS (i.e. browserify) environment
if ( typeof module !== 'undefined' && module.exports) {
module.exports = sortableDecorator;
}
2 changes: 1 addition & 1 deletion Ractive-decorators-sortable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<h1>Ractive.js sortable decorator plugin</h1>
<p class='strap'>
<span class='download'>download: <a href='https://raw.github.com/RactiveJS/Ractive-decorators-sortable/master/Ractive-decorators-sortable.js'>Ractive-decorators-sortable.js</a></span>
<span class='more-plugins'>more plugins at <a href='http://ractivejs.org/plugins'>ractivejs.org/plugins</a></span>
<span class='more-plugins'>more plugins at <a href='http://docs.ractivejs.org/latest/plugins'>docs.ractivejs.org/latest/plugins</a></span>
</p>
</header>

Expand Down
Loading