AC_4897, AC_4898, AC_4899: Multifile uploader fixes.
[acontent.git] / include / jscripts / infusion / components / reorderer / js / Reorderer.js
1 var fluid_1_4=fluid_1_4||{};(function($,fluid){var defaultAvatarCreator=function(item,cssClass,dropWarning){fluid.dom.cleanseScripts(fluid.unwrap(item));var avatar=$(item).clone();fluid.dom.iterateDom(avatar.get(0),function(node){node.removeAttribute("id");if(node.tagName.toLowerCase()==="input"){node.setAttribute("disabled","disabled")}});avatar.removeProp("id");avatar.removeClass("ui-droppable");avatar.addClass(cssClass);if(dropWarning){var avatarContainer=$(document.createElement("div"));avatarContainer.append(avatar);avatarContainer.append(dropWarning);avatar=avatarContainer}$("body").append(avatar);if(!$.browser.safari){avatar.css("display","block").width(item.offsetWidth).height(item.offsetHeight)}if($.browser.opera){avatar.hide()}return avatar};function bindHandlersToContainer(container,keyDownHandler,keyUpHandler,mouseMoveHandler){var actualKeyDown=keyDownHandler;var advancedPrevention=false;if($.browser.opera){container.keypress(function(evt){if(advancedPrevention){advancedPrevention=false;evt.preventDefault();return false}});actualKeyDown=function(evt){var oldret=keyDownHandler(evt);if(oldret===false){advancedPrevention=true}}}container.keydown(actualKeyDown);container.keyup(keyUpHandler)}function addRolesToContainer(that){that.container.attr("role",that.options.containerRole.container);that.container.attr("aria-multiselectable","false");that.container.attr("aria-readonly","false");that.container.attr("aria-disabled","false")}function createAvatarId(parentId){return parentId+"_avatar"}var adaptKeysets=function(options){if(options.keysets&&!(options.keysets instanceof Array)){options.keysets=[options.keysets]}};fluid.reorderer=function(container,options){if(!container){fluid.fail("Reorderer initialised with no container")}var thatReorderer=fluid.initView("fluid.reorderer",container,options);options=thatReorderer.options;var dropManager=fluid.dropManager();thatReorderer.layoutHandler=fluid.initSubcomponent(thatReorderer,"layoutHandler",[thatReorderer.container,options,dropManager,thatReorderer.dom]);thatReorderer.activeItem=undefined;adaptKeysets(options);var kbDropWarning=thatReorderer.locate("dropWarning");var mouseDropWarning;if(kbDropWarning){mouseDropWarning=kbDropWarning.clone()}var isMove=function(evt){var keysets=options.keysets;for(var i=0;i<keysets.length;i++){if(keysets[i].modifier(evt)){return true}}return false};var isActiveItemMovable=function(){return $.inArray(thatReorderer.activeItem,thatReorderer.dom.fastLocate("movables"))>=0};var setDropEffects=function(value){thatReorderer.dom.fastLocate("dropTargets").attr("aria-dropeffect",value)};var styles=options.styles;var noModifier=function(evt){return(!evt.ctrlKey&&!evt.altKey&&!evt.shiftKey&&!evt.metaKey)};var handleDirectionKeyDown=function(evt){var item=thatReorderer.activeItem;if(!item){return true}var keysets=options.keysets;for(var i=0;i<keysets.length;i++){var keyset=keysets[i];var keydir=fluid.keyForValue(keyset,evt.keyCode);if(!keydir){continue}var isMovement=keyset.modifier(evt);var dirnum=fluid.keycodeDirection[keydir];var relativeItem=thatReorderer.layoutHandler.getRelativePosition(item,dirnum,!isMovement);if(!relativeItem){continue}if(isMovement){var prevent=thatReorderer.events.onBeginMove.fire(item);if(prevent===false){return false}if(kbDropWarning.length>0){if(relativeItem.clazz==="locked"){thatReorderer.events.onShowKeyboardDropWarning.fire(item,kbDropWarning);kbDropWarning.show()}else{kbDropWarning.hide()}}if(relativeItem.element){thatReorderer.requestMovement(relativeItem,item)}}else{if(noModifier(evt)){item.blur();$(relativeItem.element).focus()}}return false}return true};thatReorderer.handleKeyDown=function(evt){if(!thatReorderer.activeItem||thatReorderer.activeItem!==evt.target){return true}var jActiveItem=$(thatReorderer.activeItem);if(!jActiveItem.hasClass(styles.dragging)&&isMove(evt)){if(isActiveItemMovable()){jActiveItem.removeClass(styles.selected);jActiveItem.addClass(styles.dragging);jActiveItem.attr("aria-grabbed","true");setDropEffects("move")}return false}return handleDirectionKeyDown(evt)};thatReorderer.handleKeyUp=function(evt){if(!thatReorderer.activeItem||thatReorderer.activeItem!==evt.target){return true}var jActiveItem=$(thatReorderer.activeItem);if(jActiveItem.hasClass(styles.dragging)&&!isMove(evt)){if(kbDropWarning){kbDropWarning.hide()}jActiveItem.removeClass(styles.dragging);jActiveItem.addClass(styles.selected);jActiveItem.attr("aria-grabbed","false");setDropEffects("none");return false}return false};var dropMarker;var createDropMarker=function(tagName){var dropMarker=$(document.createElement(tagName));dropMarker.addClass(options.styles.dropMarker);dropMarker.hide();return dropMarker};thatReorderer.requestMovement=function(requestedPosition,item){item=fluid.unwrap(item);if(!requestedPosition||fluid.unwrap(requestedPosition.element)===item){return }var activeItem=$(thatReorderer.activeItem);activeItem.unbind("blur.fluid.reorderer");thatReorderer.events.onMove.fire(item,requestedPosition);dropManager.geometricMove(item,requestedPosition.element,requestedPosition.position);activeItem.focus();thatReorderer.refresh();dropManager.updateGeometry(thatReorderer.layoutHandler.getGeometricInfo());thatReorderer.events.afterMove.fire(item,requestedPosition,thatReorderer.dom.fastLocate("movables"))};var hoverStyleHandler=function(item,state){thatReorderer.dom.fastLocate("grabHandle",item)[state?"addClass":"removeClass"](styles.hover)};function initMovable(item){var styles=options.styles;item.attr("aria-grabbed","false");item.mouseover(function(){thatReorderer.events.onHover.fire(item,true)});item.mouseout(function(){thatReorderer.events.onHover.fire(item,false)});var avatar;thatReorderer.dom.fastLocate("grabHandle",item).draggable({refreshPositions:false,scroll:true,helper:function(){var dropWarningEl;if(mouseDropWarning){dropWarningEl=mouseDropWarning[0]}avatar=$(options.avatarCreator(item[0],styles.avatar,dropWarningEl));avatar.prop("id",createAvatarId(thatReorderer.container.id));return avatar},start:function(e,ui){var prevent=thatReorderer.events.onBeginMove.fire(item);if(prevent===false){return false}var handle=thatReorderer.dom.fastLocate("grabHandle",item)[0];var handlePos=fluid.dom.computeAbsolutePosition(handle);var handleWidth=handle.offsetWidth;var handleHeight=handle.offsetHeight;item.focus();item.removeClass(options.styles.selected);item.addClass(options.styles.mouseDrag);item.attr("aria-grabbed","true");setDropEffects("move");dropManager.startDrag(e,handlePos,handleWidth,handleHeight);avatar.show()},stop:function(e,ui){item.removeClass(options.styles.mouseDrag);item.addClass(options.styles.selected);$(thatReorderer.activeItem).attr("aria-grabbed","false");var markerNode=fluid.unwrap(dropMarker);if(markerNode.parentNode){markerNode.parentNode.removeChild(markerNode)}avatar.hide();ui.helper=null;setDropEffects("none");dropManager.endDrag();thatReorderer.requestMovement(dropManager.lastPosition(),item);thatReorderer.activeItem.focus()},handle:thatReorderer.dom.fastLocate("grabHandle",item)})}function changeSelectedToDefault(jItem,styles){jItem.removeClass(styles.selected);jItem.removeClass(styles.dragging);jItem.addClass(styles.defaultStyle);jItem.attr("aria-selected","false")}var selectItem=function(anItem){thatReorderer.events.onSelect.fire(anItem);var styles=options.styles;if(thatReorderer.activeItem&&thatReorderer.activeItem!==anItem){changeSelectedToDefault($(thatReorderer.activeItem),styles)}thatReorderer.activeItem=anItem;var jItem=$(anItem);jItem.removeClass(styles.defaultStyle);jItem.addClass(styles.selected);jItem.attr("aria-selected","true")};var initSelectables=function(){var handleBlur=function(evt){changeSelectedToDefault($(this),options.styles);return evt.stopPropagation()};var handleFocus=function(evt){selectItem(this);return evt.stopPropagation()};var selectables=thatReorderer.dom.fastLocate("selectables");for(var i=0;i<selectables.length;++i){var selectable=$(selectables[i]);if(!$.data(selectable[0],"fluid.reorderer.selectable-initialised")){selectable.addClass(styles.defaultStyle);selectable.bind("blur.fluid.reorderer",handleBlur);selectable.focus(handleFocus);selectable.click(function(evt){var handle=fluid.unwrap(thatReorderer.dom.fastLocate("grabHandle",this));if(fluid.dom.isContainer(handle,evt.target)){$(this).focus()}});selectable.attr("role",options.containerRole.item);selectable.attr("aria-selected","false");selectable.attr("aria-disabled","false");$.data(selectable[0],"fluid.reorderer.selectable-initialised",true)}}if(!thatReorderer.selectableContext){thatReorderer.selectableContext=fluid.selectable(thatReorderer.container,{selectableElements:selectables,selectablesTabindex:thatReorderer.options.selectablesTabindex,direction:null})}};var dropChangeListener=function(dropTarget){fluid.moveDom(dropMarker,dropTarget.element,dropTarget.position);dropMarker.css("display","");if(mouseDropWarning){if(dropTarget.lockedelem){mouseDropWarning.show()}else{mouseDropWarning.hide()}}};var initItems=function(){var movables=thatReorderer.dom.fastLocate("movables");var dropTargets=thatReorderer.dom.fastLocate("dropTargets");initSelectables();for(var i=0;i<movables.length;i++){var item=movables[i];if(!$.data(item,"fluid.reorderer.movable-initialised")){initMovable($(item));$.data(item,"fluid.reorderer.movable-initialised",true)}}if(movables.length>0&&!dropMarker){dropMarker=createDropMarker(movables[0].tagName)}dropManager.updateGeometry(thatReorderer.layoutHandler.getGeometricInfo());dropManager.dropChangeFirer.addListener(dropChangeListener,"fluid.Reorderer");dropTargets.attr("aria-dropeffect","none")};if(thatReorderer.container){bindHandlersToContainer(thatReorderer.container,thatReorderer.handleKeyDown,thatReorderer.handleKeyUp);addRolesToContainer(thatReorderer);fluid.tabbable(thatReorderer.container);initItems()}if(options.afterMoveCallbackUrl){thatReorderer.events.afterMove.addListener(function(){var layoutHandler=thatReorderer.layoutHandler;var model=layoutHandler.getModel?layoutHandler.getModel():options.acquireModel(thatReorderer);$.post(options.afterMoveCallbackUrl,JSON.stringify(model))},"postModel")}thatReorderer.events.onHover.addListener(hoverStyleHandler,"style");thatReorderer.refresh=function(){thatReorderer.dom.refresh("movables");thatReorderer.dom.refresh("selectables");thatReorderer.dom.refresh("grabHandle",thatReorderer.dom.fastLocate("movables"));thatReorderer.dom.refresh("stylisticOffset",thatReorderer.dom.fastLocate("movables"));thatReorderer.dom.refresh("dropTargets");thatReorderer.events.onRefresh.fire();initItems();thatReorderer.selectableContext.selectables=thatReorderer.dom.fastLocate("selectables");thatReorderer.selectableContext.selectablesUpdated(thatReorderer.activeItem)};fluid.initDependents(thatReorderer);thatReorderer.refresh();return thatReorderer};fluid.reorderer.keys={TAB:9,ENTER:13,SHIFT:16,CTRL:17,ALT:18,META:19,SPACE:32,LEFT:37,UP:38,RIGHT:39,DOWN:40,i:73,j:74,k:75,m:77};fluid.reorderer.defaultKeysets=[{modifier:function(evt){return evt.ctrlKey},up:fluid.reorderer.keys.UP,down:fluid.reorderer.keys.DOWN,right:fluid.reorderer.keys.RIGHT,left:fluid.reorderer.keys.LEFT},{modifier:function(evt){return evt.ctrlKey},up:fluid.reorderer.keys.i,down:fluid.reorderer.keys.m,right:fluid.reorderer.keys.k,left:fluid.reorderer.keys.j}];fluid.reorderer.roles={GRID:{container:"grid",item:"gridcell"},LIST:{container:"list",item:"listitem"},REGIONS:{container:"main",item:"article"}};var simpleInit=function(container,layoutHandler,options){options=options||{};options.layoutHandler=layoutHandler;return fluid.reorderer(container,options)};fluid.reorderList=function(container,options){return simpleInit(container,"fluid.listLayoutHandler",options)};fluid.reorderGrid=function(container,options){return simpleInit(container,"fluid.gridLayoutHandler",options)};fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY="shuffleProjectFrom";fluid.reorderer.GEOMETRIC_STRATEGY="projectFrom";fluid.reorderer.LOGICAL_STRATEGY="logicalFrom";fluid.reorderer.WRAP_LOCKED_STRATEGY="lockedWrapFrom";fluid.reorderer.NO_STRATEGY=null;fluid.reorderer.relativeInfoGetter=function(orientation,coStrategy,contraStrategy,dropManager,dom,disableWrap){return function(item,direction,forSelection){var dirorient=fluid.directionOrientation(direction);var strategy=dirorient===orientation?coStrategy:contraStrategy;return strategy!==null?dropManager[strategy](item,direction,forSelection,disableWrap):null}};fluid.defaults("fluid.reorderer",{styles:{defaultStyle:"fl-reorderer-movable-default",selected:"fl-reorderer-movable-selected",dragging:"fl-reorderer-movable-dragging",mouseDrag:"fl-reorderer-movable-dragging",hover:"fl-reorderer-movable-hover",dropMarker:"fl-reorderer-dropMarker",avatar:"fl-reorderer-avatar"},selectors:{dropWarning:".flc-reorderer-dropWarning",movables:".flc-reorderer-movable",grabHandle:"",stylisticOffset:""},avatarCreator:defaultAvatarCreator,keysets:fluid.reorderer.defaultKeysets,layoutHandler:{type:"fluid.listLayoutHandler"},events:{onShowKeyboardDropWarning:null,onSelect:null,onBeginMove:"preventable",onMove:null,afterMove:null,onHover:null,onRefresh:null},mergePolicy:{keysets:"replace","selectors.labelSource":"selectors.grabHandle","selectors.selectables":"selectors.movables","selectors.dropTargets":"selectors.movables"},components:{labeller:{type:"fluid.reorderer.labeller",options:{dom:"{reorderer}.dom",getGeometricInfo:"{reorderer}.layoutHandler.getGeometricInfo",orientation:"{reorderer}.options.orientation",layoutType:"{reorderer}.options.layoutHandler"}}},disableWrap:false});fluid.reorderer.makeGeometricInfoGetter=function(orientation,sentinelize,dom){return function(){var that={sentinelize:sentinelize,extents:[{orientation:orientation,elements:dom.fastLocate("dropTargets")}],elementMapper:function(element){return $.inArray(element,dom.fastLocate("movables"))===-1?"locked":null},elementIndexer:function(element){var selectables=dom.fastLocate("selectables");return{elementClass:that.elementMapper(element),index:$.inArray(element,selectables),length:selectables.length}}};return that}};fluid.defaults(true,"fluid.listLayoutHandler",{orientation:fluid.orientation.VERTICAL,containerRole:fluid.reorderer.roles.LIST,selectablesTabindex:-1,sentinelize:true});fluid.listLayoutHandler=function(container,options,dropManager,dom){var that={};that.getRelativePosition=fluid.reorderer.relativeInfoGetter(options.orientation,fluid.reorderer.LOGICAL_STRATEGY,null,dropManager,dom,options.disableWrap);that.getGeometricInfo=fluid.reorderer.makeGeometricInfoGetter(options.orientation,options.sentinelize,dom);return that};fluid.defaults(true,"fluid.gridLayoutHandler",{orientation:fluid.orientation.HORIZONTAL,containerRole:fluid.reorderer.roles.GRID,selectablesTabindex:-1,sentinelize:false});fluid.gridLayoutHandler=function(container,options,dropManager,dom){var that={};that.getRelativePosition=fluid.reorderer.relativeInfoGetter(options.orientation,options.disableWrap?fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY:fluid.reorderer.LOGICAL_STRATEGY,fluid.reorderer.SHUFFLE_GEOMETRIC_STRATEGY,dropManager,dom,options.disableWrap);that.getGeometricInfo=fluid.reorderer.makeGeometricInfoGetter(options.orientation,options.sentinelize,dom);return that};fluid.defaults("fluid.reorderer.labeller",{strings:{overallTemplate:"%recentStatus %item %position %movable",position:"%index of %length",position_moduleLayoutHandler:"%index of %length in %moduleCell %moduleIndex of %moduleLength",moduleCell_0:"row",moduleCell_1:"column",movable:"movable",fixed:"fixed",recentStatus:"moved from position %position"},components:{resolver:{type:"fluid.messageResolver",options:{messageBase:"{labeller}.options.strings"}}},invokers:{renderLabel:{funcName:"fluid.reorderer.labeller.renderLabel",args:["{labeller}","@0","@1"]}}});fluid.reorderer.indexRebaser=function(indices){indices.index++;if(indices.moduleIndex!==undefined){indices.moduleIndex++}return indices};fluid.reorderer.labeller=function(options){var that=fluid.initLittleComponent("fluid.reorderer.labeller",options);fluid.initDependents(that);that.dom=that.options.dom;that.moduleCell=that.resolver.resolve("moduleCell_"+that.options.orientation);var layoutType=fluid.computeNickName(that.options.layoutType);that.positionTemplate=that.resolver.lookup(["position_"+layoutType,"position"]);var movedMap={};that.returnedOptions={listeners:{onRefresh:function(){var selectables=that.dom.locate("selectables");fluid.each(selectables,function(selectable){var labelOptions={};var id=fluid.allocateSimpleId(selectable);var moved=movedMap[id];var label=that.renderLabel(selectable);var plainLabel=label;if(moved){moved.newRender=plainLabel;label=that.renderLabel(selectable,moved.oldRender.position);$(selectable).one("focusout",function(){if(movedMap[id]){var oldLabel=movedMap[id].newRender.label;delete movedMap[id];fluid.updateAriaLabel(selectable,oldLabel)}});labelOptions.dynamicLabel=true}fluid.updateAriaLabel(selectable,label.label,labelOptions)})},onMove:function(item,newPosition){fluid.clear(movedMap);var movingId=fluid.allocateSimpleId(item);movedMap[movingId]={oldRender:that.renderLabel(item)}}}};return that};fluid.reorderer.labeller.renderLabel=function(that,selectable,recentPosition){var geom=that.options.getGeometricInfo();var indices=fluid.reorderer.indexRebaser(geom.elementIndexer(selectable));indices.moduleCell=that.moduleCell;var elementClass=geom.elementMapper(selectable);var labelSource=that.dom.locate("labelSource",selectable);var recentStatus;if(recentPosition){recentStatus=that.resolver.resolve("recentStatus",{position:recentPosition})}var topModel={item:typeof (labelSource)==="string"?labelSource:fluid.dom.getElementText(fluid.unwrap(labelSource)),position:that.positionTemplate.resolveFunc(that.positionTemplate.template,indices),movable:that.resolver.resolve(elementClass==="locked"?"fixed":"movable"),recentStatus:recentStatus||""};var template=that.resolver.lookup(["overallTemplate"]);var label=template.resolveFunc(template.template,topModel);return{position:topModel.position,label:label}}})(jQuery,fluid_1_4);