$(function() {
var selectable = $("#selectable") // selectable words
, selections = $("#selections") // selected words container parent
, selected = $(".selected") // selected words container
, clear = $("#clear") // clear all
, sel = ".ui-selected" // selected word
, content = ".ui-widget-content" // selectable words element `class`
, text = ".text" // selected words draggable, resizable container `class`
, handles = {} // resize handles
, str = "The quick brown fox jumps over the lazy dog ..." // text, `string`
// split `str`, return array of words
, words = str.split(" ").map(function(word, i) {
$("<span />", {
"class": content.slice(1),
"text": word,
"css": {
"margin": "2px"
}
}).appendTo(selectable)
});
// append `handles` to `text` container
$.each(["nw", "ne", "sw", "se"], function(_, handle) {
var elem = $("<div />", {
"class": "ui-resizable-handle ui-resizable-" + handle,
"css": {
"width": "calc(12px + 5%)",
"height": "calc(12px + 5%)",
"border-radius": "50%",
"background": "#000"
}
})
.appendTo(selected)
.parent().find(":last") // `se`
.css({
"right": "-5px",
"bottom": "-5px"
});
handles[handle] = elem[0];
});
// `selected` settings
// set `selected` `display` to `none`
selected
.hide(0)
.resizable({
handles: handles
})
.draggable({
containment: "parent"
});
// collect selected words at `selectable.data("selections", [])` array,
// append words to `text`,
// append `text` to `selected`,
// set `selected` `display` to `block`
selectable.data("selections", [])
.selectable({
selected: function(event, ui) {
$(this).data("selections").push($(ui.selected).text())
},
stop: function() {
selected
.find(".text")
.remove()
.addBack()
.prepend(
$("<div />", {
"class": "text",
"text": $(this).data("selections").join(" "),
"css": {
"position": "relative",
"display": "block",
"padding": "calc(15%)",
"height": "calc(50%)",
"white-space": "pre-line",
"overflow": "hidden"
}
})
).show(0)
}
});
// remove selected words from `selectable.data("selections", [])` array,
// remove `sel` `class` from `selectable` words,
// remove `text`,
// set `selected` display to `none`
clear.on("click", function(e) {
selectable.data("selections", [])
.find(sel).removeClass(sel.slice(1));
selections.find(text).remove()
.addBack().find(selected).hide(0)
});
// do stuff
var phrase = [0, 1, 2, 8, 4, 5, 6, 7, 3, 9];
setTimeout(function() {
$.when(
selectable.queue("phrase", $.map(phrase, function(word, i) {
return function(next) {
return $.when(!$(this).find(content).eq(word)
.addClass("ui-selecting").parent()
.data("ui-selectable")._mouseStop(false) && $(this)
).then(function(el) {
return el.delay(2000);
}).then(next)
}
})
)
.dequeue("phrase").promise("phrase")
, clear
)
.then(function(el, button) {
button.trigger("click")
});
}, 2713);
});
#selectable .ui-selecting {
background: #FECA40;
}
#selectable .ui-selected {
background: #F39814;
color: white;
}
#selections {
width: 100%;
height: 400px;
}
#clear {
position: relative;
left: calc(37%);
}
.selected {
width: 150px;
height: 150px;
overflow: hidden;
}
.selected,
.text {
display: block;
background-color: rgb(225, 225, 225);
}
.ui-widget-content {
border: 1px dotted rgba(170, 170, 170, .25);
}
.ui-widget-conent span {
display: inline-block;
padding: 4px;
margin: 4px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="http://code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<link href="http://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"
rel="stylesheet" />
<div id="selectable"></div>
<br />
<hr />
<button id="clear">clear selections</button>
<hr />
<br />
<div id="selections">
<div class="selected"></div>
</div>