jqueryui_interactions.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # jQuery UI interactions
  4. *
  5. * Demo JS code for jqueryui_interactions.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var JqueryUiInteractions = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Draggable
  15. var _componentUiDraggable = function() {
  16. if (!$().draggable) {
  17. console.warn('Warning - jQuery UI components are not loaded.');
  18. return;
  19. }
  20. // Basic setup
  21. $('.draggable-element').draggable({
  22. containment: '#draggable-default-container'
  23. });
  24. //
  25. // Constrain movement
  26. //
  27. // Both
  28. $('#draggable-move-both').draggable({
  29. containment: '#draggable-containment-container'
  30. });
  31. // Vertical
  32. $('#draggable-move-vertical').draggable({
  33. containment: '#draggable-containment-container',
  34. axis: 'y'
  35. });
  36. // Horizontal
  37. $('#draggable-move-horizontal').draggable({
  38. containment: '#draggable-containment-container',
  39. axis: 'x'
  40. });
  41. //
  42. // Revert position
  43. //
  44. // Element
  45. $('#draggable-revert-element').draggable({
  46. containment: '#draggable-revert-container',
  47. revert: true
  48. });
  49. // Clone helper
  50. $('#draggable-revert-clone').draggable({
  51. containment: '#draggable-revert-container',
  52. revert: true,
  53. helper: 'clone'
  54. });
  55. // Speed
  56. $('#draggable-revert-speed').draggable({
  57. containment: '#draggable-revert-container',
  58. revert: true,
  59. revertDuration: 1500
  60. });
  61. //
  62. // Cursors
  63. //
  64. // Move cursor
  65. $('#draggable-cursor-move').draggable({
  66. containment: '#draggable-cursor-container',
  67. cursor: 'move'
  68. });
  69. // Crosshair cursor
  70. $( '#draggable-cursor-crosshair' ).draggable({
  71. containment: '#draggable-cursor-container',
  72. cursor: 'crosshair'
  73. });
  74. // Bottom cursor
  75. $( '#draggable-cursor-bottom' ).draggable({
  76. containment: '#draggable-cursor-container',
  77. cursorAt: {
  78. bottom: 0
  79. }
  80. });
  81. //
  82. // Handles
  83. //
  84. // Text
  85. $( '#draggable-handle-text' ).draggable({
  86. containment: '#draggable-handle-container',
  87. handle: 'span'
  88. });
  89. // Icon
  90. $( '#draggable-handle-icon' ).draggable({
  91. containment: '#draggable-handle-container',
  92. handle: '.handle-icon'
  93. });
  94. // Exception
  95. $( '#draggable-handle-exception' ).draggable({
  96. containment: '#draggable-handle-container',
  97. cancel: 'span'
  98. });
  99. //
  100. // Events
  101. //
  102. // Define elements
  103. var $start_counter = $('#draggable-event-start'),
  104. $drag_counter = $('#draggable-event-drag'),
  105. $stop_counter = $('#draggable-event-stop'),
  106. counts = [0, 0, 0];
  107. // Start event
  108. $start_counter.draggable({
  109. containment: '#draggable-events-container',
  110. start: function() {
  111. counts[0]++;
  112. updateCounterStatus( $start_counter, counts[0]);
  113. }
  114. });
  115. // Drag event
  116. $drag_counter.draggable({
  117. containment: '#draggable-events-container',
  118. drag: function() {
  119. counts[1]++;
  120. updateCounterStatus($drag_counter, counts[1]);
  121. }
  122. });
  123. // Stop event
  124. $stop_counter.draggable({
  125. containment: '#draggable-events-container',
  126. stop: function() {
  127. counts[2]++;
  128. updateCounterStatus($stop_counter, counts[2]);
  129. }
  130. });
  131. // Update counter text
  132. function updateCounterStatus( $event_counter, new_count ) {
  133. $( '.event-count', $event_counter ).text( new_count );
  134. }
  135. };
  136. // Droppable
  137. var _componentUiDroppable = function() {
  138. if (!$().draggable || !$().droppable) {
  139. console.warn('Warning - jQuery UI components are not loaded.');
  140. return;
  141. }
  142. //
  143. // Basic functionality
  144. //
  145. // Drag
  146. $('#droppable-basic-element').draggable({
  147. containment: '#droppable-basic-container'
  148. });
  149. // Drop
  150. $('#droppable-basic-target').droppable({
  151. drop: function(event, ui) {
  152. $(this).addClass('bg-success-400 border-success-400 text-white').html('<span>Dropped!</span>');
  153. }
  154. });
  155. //
  156. // Accept drop
  157. //
  158. // Drag
  159. $('#droppable-accept-yes, #droppable-accept-no').draggable({
  160. containment: '#droppable-accept-container'
  161. });
  162. // Drop
  163. $('#droppable-accept-target').droppable({
  164. accept: '#droppable-accept-yes',
  165. drop: function(event, ui) {
  166. $(this).addClass('bg-success-400 border-success-400 text-white').html('<span>Dropped!</span>');
  167. }
  168. });
  169. //
  170. // Revert draggable position
  171. //
  172. // Drag (revert on drop)
  173. $('#droppable-revert-drop').draggable({
  174. containment: '#droppable-revert-container',
  175. revert: 'valid'
  176. });
  177. // Drag (revert always except drop)
  178. $('#droppable-revert-except').draggable({
  179. containment: '#droppable-revert-container',
  180. revert: 'invalid'
  181. });
  182. // Drop
  183. $('#droppable-revert-target').droppable({
  184. drop: function(event, ui) {
  185. $(this).addClass('bg-success-400 border-success-400 text-white').html('<span>Dropped!</span>');
  186. }
  187. });
  188. //
  189. // Visual feedback
  190. //
  191. // Drag
  192. $('#droppable-visual-element').draggable({
  193. containment: '#droppable-visual-container'
  194. });
  195. // Active drop
  196. $('#droppable-visual-target-active').droppable({
  197. containment: '#droppable-visual-container',
  198. accept: '#droppable-visual-element',
  199. activeClass: 'bg-slate border-slate text-white',
  200. drop: function(event, ui) {
  201. $(this).addClass('bg-success-400 border-success-400 text-white').html('<span>Dropped!</span>');
  202. }
  203. });
  204. // Hover drop
  205. $('#droppable-visual-target-hover').droppable({
  206. containment: '#droppable-visual-container',
  207. hoverClass: 'bg-blue border-blue text-white',
  208. drop: function(event, ui) {
  209. $(this).addClass('bg-teal-400 border-teal-400 text-white').html('<span>Dropped!</span>');
  210. }
  211. });
  212. };
  213. // Resizable
  214. var _componentUiResizable = function() {
  215. if (!$().resizable) {
  216. console.warn('Warning - jQuery UI components are not loaded.');
  217. return;
  218. }
  219. // Basic functionality
  220. $('#resizable-basic-element').resizable({
  221. minWidth: 50,
  222. minHeight: 50
  223. });
  224. // Animated resize
  225. $('#resizable-animate-element').resizable({
  226. minWidth: 50,
  227. minHeight: 50,
  228. animate: true
  229. });
  230. // Preserve aspect ratio
  231. $('#resizable-ratio-element').resizable({
  232. minWidth: 50,
  233. minHeight: 50,
  234. aspectRatio: 16 / 9
  235. });
  236. // Synchronous resize
  237. $('#resizable-sync-element1').resizable({
  238. minWidth: 50,
  239. minHeight: 50,
  240. alsoResize: '#resizable-sync-element2'
  241. });
  242. $('#resizable-sync-element2').resizable({
  243. minWidth: 50,
  244. minHeight: 50,
  245. alsoResize: '#resizable-sync-element1'
  246. });
  247. };
  248. // Selectable
  249. var _componentUiSelectable = function() {
  250. if (!$().selectable) {
  251. console.warn('Warning - jQuery UI components are not loaded.');
  252. return;
  253. }
  254. // Basic functionality
  255. $('#selectable-basic').selectable();
  256. // Serialize
  257. $('#selectable-serialize').selectable({
  258. stop: function() {
  259. var result = $('#select-result').empty();
  260. $('.ui-selected', this).each(function() {
  261. var index = $('#selectable-serialize li').index(this);
  262. result.append(' #' + (index + 1));
  263. });
  264. }
  265. });
  266. };
  267. // Sortable
  268. var _componentUiSortable = function() {
  269. if (!$().sortable) {
  270. console.warn('Warning - jQuery UI components are not loaded.');
  271. return;
  272. }
  273. // Basic functionality
  274. $('#sortable-list-basic').sortable();
  275. $('#sortable-list-basic').disableSelection();
  276. // Placeholder
  277. $('#sortable-list-placeholder').sortable({
  278. placeholder: 'sortable-placeholder',
  279. start: function(e, ui){
  280. ui.placeholder.height(ui.item.outerHeight());
  281. }
  282. });
  283. $('#sortable-list-placeholder').disableSelection();
  284. // Connected lists
  285. $('#sortable-list-first, #sortable-list-second').sortable({
  286. connectWith: '.selectable-demo-connected'
  287. }).disableSelection();
  288. //
  289. // Include/exclude items
  290. //
  291. // Specify sort items
  292. $('#sortable-list-specify').sortable({
  293. items: 'li:not(.ui-handle-excluded)'
  294. });
  295. // Exclude items
  296. $('#sortable-list-cancel').sortable({
  297. cancel: '.ui-handle-excluded'
  298. });
  299. // Disable selections
  300. $('#sortable-list-specify li, #sortable-list-cancel li').disableSelection();
  301. };
  302. //
  303. // Return objects assigned to module
  304. //
  305. return {
  306. init: function() {
  307. _componentUiDraggable();
  308. _componentUiDroppable();
  309. _componentUiResizable();
  310. _componentUiSelectable();
  311. _componentUiSortable();
  312. }
  313. }
  314. }();
  315. // Initialize module
  316. // ------------------------------
  317. document.addEventListener('DOMContentLoaded', function() {
  318. JqueryUiInteractions.init();
  319. });