react-sortable-mixin.js 3.6 KB
Newer Older
R
RubaXa 已提交
1 2 3 4 5 6 7 8
/**
 * @author RubaXa <trash@rubaxa.org>
 * @licence MIT
 */

(function (factory) {
	'use strict';

R
RubaXa 已提交
9
	if (typeof module != 'undefined' && typeof module.exports != 'undefined') {
R
RubaXa 已提交
10
		module.exports = factory(require('./Sortable'));
R
RubaXa 已提交
11
	}
R
RubaXa 已提交
12
	else if (typeof define === 'function' && define.amd) {
R
RubaXa 已提交
13
		define(['./Sortable'], factory);
R
RubaXa 已提交
14
	}
R
RubaXa 已提交
15 16 17 18 19 20 21
	else {
		/* jshint sub:true */
		window['SortableMixin'] = factory(Sortable);
	}
})(function (/** Sortable */Sortable) {
	'use strict';

R
RubaXa 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	var _nextSibling;

	var _activeComponent;

	var _defaultOptions = {
		ref: 'list',
		model: 'items',

		animation: 100,
		onStart: 'handleStart',
		onEnd: 'handleEnd',
		onAdd: 'handleAdd',
		onUpdate: 'handleUpdate',
		onRemove: 'handleRemove',
		onSort: 'handleSort',
		onFilter: 'handleFilter'
	};


	function _getModelName(component) {
		return component.sortableOptions && component.sortableOptions.model || _defaultOptions.model;
	}


	function _getModelItems(component) {
R
RubaXa 已提交
47 48 49 50
		var name = _getModelName(component),
			items = component.state && component.state[name] || component.props[name];

		return items.slice();
R
RubaXa 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
	}


	function _extend(dst, src) {
		for (var key in src) {
			if (src.hasOwnProperty(key)) {
				dst[key] = src[key];
			}
		}

		return dst;
	}

R
RubaXa 已提交
64 65 66 67 68 69 70 71

	/**
	 * Simple and easy mixin-wrapper for rubaxa/Sortable library, in order to
	 * make reorderable drag-and-drop lists on modern browsers and touch devices.
	 *
	 * @mixin
	 */
	var SortableMixin = {
R
RubaXa 已提交
72 73
		sortableMixinVersion: '0.1.0',

74 75 76 77 78 79 80 81 82

		/**
		 * @type {Sortable}
		 * @private
		 */
		_sortableInstance: null,


		componentDidMount: function () {
R
RubaXa 已提交
83 84
			var options = _extend(_extend({}, _defaultOptions), this.sortableOptions || {}),
				copyOptions = _extend({}, options),
85

R
RubaXa 已提交
86 87
				emitEvent = function (/** string */type, /** Event */evt) {
					var method = this[options[type]];
88 89 90 91 92
					method && method.call(this, evt, this._sortableInstance);
				}.bind(this);


			// Bind callbacks so that "this" refers to the component
R
RubaXa 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
			'onStart onEnd onAdd onSort onUpdate onRemove onFilter'.split(' ').forEach(function (/** string */name) {
				copyOptions[name] = function (evt) {
					if (name === 'onStart') {
						_nextSibling = evt.item.nextElementSibling;
						_activeComponent = this;
					}
					else if (name === 'onAdd' || name === 'onUpdate') {
						evt.from.insertBefore(evt.item, _nextSibling);

						var newState = {},
							remoteState = {},
							oldIndex = evt.oldIndex,
							newIndex = evt.newIndex,
							items = _getModelItems(this),
							remoteItems,
							item;

						if (name === 'onAdd') {
							remoteItems = _getModelItems(_activeComponent);
							item = remoteItems.splice(oldIndex, 1)[0];
							items.splice(newIndex, 0, item);

							remoteState[_getModelName(_activeComponent)] = remoteItems;
						}
						else {
							items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]);
						}

						newState[_getModelName(this)] = items;
						this.setState(newState);
						(this !== _activeComponent) && _activeComponent.setState(remoteState);
					}

					setTimeout(function () {
						emitEvent(name, evt);
					}, 0);
				}.bind(this);
			}, this);
131 132 133


			/** @namespace this.refs — http://facebook.github.io/react/docs/more-about-refs.html */
R
RubaXa 已提交
134
			this._sortableInstance = Sortable.create((this.refs[options.ref] || this).getDOMNode(), copyOptions);
135 136
		},

137 138 139 140 141
		componentWillReceiveProps: function (nextProps) {
			var newState = {},
				modelName = _getModelName(this),
				items;

142 143 144
			items = nextProps[modelName];

			if (items) {
145 146 147 148
				newState[modelName] = items;
				this.setState(newState);
			}
		},
149 150 151 152 153

		componentWillUnmount: function () {
			this._sortableInstance.destroy();
			this._sortableInstance = null;
		}
R
RubaXa 已提交
154 155 156 157 158 159
	};


	// Export
	return SortableMixin;
});