Guest User

MyFaces Dev [GSOC] Prototpyes: DnD

a guest
May 28th, 2010
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 11.33 KB | None | 0 0
  1. ===============================
  2. ==========<fx:dragSource>======
  3. ===============================
  4.   REFS:
  5.     [0] http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dnd
  6.     [1] http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data
  7.     [2] https://developer.mozilla.org/En/DragDrop/Drag_and_Drop
  8.     [3] https://developer.mozilla.org/En/DragDrop/DataTransfer
  9.     [4] https://developer.mozilla.org/En/DragDrop/Recommended_Drag_Types
  10.     [5] https://developer.mozilla.org/En/DragDrop/Drag_Operations
  11.  
  12.   EXTENDS:
  13.     ClientBehavior
  14.  
  15.   ATTRIBUTES:
  16.     action (required): Allowed DnD drag action from this source. Can be comma separated set or String[] or Collection<String> of copy, copyLink, copyMove, link, linkMove, move, all.
  17.     dropZoneTypes (optional): With this property, we can specify which types of drop zones we can make DnD from this source. Can be comma separated set or String[] or Collection<String>
  18.     param(optional): Parameter to be sent to server when drop operation is done.
  19.   NOTES:
  20.     May use "custom non-visible data" [1], "data-*" when at least one browser starts supporting it  
  21.  
  22. ===============================
  23. ==========<fx:dropZone>========
  24. ===============================
  25.   REFS:
  26.     [0] http://www.whatwg.org/specs/web-apps/current-work/multipage/dnd.html#dnd
  27.     [1] http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data
  28.     [2] http://html5demos.com/drag-anything
  29.     [3] http://apirocks.com/html5/html5.html#slide13
  30.     [4] https://developer.mozilla.org/En/DragDrop/Drag_and_Drop
  31.     [5] https://developer.mozilla.org/En/DragDrop/DataTransfer
  32.     [6] https://developer.mozilla.org/En/DragDrop/Recommended_Drag_Types
  33.     [7] https://developer.mozilla.org/En/DragDrop/Drag_Operations
  34.  
  35.   EXTENDS:
  36.     AjaxBehavior
  37.  
  38.   ATTRIBUTES:
  39.     actions (required): Allowed DnD actions. Can be comma separated set or String[] or Collection<String> of copy, copyLink, copyMove, link, linkMove, move, all.
  40.       If action of dragEvent are not one of these, DnD will stop.
  41.     dropListener (required): Listener method to handle DnD operation on server side.
  42.     rerender (optional): same with <f:ajax> rerender attribute.
  43.     types (optional): Used in conjunction with <fx:dragSource>'s dropZoneTypes attribute. Types of this dropZone.
  44.       Can be comma separated set or String[] or Collection<String>.
  45.     acceptMimeTypes: If this is set, only content dropped into this zone with defined mime type will be accepted and sent to server-side drop listener.
  46.        HTML5 DnD allows us to drop anything into drop zone[2]: files from desktop, images on some other document[3], etc. So this property is a filter.
  47.       If value is "*" any content dropped into this zone will be accepted. All type info and data of dropped stuff will be sent to dropListener.
  48.       For example, if this is true and we dragNdrop some image on any Html page(even not generated by JSF), dropListener will be triggered with the following data:
  49.         content-type   /  value
  50.         ------------    --------------------------------
  51.         text/uri-list  /  http://example.org/someImage.png
  52.         Text      /  http://example.org/someImage.png
  53.         text/plain    /  http://example.org/someImage.png
  54.         URL        /  http://example.org/someImage.png
  55.      
  56.       Another example may be dropping files from your desktop, but what to do with them? We'll have <hx:inputFileUpload> for DnD file uploading.
  57.       So better not to accept files, or even they're accepted, we'd better not to send their information to server.
  58.       Mime type can be something like "text/x-myfaces-html5-dnd-source" for MyFaces generated components.
  59.       Default to "text/x-myfaces-html5-dnd-source", thus only MyFaces generated components can be dropped into this zone.
  60.   NOTES:
  61.  
  62. ===============================
  63. ==========<hx:panel>========
  64. ===============================
  65.   REFS:
  66.  
  67.   EXTENDS:
  68.     <t:div> or something (I don't think a dependency to Tomahawk is good. So, need to rewrite that stuff or copy them, preferably with maven-dependency-plugin)
  69.  
  70.   ATTRIBUTES:
  71.     ondragstart, ondragend, ondragenter, ondragover, ondrop, other DnD behavioral attributes
  72.     dragStartStyle, dragStartStyleClass: CSS stuff to set when this component is being dragged
  73.     dragOverStyle, dragOverStyleClass: CSS stuff to set when something is dragged onto this component
  74.    
  75.     other stuff that wrapped components don't support (not clear yet!)
  76.  
  77.   NOTES:
  78.     This is just like <t:div> or <rich:panel>. For example DnD does not work with components that dont support "dragstart" clientbehavior event.
  79.     That is why <hx:panel> is necessary. We can wrap those components (ie. h:inputText, h:panelGrid) with <hx:panel>.
  80.    
  81.   <!-- - - - - - - - - -usage: <fx:dragSource> with dropZoneType - - - - - - - - - -  - - - - - - - - - - - - -->
  82.   <hx:panel id="draggable1" >
  83.     <!-- SOME CONTENT -->
  84.     <fx:dragSource action="copy" dropZoneTypes="#{someBean.optionalDropZoneTypes}"
  85.      param="#{someBean.dndParamToSendToServer}"/>
  86.   </hx:panel>
  87.  
  88.   <!-- expected HTML5 code-->
  89.   <div id="draggable1" draggable="true"
  90.      ondragstart="return dragStart(event, 'copy', {'firstDropZoneType','secondDropZoneType'}, 'literalDndParamToSendToServer')"
  91.      >
  92.     <!-- RENDERED CONTENT -->
  93.   </div>
  94.  
  95.  
  96.   <!-- - - - - - - - - -usage: <fx:dragSource> with event handler chains - - - - - - - - - -  - - - - - - - - - - - - -->
  97.   <hx:panel id="draggable2"
  98.      ondragstart="log('dragging');"
  99.      ondragend="alert('Drag ended. May not have been dropped succesfully though!')">
  100.     <!-- SOME CONTENT -->
  101.     <fx:dragSource action="move" param="anotherDndParamToSendToServer" />
  102.   </hx:panel>
  103.  
  104.   <!-- expected HTML5 code-->
  105.   <div id="draggable2" draggable="true"
  106.      ondragstart="log('dragging'); return dragStart(event, 'move', null, 'secondDndValueToSendServer')"
  107.      ondragend="alert('Drag ended. May not have been dropped succesfully though!')"
  108.      >
  109.     <!-- RENDERED CONTENT -->
  110.   </div>
  111.  
  112.   <!-- - - - - - - - - -usage: <fx:dragSource> within <hx:inputText> (allowed with HTML5, doesn't make sense though) - - - - - - - - - -  - - - - - - - - - - - - -->
  113.   <hx:inputText id="draggableInputText">
  114.     <fx:dragSource action="move"/>
  115.   </hx:panel>
  116.  
  117.   <input type="text" id="draggableInputText" draggable="true"
  118.      ondragstart="return dragStart(event, 'move')" />
  119.  
  120.  
  121.  
  122.   <!-- - - - - - - - - -usage: <fx:dropZone> with type and rerender - - - - - - - - - -  - - - - - - - - - - - - -->
  123.   <hx:panel id="dropZone1">
  124.     <!-- SOME CONTENT -->
  125.     <fx:dropZone actions="move" dropListener="#{someBean.dropListener}"
  126.      rerender="draggable1" types="firstDropZoneType"/>
  127.   </hx:panel>
  128.  
  129.   <div id="dropZone1"
  130.    ondragenter="dragEnter(event,{'move'},{'firstDropZoneType'}), {'text/x-myfaces-html5-dnd-source'}"
  131.    ondragover="dragOver(event)"
  132.    ondrop="drop(event, 'stuff necessary to trigger someBean.dropListener and rerender draggable1 using jsf.ajax.request')">
  133.     <!-- RENDERED CONTENT -->
  134.   </div>
  135.  
  136.  
  137.  
  138.   <!-- - - - - - - - - -usage: <fx:dropZone> with handler chaining - - - - - - - - - -  - - - - - - - - - - - - -->
  139.   <hx:panel id="dropZone2"
  140.    ondragover="makeRedBorder();"
  141.    ondragleave="clearRedBorder();"
  142.    ondrop="makeGreenBorder();"
  143.    >
  144.     <!-- SOME CONTENT -->
  145.     <fx:dropZone actions="copy,move" dropListener="#{someBean.dropListener}" />
  146.   </hx:panel>
  147.  
  148.   <div id="dropZone2"
  149.    ondragenter="dragEnter(event,{'copy','move'}), null, {'text/x-myfaces-html5-dnd-source'}"
  150.    ondragover="makeRedBorder(); dragOver(event)"
  151.    ondragleave="clearRedBorder();"
  152.    ondrop="makeGreenBorder();drop(event)">
  153.     <!-- RENDERED CONTENT -->
  154.   </div>
  155.  
  156.  
  157.  
  158.   <!-- - - - - - - - - -usage: <fx:dropZone> that accepts some specified mime typed content - - - - - - - - - -  - - - - - - - - - - - - -->
  159.   <hx:panel id="dropZone3">
  160.     <!-- SOME CONTENT -->
  161.     <fx:dropZone actions="copy" dropListener="#{someBean.dropListener}" acceptMimeTypes="text/plain, URL" />
  162.   </hx:panel>
  163.  
  164.   <div id="dropZone3"
  165.    ondragenter="dragEnter(event,{'copy','move'}, null, {'text/plain', 'URL'})"
  166.    ondragover="dragOver(event)"
  167.    ondrop="drop(event)">
  168.     <!-- RENDERED CONTENT -->
  169.   </div>
  170.  
  171. <script><!--
  172.  
  173. var paramMimeType = 'text/x-myfaces-html5-dnd-param';
  174. var myFacesComponentSourceMimeType = 'text/x-myfaces-html5-dnd-source';
  175. var dropZoneMimeType = 'text/x-myfaces-html5-drop-zone-type';
  176. var myFacesComponentSourceConstant = 'org.apache.myfaces';
  177.  
  178. /*
  179. * This function is only used by MyFaces generated draggable elements, the ones that have <fx:dragSource> behavior.
  180. */
  181. function dragStart(event, effectAllowed, paramToSendToServer, dropZoneTypes) {
  182.  
  183.  //set allowed effect
  184.  event.dataTransfer.effectAllowed = effectAllowed;  //only allow some specific event
  185.  
  186.  //with this, drop zone will understand that source of this dnd operation is some MyFaces component
  187.  event.dataTransfer.setData(myFacesComponentSourceMimeType, myFacesComponentSourceConstant);
  188.  
  189.  //this will be set if we want to send an optional parameter to the server-side drop listener
  190.  if(paramToSendToServer)
  191.    event.dataTransfer.setData(paramMimeType, valueToSendToServer);    
  192.  
  193.  //this will be set if we want to make the drop only into specific dropzones with specific types
  194.  if(dropZoneType)
  195.    event.dataTransfer.setData(dropZoneMimeType, dropZoneTypes);  //in fact, dropZoneTypes is an array. need to iterate  
  196.      
  197.  return true;
  198. }
  199.  
  200. function dragEnter(event, allowedEffects, dropZoneTypes, acceptedMimeTypes) {
  201.  //check allowed mime types first
  202.  var found = false;
  203.  var foundTypes = acceptedMimeTypes.filter(function (mimeType) event.dataTransfer.types.contains(mimeType));
  204.  
  205.  //if even one of the event mime types are not allowed, stop DnD  
  206.  if(foundTypes.length == 0)
  207.    return true; //don't cancel the event, thus stop DnD
  208.  
  209.  //check allowed effect
  210.  //changeIt: allowedEffects is an array in fact, so need to iterate
  211.  if (event.dataTransfer.effectAllowed != allowedEffect)    
  212.    return true; //don't cancel the event, thus stop DnD
  213.  
  214.  //check drop zone type
  215.  //changeIt: in fact these are arrays too. need to iterate or use contains() function
  216.  if (event.dataTransfer.getData(dropZoneMimeType) != dropZoneTypes)
  217.    return true; //don't cancel the event, thus stop DnD
  218.  
  219.  //cancel the event. this is necessary for DnD execution  
  220.  if (event.preventDefault)  
  221.    event.preventDefault();
  222.  
  223.  return false;
  224. }
  225.  
  226. function dragOver(event){
  227.  //don't check source, allowedEffect and dropZoneType, since they are checked at dragEnter function
  228.  
  229.  //show same user hint effect specified by drag source
  230.  event.dataTransfer.dropEffect = event.dataTransfer.effectAllowed;  
  231.  
  232.  //cancel the event, so effect on screen is updated
  233.  if (event.preventDefault)
  234.    event.preventDefault();
  235.  
  236.  return false;
  237. }
  238.  
  239. function drop(event, jsf_ajax_request_necessary_stuff){
  240.  //cancel the event. this is necessary for DnD execution  
  241.  if (event.preventDefault)
  242.    event.preventDefault();
  243.  
  244.  //CALL jsf.ajax.request with constructing parameters from jsf_ajax_request_necessary_stuff
  245.  //this call should trigger the dropListener and make the rerender operation
  246.  //need to send event mime types and values too!
  247.  
  248.  return false;
  249. }
  250.  
  251. --></script>
Advertisement
Add Comment
Please, Sign In to add comment