Browse Source

#317: + обновить дату по загрузки страницы

react
RubaXa 10 years ago
parent
commit
7bad7a3c1e
  1. 8
      index.html
  2. 131
      react-sortable-mixin.js
  3. 37
      st/app.js

8
index.html

@ -210,14 +210,6 @@
</div>
<!-- React mixin -->
<a name="react"></a>
<div class="container" style="margin-top: 100px">
<div id="react-box" style="margin-left: 30px;"></div>
<div style="clear: both"></div>
</div>
<!-- Code example -->
<a name="c"></a>
<div class="container" style="margin-top: 100px">

131
react-sortable-mixin.js vendored

@ -19,30 +19,12 @@
})(function (/** Sortable */Sortable) {
'use strict';
var _nextSibling;
/**
* 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 = {
sortableMixinVersion: '0.0.0',
/**
* @type {Sortable}
* @private
*/
_sortableInstance: null,
var _activeComponent;
/**
* Sortable options
* @returns {object}
*/
getDefaultProps: function () {
return {
sortable: {
var _defaultOptions = {
ref: 'list',
model: 'items',
@ -54,65 +36,100 @@
onRemove: 'handleRemove',
onSort: 'handleSort',
onFilter: 'handleFilter'
}
};
},
componentDidMount: function () {
var nextSibling,
sortableProps = this.props.sortable,
sortableOptions = {},
callMethod = function (/** string */type, /** Event */evt) {
var method = this[sortableProps[type]];
method && method.call(this, evt, this._sortableInstance);
}.bind(this);
function _getModelName(component) {
return component.sortableOptions && component.sortableOptions.model || _defaultOptions.model;
}
// Pass through unrecognized options
for (var key in sortableProps) {
sortableOptions[key] = sortableProps[key];
function _getModelItems(component) {
return component.state[_getModelName(component)].slice();
}
// Bind callbacks so that "this" refers to the component
'onEnd onAdd onUpdate onRemove onFilter'.split(' ').forEach(function (/** string */name) {
if (sortableProps[name]) {
sortableOptions[name] = callMethod.bind(this, name);
function _extend(dst, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) {
dst[key] = src[key];
}
}
}.bind(this));
return dst;
}
sortableOptions.onStart = function (/** Event */evt) {
nextSibling = evt.item.nextSibling;
callMethod('onStart', evt);
}.bind(this);
/**
* 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 = {
sortableMixinVersion: '0.1.0',
sortableOptions.onSort = function (/** Event */evt) {
evt.from.insertBefore(evt.item, nextSibling || null);
var modelName = sortableProps.model,
newState = {},
items = this.state[modelName];
/**
* @type {Sortable}
* @private
*/
_sortableInstance: null,
if (items) {
items = items.slice(); // clone
items.splice(evt.newIndex, 0, items.splice(evt.oldIndex, 1)[0]);
newState[modelName] = items;
componentDidMount: function () {
var options = _extend(_extend({}, _defaultOptions), this.sortableOptions || {}),
copyOptions = _extend({}, options),
emitEvent = function (/** string */type, /** Event */evt) {
var method = this[options[type]];
method && method.call(this, evt, this._sortableInstance);
}.bind(this);
// Bind callbacks so that "this" refers to the component
'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);
}
callMethod('onSort', evt);
setTimeout(function () {
emitEvent(name, evt);
}, 0);
}.bind(this);
}, this);
/** @namespace this.refs — http://facebook.github.io/react/docs/more-about-refs.html */
if (!sortableProps.ref || this.refs[sortableProps.ref]) {
this._sortableInstance = Sortable.create((this.refs[sortableProps.ref] || this).getDOMNode(), sortableOptions);
}
this._sortableInstance = Sortable.create((this.refs[options.ref] || this).getDOMNode(), copyOptions);
},

37
st/app.js

@ -195,43 +195,6 @@
$scope.sortableConfig['on' + name] = console.log.bind(console, name);
});
}]);
// React
loadScripts({
'React': '//fb.me/react-0.12.2.js',
'SortableMixin': 'react-sortable-mixin.js'
}, function (React, SortableMixin) {
var SortableList = React.createClass({
mixins: [SortableMixin],
getInitialState: function() {
return {
items: [
'Mixin',
'Sortable'
]
};
},
render: function() {
return React.DOM.div(null,
React.DOM.h4({ children: 'React mixin', className: 'layer title title_xl', style: { marginBottom: 0 } }),
React.DOM.div({ style: { width: '30%', marginLeft: '10px', cursor: 'move' }, className: 'block__list_words' },
React.DOM.ul({
ref: 'list',
children: this.state.items.map(function (v) {
return React.DOM.li(null, v);
})
})
)
);
}
});
React.render(React.createElement(SortableList, {}), byId('react-box'));
});
})();

Loading…
Cancel
Save