Browse Source

Finalizado o drag da criação de comandos

Igor Félix 6 years ago
parent
commit
dc0fe1d17d
5 changed files with 435 additions and 65 deletions
  1. 15 0
      css/ivprog-visual-1.0.css
  2. 1 1
      js/ivprog-visual-1.0.js
  3. 85 53
      js/ivprog-visual-functions-1.0.js
  4. 331 0
      js/jquery.simulate.js
  5. 3 11
      main.html

+ 15 - 0
css/ivprog-visual-1.0.css

@@ -265,4 +265,19 @@ div.buttons_manage_columns {
 	z-index: 99999999;
 	opacity: .8;
 	cursor: move;
+	border: 1px solid green;
+}
+
+#outer {
+    background: blue;
+    width: 200px;
+    height: 200px;
+}
+.inner {
+    background: yellow;
+    width: 50px;
+    height: 50px;
+}
+.commands_list_div, .variables_list_div {
+	min-height: 20px;
 }

+ 1 - 1
js/ivprog-visual-1.0.js

@@ -73,4 +73,4 @@ var funcaoSomar = new Funcao("somar", tiposDados.integer, 0, [], false, false, n
 funcaoSomar.lista_parametros.push(new Variavel(tiposDados.integer, "a"));
 funcaoSomar.lista_parametros.push(new Variavel(tiposDados.integer, "b"));
 
-adicionarFuncao(funcaoSomar);
+//adicionarFuncao(funcaoSomar);

+ 85 - 53
js/ivprog-visual-functions-1.0.js

@@ -204,67 +204,88 @@ function renderGlobals() {
 var has_element_created_draged = false;
 var which_element_is_draged = null;
 
-function createCommentDragObject() {
-	var ret = '';
-	ret += '<div class="ui comment created_element"> <i class="ui icon small quote left"></i> <span class="span_comment_text" "> Testando </span>';
-	ret += '</div>';
+function ended(event) {
+	var el = document.elementFromPoint(event.clientX, event.clientY);
+	//console.log("elemento que ele soltou:");
+	//console.log(el);
+	
+	$(el).remove();
+
+	var el = document.elementFromPoint(event.clientX, event.clientY);
+	//console.log("depois de remover:");
+	//console.log(el);
+
+
+	// Só irá adicionar se soltar o elemento no espaço para a função correta:
+	if ($(el).data('fun') == function_to_add) {
+		// Se a lista de comandos estiver vazia, então é o primeiro.
+		// Portanto, ele deve soltar o elemento obrigatoriamente no objeto vazio
+		if ((programa.funcoes[function_to_add].comandos == null)  || (programa.funcoes[function_to_add].comandos.length == 0)) {
+			
+				// pode adicionar 
+				programa.funcoes[function_to_add].comandos = [];
+				programa.funcoes[function_to_add].comandos.push(new Comentario(i18n('text_comment')));
+			
+		} else {
+			programa.funcoes[function_to_add].comandos.push(new Comentario(i18n('text_comment')));
+		}
+
+	} else { // Se entrar nesse bloco 'else', quer dizer que o usuário não soltou o elemento necessariamente na div específica
+			 // portanto, devemos procurar nos elementos DOM superiores, se existe algum com o data-fun
+
+		var hier = $(el).parentsUntil(".all_functions");
+		for (i = 0; i < hier.length; i++) {
+			if ($(hier[i]).data('fun') == function_to_add) {
+				programa.funcoes[function_to_add].comandos.push(new Comentario(i18n('text_comment')));
+				break;
+			}
+		}
+
 
-	has_element_created_draged = true;
-	which_element_is_draged = tiposComandos.comment;
+	}
+
+	renderAlgorithm();
 
-	$('body').append(ret);
 }
 
-var myDraggable = null;
-// Yeah... we're going to hack the widget
-var widget = null;
-var clickEvent = null;
-
-function flutuateCreatedElement() {
-	$('.created_element').css('top', mouseY);
-    $('.created_element').css('left', mouseX);
-
-
-      myDraggable = $('.created_element').draggable();
-  
-	  // Yeah... we're going to hack the widget
-	  widget = myDraggable.data('ui-draggable');
-	  clickEvent = null;
-	  
-	  myDraggable.click(function(event){
-
-
-	      if(!clickEvent){
-	        widget._mouseStart(event);
-	        clickEvent = event;
-	      }
-	      else {
-	        widget._mouseUp(event);
-	        clickEvent = null;
-	      }
-	    });
-	    
-	  $(document).mousemove(function(event){
-	    if(clickEvent){
-	      // We need to set this to our own clickEvent, otherwise
-	      // it won't position correctly.
-	      widget._mouseDownEvent = clickEvent;
-	      widget._mouseMove(event);
-	    }
-	  });
 
+function createCommentDragObject() {
+	var ret = '';
+	ret += '<div class="ui comment created_element" onclick="ended(event)" id="sera"> <i class="ui icon small quote left"></i> <span class="span_comment_text" "> '+i18n('text_comment')+' </span>';
+	ret += '</div>';
+
+	return ret;
 }
 
+var function_to_add = -1;
+
 function addHandlers() {
 
+	$('.create_comment').on('click', function(e){
+
+		has_element_created_draged = true;
+		which_element_is_draged = tiposComandos.command;
+
+		function_to_add = $(e.target).data('fun');
+
+		var inner = $(createCommentDragObject()).draggable().appendTo("body");
+	    
+	    inner.css("position", "absolute");
+	    
+	    e.type = "mousedown.draggable";
+	    e.target = inner[0];
+	    inner.css("left", e.pageX - 15);
+	    inner.css("top", e.pageY - 15);
+	    inner.trigger(e);
+		
+	});
+
 	$('.ui.buttons .dropdown').dropdown({
 		    onChange: function(value, text, $selectedItem) {
 		    	if (value == tiposComandos.comment) {
-		    		createCommentDragObject();
+		    		
 		    	}
 
-		    	flutuateCreatedElement();
-
 		    }
 		});
 
@@ -1806,7 +1827,7 @@ function manageDragableCommands(sequence) {
 
 
 function appendFunction(function_obj, sequence) {
-	var appender = '<div class="ui secondary segment function_div list-group-item">';
+	var appender = '<div class="ui secondary segment function_div list-group-item" data-fun="'+sequence+'">';
 
 	if (function_obj.comentario_funcao) {
 		appender += renderComment(function_obj.comentario_funcao, sequence, true, -1);
@@ -1820,9 +1841,9 @@ function appendFunction(function_obj, sequence) {
 	appender += '<div class="ui icon buttons add_var_top_button"><div class="ui icon button" onclick="addVariable('+sequence+')"><i class="icon superscript"></i></div>';
 	
 	appender += '<div class="ui icon button dropdown" ><i class="icon code"></i> <div class="menu" id="menu_commands_'+sequence+'"> ';
-	appender += '<a class="item" data-text="'+tiposComandos.reader+'"><i class="download icon"></i> ' +i18n('text_read_var')+ '</a>'
-			  + '<a class="item" data-text="'+tiposComandos.writer+'"><i class="upload icon"></i> '+i18n('text_write_var')+'</a>'
-			  + '<a class="item create_comment" data-text="'+tiposComandos.comment+'"><i class="quote left icon"></i> '+i18n('text_comment')+'</a>'
+	appender += '<a class="item" data-text="'+tiposComandos.reader+'" data-fun="'+sequence+'"><i class="download icon"></i> ' +i18n('text_read_var')+ '</a>'
+			  + '<a class="item" data-text="'+tiposComandos.writer+'" data-fun="'+sequence+'"><i class="upload icon"></i> '+i18n('text_write_var')+'</a>'
+			  + '<a class="item create_comment" data-text="'+tiposComandos.comment+'" data-fun="'+sequence+'"><i class="quote left icon"></i> '+i18n('text_comment')+'</a>'
 				+ '</div></div></div>';
 
 
@@ -1848,9 +1869,20 @@ function appendFunction(function_obj, sequence) {
 		+ '<div class="ui top attached segment variables_list_div">'
 		+ renderVariables(function_obj, sequence)
 		+ '</div>'
-		+ '<div class="ui bottom attached segment commands_list_div"></div>'		
+		+ '<div class="ui bottom attached segment commands_list_div" data-fun="'+sequence+'">';
+
+
+	if (programa.funcoes[sequence].comandos) {
+		for (l = 0; l < programa.funcoes[sequence].comandos.length; l++) {
+			if (programa.funcoes[sequence].comandos[l].tipo == tiposComandos.comment) {
+				appender += renderComment(programa.funcoes[sequence].comandos[l], sequence, false, l);
+			}
+		}
+	}
+
+	appender += '</div>';
 
-		+ '<div class="function_close_div">}</div>'
+	appender += '<div class="function_close_div">}</div>'
 		+ '</div>'
 		+ '</div>';
 

+ 331 - 0
js/jquery.simulate.js

@@ -0,0 +1,331 @@
+ /*!
+ * jQuery Simulate v@VERSION - simulate browser mouse and keyboard events
+ * https://github.com/jquery/jquery-simulate
+ *
+ * Copyright jQuery Foundation and other contributors
+ * Released under the MIT license.
+ * http://jquery.org/license
+ *
+ * Date: @DATE
+ */
+
+;(function( $, undefined ) {
+
+var rkeyEvent = /^key/,
+	rmouseEvent = /^(?:mouse|contextmenu)|click/;
+
+$.fn.simulate = function( type, options ) {
+	return this.each(function() {
+		new $.simulate( this, type, options );
+	});
+};
+
+$.simulate = function( elem, type, options ) {
+	var method = $.camelCase( "simulate-" + type );
+
+	this.target = elem;
+	this.options = options;
+
+	if ( this[ method ] ) {
+		this[ method ]();
+	} else {
+		this.simulateEvent( elem, type, options );
+	}
+};
+
+$.extend( $.simulate, {
+
+	keyCode: {
+		BACKSPACE: 8,
+		COMMA: 188,
+		DELETE: 46,
+		DOWN: 40,
+		END: 35,
+		ENTER: 13,
+		ESCAPE: 27,
+		HOME: 36,
+		LEFT: 37,
+		NUMPAD_ADD: 107,
+		NUMPAD_DECIMAL: 110,
+		NUMPAD_DIVIDE: 111,
+		NUMPAD_ENTER: 108,
+		NUMPAD_MULTIPLY: 106,
+		NUMPAD_SUBTRACT: 109,
+		PAGE_DOWN: 34,
+		PAGE_UP: 33,
+		PERIOD: 190,
+		RIGHT: 39,
+		SPACE: 32,
+		TAB: 9,
+		UP: 38
+	},
+
+	buttonCode: {
+		LEFT: 0,
+		MIDDLE: 1,
+		RIGHT: 2
+	}
+});
+
+$.extend( $.simulate.prototype, {
+
+	simulateEvent: function( elem, type, options ) {
+		var event = this.createEvent( type, options );
+		this.dispatchEvent( elem, type, event, options );
+	},
+
+	createEvent: function( type, options ) {
+		if ( rkeyEvent.test( type ) ) {
+			return this.keyEvent( type, options );
+		}
+
+		if ( rmouseEvent.test( type ) ) {
+			return this.mouseEvent( type, options );
+		}
+	},
+
+	mouseEvent: function( type, options ) {
+		var event, eventDoc, doc, body;
+		options = $.extend({
+			bubbles: true,
+			cancelable: (type !== "mousemove"),
+			view: window,
+			detail: 0,
+			screenX: 0,
+			screenY: 0,
+			clientX: 1,
+			clientY: 1,
+			ctrlKey: false,
+			altKey: false,
+			shiftKey: false,
+			metaKey: false,
+			button: 0,
+			relatedTarget: undefined
+		}, options );
+
+		if ( document.createEvent ) {
+			event = document.createEvent( "MouseEvents" );
+			event.initMouseEvent( type, options.bubbles, options.cancelable,
+				options.view, options.detail,
+				options.screenX, options.screenY, options.clientX, options.clientY,
+				options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
+				options.button, options.relatedTarget || document.body.parentNode );
+
+			// IE 9+ creates events with pageX and pageY set to 0.
+			// Trying to modify the properties throws an error,
+			// so we define getters to return the correct values.
+			if ( event.pageX === 0 && event.pageY === 0 && Object.defineProperty ) {
+				eventDoc = event.relatedTarget.ownerDocument || document;
+				doc = eventDoc.documentElement;
+				body = eventDoc.body;
+
+				Object.defineProperty( event, "pageX", {
+					get: function() {
+						return options.clientX +
+							( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) -
+							( doc && doc.clientLeft || body && body.clientLeft || 0 );
+					}
+				});
+				Object.defineProperty( event, "pageY", {
+					get: function() {
+						return options.clientY +
+							( doc && doc.scrollTop || body && body.scrollTop || 0 ) -
+							( doc && doc.clientTop || body && body.clientTop || 0 );
+					}
+				});
+			}
+		} else if ( document.createEventObject ) {
+			event = document.createEventObject();
+			$.extend( event, options );
+			// standards event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ff974877(v=vs.85).aspx
+			// old IE event.button uses constants defined here: http://msdn.microsoft.com/en-us/library/ie/ms533544(v=vs.85).aspx
+			// so we actually need to map the standard back to oldIE
+			event.button = {
+				0: 1,
+				1: 4,
+				2: 2
+			}[ event.button ] || ( event.button === -1 ? 0 : event.button );
+		}
+
+		return event;
+	},
+
+	keyEvent: function( type, options ) {
+		var event;
+		options = $.extend({
+			bubbles: true,
+			cancelable: true,
+			view: window,
+			ctrlKey: false,
+			altKey: false,
+			shiftKey: false,
+			metaKey: false,
+			keyCode: 0,
+			charCode: undefined
+		}, options );
+
+		if ( document.createEvent ) {
+			try {
+				event = document.createEvent( "KeyEvents" );
+				event.initKeyEvent( type, options.bubbles, options.cancelable, options.view,
+					options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
+					options.keyCode, options.charCode );
+			// initKeyEvent throws an exception in WebKit
+			// see: http://stackoverflow.com/questions/6406784/initkeyevent-keypress-only-works-in-firefox-need-a-cross-browser-solution
+			// and also https://bugs.webkit.org/show_bug.cgi?id=13368
+			// fall back to a generic event until we decide to implement initKeyboardEvent
+			} catch( err ) {
+				event = document.createEvent( "Events" );
+				event.initEvent( type, options.bubbles, options.cancelable );
+				$.extend( event, {
+					view: options.view,
+					ctrlKey: options.ctrlKey,
+					altKey: options.altKey,
+					shiftKey: options.shiftKey,
+					metaKey: options.metaKey,
+					keyCode: options.keyCode,
+					charCode: options.charCode
+				});
+			}
+		} else if ( document.createEventObject ) {
+			event = document.createEventObject();
+			$.extend( event, options );
+		}
+
+		if ( !!/msie [\w.]+/.exec( navigator.userAgent.toLowerCase() ) || (({}).toString.call( window.opera ) === "[object Opera]") ) {
+			event.keyCode = (options.charCode > 0) ? options.charCode : options.keyCode;
+			event.charCode = undefined;
+		}
+
+		return event;
+	},
+
+	dispatchEvent: function( elem, type, event ) {
+		if ( elem.dispatchEvent ) {
+			elem.dispatchEvent( event );
+		} else if ( type === "click" && elem.click && elem.nodeName.toLowerCase() === "input" ) {
+			elem.click();
+		} else if ( elem.fireEvent ) {
+			elem.fireEvent( "on" + type, event );
+		}
+	},
+
+	simulateFocus: function() {
+		var focusinEvent,
+			triggered = false,
+			element = $( this.target );
+
+		function trigger() {
+			triggered = true;
+		}
+
+		element.bind( "focus", trigger );
+		element[ 0 ].focus();
+
+		if ( !triggered ) {
+			focusinEvent = $.Event( "focusin" );
+			focusinEvent.preventDefault();
+			element.trigger( focusinEvent );
+			element.triggerHandler( "focus" );
+		}
+		element.unbind( "focus", trigger );
+	},
+
+	simulateBlur: function() {
+		var focusoutEvent,
+			triggered = false,
+			element = $( this.target );
+
+		function trigger() {
+			triggered = true;
+		}
+
+		element.bind( "blur", trigger );
+		element[ 0 ].blur();
+
+		// blur events are async in IE
+		setTimeout(function() {
+			// IE won't let the blur occur if the window is inactive
+			if ( element[ 0 ].ownerDocument.activeElement === element[ 0 ] ) {
+				element[ 0 ].ownerDocument.body.focus();
+			}
+
+			// Firefox won't trigger events if the window is inactive
+			// IE doesn't trigger events if we had to manually focus the body
+			if ( !triggered ) {
+				focusoutEvent = $.Event( "focusout" );
+				focusoutEvent.preventDefault();
+				element.trigger( focusoutEvent );
+				element.triggerHandler( "blur" );
+			}
+			element.unbind( "blur", trigger );
+		}, 1 );
+	}
+});
+
+
+
+/** complex events **/
+
+function findCenter( elem ) {
+	var offset,
+		document = $( elem.ownerDocument );
+	elem = $( elem );
+	offset = elem.offset();
+
+	return {
+		x: offset.left + elem.outerWidth() / 2 - document.scrollLeft(),
+		y: offset.top + elem.outerHeight() / 2 - document.scrollTop()
+	};
+}
+
+function findCorner( elem ) {
+	var offset,
+		document = $( elem.ownerDocument );
+	elem = $( elem );
+	offset = elem.offset();
+
+	return {
+		x: offset.left - document.scrollLeft(),
+		y: offset.top - document.scrollTop()
+	};
+}
+
+$.extend( $.simulate.prototype, {
+	simulateDrag: function() {
+		var i = 0,
+			target = this.target,
+			eventDoc = target.ownerDocument,
+			options = this.options,
+			center = options.handle === "corner" ? findCorner( target ) : findCenter( target ),
+			x = Math.floor( center.x ),
+			y = Math.floor( center.y ),
+			coord = { clientX: x, clientY: y },
+			dx = options.dx || ( options.x !== undefined ? options.x - x : 0 ),
+			dy = options.dy || ( options.y !== undefined ? options.y - y : 0 ),
+			moves = options.moves || 3;
+
+		this.simulateEvent( target, "mousedown", coord );
+
+		for ( ; i < moves ; i++ ) {
+			x += dx / moves;
+			y += dy / moves;
+
+			coord = {
+				clientX: Math.round( x ),
+				clientY: Math.round( y )
+			};
+
+			this.simulateEvent( eventDoc, "mousemove", coord );
+		}
+
+		if ( $.contains( eventDoc, target ) ) {
+			this.simulateEvent( target, "mouseup", coord );
+			this.simulateEvent( target, "click", coord );
+		} else {
+			this.simulateEvent( eventDoc, "mouseup", coord );
+		}
+	}
+});
+
+})( jQuery );

+ 3 - 11
main.html

@@ -6,6 +6,9 @@
     <link rel="stylesheet" type="text/css" href="css/ivprog-visual-1.0.css">
     <script src="js/jquery-3.3.1.min.js"></script>
 
+    <script src="js/jquery.simulate.js"></script>
+    
+
     <script src="js/iassign-integration-functions.js"></script>
 
     <script src="i18n/i18n-engine.js"></script>
@@ -111,16 +114,5 @@
       
     </div>
 
-    <i class="icon trash alternate outline huge" onmouseover="console.log('oi');" ></i>
-
-
-
-
-
-
-
-
-
-
   </body>
 </html>