Browse Source

Fix move command

pull/36/head
Artur Arseniev 10 years ago
parent
commit
3b4335067c
  1. 5
      src/commands/view/MoveComponent.js
  2. 2
      src/demo.js
  3. 2
      src/dom_components/config/config.js
  4. 4
      src/dom_components/main.js
  5. 2
      src/dom_components/model/Component.js
  6. 102
      src/navigator/view/ItemSort.js
  7. 2
      src/navigator/view/ItemView.js

5
src/commands/view/MoveComponent.js

@ -28,6 +28,9 @@ define(['backbone', './SelectComponent','./SelectPosition'],
* @param {Event} e * @param {Event} e
* */ * */
initSorter: function(e){ initSorter: function(e){
var el = $(e.target).get('model');
if(!el.get('draggable'))
return;
if(this.sorter) if(this.sorter)
this.sorter.startSort(e.target); this.sorter.startSort(e.target);
this.stopSelectComponent(e); this.stopSelectComponent(e);
@ -50,7 +53,7 @@ define(['backbone', './SelectComponent','./SelectPosition'],
e.stopPropagation(); e.stopPropagation();
var $this = $(e.target); var $this = $(e.target);
if($this.data('model').get('movable')){ if($this.data('model').get('draggable')){
$this.addClass(this.hoverClass); $this.addClass(this.hoverClass);
this.attachBadge(e.target); this.attachBadge(e.target);
} }

2
src/demo.js

@ -9,7 +9,7 @@ require(['config/require-config'], function() {
container : '#gjs', container : '#gjs',
height: '100%', height: '100%',
fromElement: true, fromElement: true,
storage:{ autoload: 0 }, //storage:{ autoload: 0 },
commands: { commands: {
defaults : [{ defaults : [{

2
src/dom_components/config/config.js

@ -9,7 +9,7 @@ define(function () {
removable : false, removable : false,
copyable : false, copyable : false,
stylable : ['background','background-color','background-image', 'background-repeat','background-attachment','background-position'], stylable : ['background','background-color','background-image', 'background-repeat','background-attachment','background-position'],
movable : false, draggable : false,
badgable : false, badgable : false,
components: [], components: [],
}, },

4
src/dom_components/main.js

@ -132,7 +132,7 @@ define(function(require) {
* @param {string} [component.tagName='div'] Tag name * @param {string} [component.tagName='div'] Tag name
* @param {string} [component.type=''] Type of the component. Available: ''(default), 'text', 'image' * @param {string} [component.type=''] Type of the component. Available: ''(default), 'text', 'image'
* @param {boolean} [component.removable=true] If component is removable * @param {boolean} [component.removable=true] If component is removable
* @param {boolean} [component.movable=true] If is possible to move the component around the structure * @param {boolean} [component.draggable=true] If is possible to move the component around the structure
* @param {boolean} [component.droppable=true] If is possible to drop inside other components * @param {boolean} [component.droppable=true] If is possible to drop inside other components
* @param {boolean} [component.badgable=true] If the badge is visible when the component is selected * @param {boolean} [component.badgable=true] If the badge is visible when the component is selected
* @param {boolean} [component.stylable=true] If is possible to style component * @param {boolean} [component.stylable=true] If is possible to style component
@ -146,7 +146,7 @@ define(function(require) {
* var comp1 = ComponentsService.addComponent({ * var comp1 = ComponentsService.addComponent({
* tagName: 'div', * tagName: 'div',
* removable: true, // Can't remove it * removable: true, // Can't remove it
* movable: true, // Can't move it * draggable: true, // Can't move it
* copyable: true, // Disable copy/past * copyable: true, // Disable copy/past
* content: 'Content text', // Text inside component * content: 'Content text', // Text inside component
* style: { color: 'red'}, * style: { color: 'red'},

2
src/dom_components/model/Component.js

@ -8,7 +8,7 @@ define(['backbone','./Components', 'ClassManager/model/ClassTags'],
type: '', type: '',
editable: false, editable: false,
removable: true, removable: true,
movable: true, draggable: true,
droppable: true, droppable: true,
badgable: true, badgable: true,
stylable: true, stylable: true,

102
src/navigator/view/ItemSort.js

@ -1,10 +1,10 @@
define(['backbone'], define(['backbone'],
function(Backbone) { function(Backbone) {
/** /**
* @class ItemSort * @class ItemSort
* */ * */
return Backbone.View.extend({ return Backbone.View.extend({
initialize: function(o) { initialize: function(o) {
_.bindAll(this,'startMove','onMove','endMove','rollback', 'itemLeft'); _.bindAll(this,'startMove','onMove','endMove','rollback', 'itemLeft');
this.config = o.config || {}; this.config = o.config || {};
@ -13,12 +13,12 @@ define(['backbone'],
this.itemsClass = '.' + this.pfx + this.config.itemsClass; this.itemsClass = '.' + this.pfx + this.config.itemsClass;
this.setElement('.'+this.pfx+this.config.containerId); this.setElement('.'+this.pfx+this.config.containerId);
}, },
/** /**
* Picking component to move * Picking component to move
* @param {Object} Element view * @param {Object} Element view
* @param {Object} Event * @param {Object} Event
* *
* @return void * @return void
* */ * */
startMove: function(eV, e){ startMove: function(eV, e){
@ -26,20 +26,20 @@ define(['backbone'],
this.eV = eV; this.eV = eV;
this.$sel = this.eV.$el; this.$sel = this.eV.$el;
this.$selParent = this.$sel.closest(this.itemsClass); this.$selParent = this.$sel.closest(this.itemsClass);
// In case the component selected is not movable // In case the component selected is not draggable
if( !eV.model.get('movable') ) if( !eV.model.get('draggable') )
return; return;
// Create placeholder if not exists // Create placeholder if not exists
if(!this.$plh){ if(!this.$plh){
var pfx = this.pfx; var pfx = this.pfx;
this.$plh = $('<div>', {id: pfx + 'placeholder'}).css({'pointer-events':'none'}).hide(); this.$plh = $('<div>', {id: pfx + 'placeholder'}).css({'pointer-events':'none'}).hide();
this.$plh.append( $('<div>', {id: pfx + "plh-int", class: pfx + 'insert'} ) ); this.$plh.append( $('<div>', {id: pfx + "plh-int", class: pfx + 'insert'} ) );
if(!this.$el.length) if(!this.$el.length)
this.$el = $('.'+this.pfx+this.config.containerId); this.$el = $('.'+this.pfx+this.config.containerId);
this.$plh.appendTo(this.$el); this.$plh.appendTo(this.$el);
} }
this.$plh.data('hide',1); this.$plh.data('hide',1);
@ -48,11 +48,11 @@ define(['backbone'],
$(document).on('mouseup',this.endMove); $(document).on('mouseup',this.endMove);
$(document).on('keypress',this.rollback); $(document).on('keypress',this.rollback);
}, },
/** /**
* Get children dimensions * Get children dimensions
* @param {Object} Parent element * @param {Object} Parent element
* *
* @retun {Array} * @retun {Array}
* */ * */
getChildrenDim: function(el){ getChildrenDim: function(el){
@ -68,16 +68,16 @@ define(['backbone'],
}); });
return dim; return dim;
}, },
/** /**
* During move * During move
* @param {Object} Event * @param {Object} Event
* *
* @return void * @return void
* */ * */
onMove: function(e){ onMove: function(e){
this.moved = true; this.moved = true;
if(this.$plh.data('hide')){ if(this.$plh.data('hide')){
this.$plh.show(); this.$plh.show();
this.$plh.data('hide',0); this.$plh.data('hide',0);
@ -90,17 +90,17 @@ define(['backbone'],
this.inspect(e); this.inspect(e);
this.updatePosition(this.rX, this.rY); this.updatePosition(this.rX, this.rY);
var actualPos = this.posIndex+':'+this.posMethod; var actualPos = this.posIndex+':'+this.posMethod;
//If there is a significant changes with the pointer //If there is a significant changes with the pointer
if(!this.lastPos || (this.lastPos != actualPos)){ if(!this.lastPos || (this.lastPos != actualPos)){
this.updatePlaceholderPos(this.posIndex, this.posMethod); this.updatePlaceholderPos(this.posIndex, this.posMethod);
this.lastPos = this.posIndex+':'+this.posMethod; this.lastPos = this.posIndex+':'+this.posMethod;
} }
//Working alternative for find taget element //Working alternative for find taget element
//var $targetEl = this.$selParent.children('.'+this.pfx+this.config.itemClass).eq(this.aIndex); //var $targetEl = this.$selParent.children('.'+this.pfx+this.config.itemClass).eq(this.aIndex);
}, },
/** /**
* Search where to put placeholder * Search where to put placeholder
* @param int X position of the mouse * @param int X position of the mouse
* @param int Y position of the mouse * @param int Y position of the mouse
@ -108,7 +108,7 @@ define(['backbone'],
* */ * */
updatePosition: function( posX, posY ){ updatePosition: function( posX, posY ){
this.posMethod = "before"; this.posMethod = "before";
this.posIndex = 0; this.posIndex = 0;
var leftLimit = 0, xLimit = 0, dimRight = 0, yLimit = 0, xCenter = 0, yCenter = 0, dimDown = 0, dim = 0; var leftLimit = 0, xLimit = 0, dimRight = 0, yLimit = 0, xCenter = 0, yCenter = 0, dimDown = 0, dim = 0;
for(var i = 0; i < this.cDim.length; i++){ //Dim => t,l,h,w for(var i = 0; i < this.cDim.length; i++){ //Dim => t,l,h,w
dim = this.cDim[i]; dim = this.cDim[i];
@ -120,7 +120,7 @@ define(['backbone'],
(leftLimit && dimRight < leftLimit)) //No need with this one if over the limit (leftLimit && dimRight < leftLimit)) //No need with this one if over the limit
continue; continue;
if(!dim[4]){ //If it's not inFlow (like float element) if(!dim[4]){ //If it's not inFlow (like float element)
if( posY < dimDown) if( posY < dimDown)
yLimit = dimDown; yLimit = dimDown;
if( posX < xCenter){ //If mouse lefter than center if( posX < xCenter){ //If mouse lefter than center
xLimit = xCenter; xLimit = xCenter;
@ -142,8 +142,8 @@ define(['backbone'],
} }
} }
}, },
/** /**
* Updates the position of the placeholder * Updates the position of the placeholder
* @param int Index of the nearest child * @param int Index of the nearest child
* @param str Before or after position * @param str Before or after position
@ -157,7 +157,7 @@ define(['backbone'],
if( this.cDim[index] ){ if( this.cDim[index] ){
var elDim = this.cDim[index]; var elDim = this.cDim[index];
//If it's like with 'float' style //If it's like with 'float' style
if(!elDim[4]){ if(!elDim[4]){
w = 'auto'; w = 'auto';
h = elDim[2] - (marg * 2) + un; h = elDim[2] - (marg * 2) + un;
t = elDim[0] + marg; t = elDim[0] + marg;
@ -185,11 +185,11 @@ define(['backbone'],
if(h) if(h)
plh.style.height = h; plh.style.height = h;
}, },
/** /**
* Leave item * Leave item
* @param event * @param event
* *
* @return void * @return void
* */ * */
endMove: function(e){ endMove: function(e){
@ -202,14 +202,14 @@ define(['backbone'],
this.move(this.$targetEl, this.$sel, this.posIndex, this.posMethod); this.move(this.$targetEl, this.$sel, this.posIndex, this.posMethod);
this.itemLeft(); this.itemLeft();
}, },
/** /**
* Move component to new position * Move component to new position
* @param {Object} Component to move * @param {Object} Component to move
* @param {Object} Target component * @param {Object} Target component
* @param {Integer} Indicates the position inside the collection * @param {Integer} Indicates the position inside the collection
* @param {String} Before of after component * @param {String} Before of after component
* *
* @return void * @return void
* */ * */
move: function(target, el, posIndex, method){ move: function(target, el, posIndex, method){
@ -222,10 +222,10 @@ define(['backbone'],
var collection = model.collection; var collection = model.collection;
var targetModel = trg.data('model'); var targetModel = trg.data('model');
var targetCollection = targetModel.collection; var targetCollection = targetModel.collection;
if(!this.cDim.length) if(!this.cDim.length)
targetCollection = targetModel.get('components'); targetCollection = targetModel.get('components');
if(targetCollection && targetModel.get('droppable')){ if(targetCollection && targetModel.get('droppable')){
index = method == 'after' ? index + 1 : index; index = method == 'after' ? index + 1 : index;
var modelTemp = targetCollection.add({style:{}}, { at: index}); var modelTemp = targetCollection.add({style:{}}, { at: index});
@ -235,11 +235,11 @@ define(['backbone'],
}else }else
console.warn("Invalid target position"); console.warn("Invalid target position");
}, },
/** /**
* Track inside which element pointer entered * Track inside which element pointer entered
* @param event * @param event
* *
* @return void * @return void
* */ * */
inspect: function(e){ inspect: function(e){
@ -269,11 +269,11 @@ define(['backbone'],
this.cDim = []; this.cDim = [];
} }
}, },
/** /**
* Triggered when pointer leaves item * Triggered when pointer leaves item
* @param event * @param event
* *
* @return void * @return void
* */ * */
itemLeft: function(e){ itemLeft: function(e){
@ -282,11 +282,11 @@ define(['backbone'],
this.$targetEl = null; this.$targetEl = null;
} }
}, },
/** /**
* Returns dimension of the target * Returns dimension of the target
* @param Event * @param Event
* *
* @return Array * @return Array
* */ * */
getTargetDim: function(e){ getTargetDim: function(e){
@ -294,25 +294,25 @@ define(['backbone'],
$elO = $el.offset(); $elO = $el.offset();
return [ $elO.top - this.elT, $elO.left - this.elL, $el.outerHeight(), $el.outerWidth() ]; return [ $elO.top - this.elT, $elO.left - this.elL, $el.outerHeight(), $el.outerWidth() ];
}, },
/** /**
* Check if pointer is near to the borders of the target * Check if pointer is near to the borders of the target
* @param event * @param event
* @return Bool * @return Bool
* */ * */
nearToBorders: function(e){ nearToBorders: function(e){
var m = 10; //Limit in pixels for be near var m = 10; //Limit in pixels for be near
if(!this.dimT) if(!this.dimT)
return; return;
var dimT = this.dimT; var dimT = this.dimT;
if( ((dimT[0] + m) > this.rY) || (this.rY > (dimT[0] + dimT[2] - m)) || if( ((dimT[0] + m) > this.rY) || (this.rY > (dimT[0] + dimT[2] - m)) ||
((dimT[1] + m) > this.rX) || (this.rX > (dimT[1] + dimT[3] - m)) ) ((dimT[1] + m) > this.rX) || (this.rX > (dimT[1] + dimT[3] - m)) )
return 1; return 1;
else else
return 0; return 0;
}, },
/** /**
* Rollback to previous situation * Rollback to previous situation
* @param Event * @param Event
* @param Bool Indicates if rollback in anycase * @param Bool Indicates if rollback in anycase

2
src/navigator/view/ItemView.js

@ -196,7 +196,7 @@ define(['backbone', 'text!./../template/item.html','require'],
}).render().$el; }).render().$el;
this.$el.find('.'+ pfx +'children').html(this.$components); this.$el.find('.'+ pfx +'children').html(this.$components);
this.$caret = this.$el.find('> .' + pfx + 'title > #' + pfx + 'caret'); this.$caret = this.$el.find('> .' + pfx + 'title > #' + pfx + 'caret');
if(!this.model.get('movable') || !this.config.sortable){ if(!this.model.get('draggable') || !this.config.sortable){
this.$el.find('> #' + pfx + 'move').detach(); this.$el.find('> #' + pfx + 'move').detach();
} }
if(!vis) if(!vis)

Loading…
Cancel
Save