From 1999bcf2aed40a007281aeab4169c726f80d526f Mon Sep 17 00:00:00 2001 From: owen-m1 Date: Tue, 18 Dec 2018 18:50:10 -0500 Subject: [PATCH] first version --- .gitignore | 3 ++ README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++-- jquery-sortable.js | 76 +++++++++++++++++++++++++++++++++++++++++++ package.json | 26 +++++++++------ 4 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 jquery-sortable.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6a02ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/example/ +/node_modules/ +/package-lock.json \ No newline at end of file diff --git a/README.md b/README.md index 8ce3e35..fe296ec 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,80 @@ -# THIS PROJECT NEEDS A MAINTAINER. +# jQuery SortableJS ---- +A jQuery binding for SortableJS -# HELP WANTED ;] +## Installation + +### Webpack/Browserify +Install package: +``` +npm i -D jquery-sortablejs +``` + +Import into project: +```javascript +import 'jquery-sortablejs'; +``` + +### CDN: +```HTML + +``` + +## Usage + +### Initialization +```javascript +// Without options: +$('#my-list').sortable(); + +// With options: +$('#my-list').sortable({ + // SortableJS options go here + // See: (https://github.com/SortableJS/Sortable#options) + + handle: '.handle', + invertSwap: true, + // . . . +}) +``` + +### Getting Sortable instance +```javascript +$('#my-list').sortable(); + + +var mySortableList = $('#my-list').sortable('widget'); +``` + +### Getting/Setting options +Call `.sortable()` with the first argument as the option name and the second as the option value +```javascript +// Get an option +var isDisabled = $('#my-list').sortable('disabled'); + +// Set an option +$('#my-list').sortable('disabled', !isDisabled); +``` + +### Destroying Sortable +Pass `'destroy'` argument into `.sortable()` +```javascript +// Destroy Sortable +$('#my-list').sortable('destroy'); +``` + +### Calling Sortable functions +Pass the name of the function as string into `.sortable()`, followed by any arguments +```javascript +// Sortable toArray +var order = $('#my-list').sortable('toArray'); +``` + +## License +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/jquery-sortable.js b/jquery-sortable.js new file mode 100644 index 0000000..caea8fe --- /dev/null +++ b/jquery-sortable.js @@ -0,0 +1,76 @@ +(function (factory) { + "use strict"; + var sortable, + jq, + _this = this + ; + + if (typeof define === "function" && define.amd) { + try { + define(["sortablejs", "jquery"], function(Sortable, $) { + sortable = Sortable; + jq = $; + checkErrors(); + factory(Sortable, $); + }); + } catch(err) { + checkErrors(); + } + return; + } else if (typeof exports === 'object') { + try { + sortable = require('sortablejs'); + jq = require('jquery'); + } catch(err) { } + } + + if (typeof jQuery === 'function' || typeof $ === 'function') { + jq = jQuery || $; + } + + if (typeof Sortable !== 'undefined') { + sortable = Sortable; + } + + function checkErrors() { + if (!jq) { + throw new Error('jQuery is required for jquery-sortablejs'); + } + + if (!sortable) { + throw new Error('SortableJS is required for jquery-sortablejs (https://github.com/SortableJS/Sortable)'); + } + } + checkErrors(); + factory(sortable, jq); +})(function (Sortable, $) { + "use strict"; + + $.fn.sortable = function (options) { + var retVal, + args = arguments; + + this.each(function () { + var $el = $(this), + sortable = $el.data('sortable'); + + if (!sortable && (options instanceof Object || !options)) { + sortable = new Sortable(this, options); + $el.data('sortable', sortable); + } else if (sortable) { + if (options === 'destroy') { + sortable.destroy(); + $el.removeData('sortable'); + } else if (options === 'widget') { + retVal = sortable; + } else if (typeof sortable[options] === 'function') { + retVal = sortable[options].apply(sortable, [].slice.call(args, 1)); + } else if (options in sortable.options) { + retVal = sortable.option.apply(sortable, args); + } + } + }); + + return (retVal === void 0) ? this : retVal; + }; +}); diff --git a/package.json b/package.json index f12ac36..104d0b9 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,31 @@ { "name": "jquery-sortablejs", - "version": "0.1.0", - "description": "A jQuery binding to SortableJS.", - "main": "jquery-sortable.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, + "version": "1.0.0", + "description": "A jQuery binding for SortableJS", + "main": "./jquery-sortable.js", "repository": { "type": "git", "url": "git+https://github.com/SortableJS/jquery-sortablejs.git" }, "keywords": [ - "jquery", - "sortable" + "sortable", + "jQuery", + "drag", + "drop", + "draggable", + "sort", + "reordering" ], - "author": "", + "peerDependencies": { + "jquery": "*", + "sortablejs": "*" + }, "license": "MIT", "bugs": { "url": "https://github.com/SortableJS/jquery-sortablejs/issues" }, + "files": [ + "jquery-sortable.js" + ], "homepage": "https://github.com/SortableJS/jquery-sortablejs#readme" }