submit.js.tpl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <script>
  2. var DraggableAnswer = {
  3. gallery: null,
  4. trash: null,
  5. deleteItem: function (item, insertHere) {
  6. if (insertHere.find(".exercise-draggable-answer-option").length > 0) {
  7. return false;
  8. }
  9. item.fadeOut(function () {
  10. var $list = $('<div class="gallery ui-helper-reset"/>').appendTo(insertHere);
  11. item.find('a.btn').remove();
  12. var droppedId = item.attr('id'),
  13. dropedOnId = insertHere.attr('id'),
  14. originSelectId = 'window_' + droppedId + '_select',
  15. value = dropedOnId.split('_')[2];
  16. $('#' + originSelectId + ' option')
  17. .filter(function (index) {
  18. return index === parseInt(value);
  19. })
  20. .attr("selected", true);
  21. var recycleButton = $('<a>')
  22. .attr('href', '#')
  23. .addClass('btn btn-default btn-xs')
  24. .append(
  25. "{{ "Undo" | get_lang }} ",
  26. $('<i>').addClass('fa fa-undo')
  27. )
  28. .on('click', function (e) {
  29. e.preventDefault();
  30. var liParent = $(this).parent();
  31. DraggableAnswer.recycleItem(liParent);
  32. });
  33. item.append(recycleButton).appendTo($list).fadeIn();
  34. });
  35. },
  36. recycleItem: function (item) {
  37. item.fadeOut(function () {
  38. item
  39. .find('a.btn')
  40. .remove()
  41. .end()
  42. .find("img")
  43. .end()
  44. .appendTo(DraggableAnswer.gallery)
  45. .fadeIn();
  46. });
  47. var droppedId = item.attr('id'),
  48. originSelectId = 'window_' + droppedId + '_select';
  49. $('#' + originSelectId + ' option:first').attr('selected', 'selected');
  50. },
  51. init: function (gallery, trash) {
  52. this.gallery = gallery;
  53. this.trash = trash;
  54. $("li", DraggableAnswer.gallery).draggable({
  55. cancel: "a.ui-icon",
  56. revert: "invalid",
  57. containment: "document",
  58. helper: "clone",
  59. cursor: "move"
  60. });
  61. DraggableAnswer.trash.droppable({
  62. accept: ".exercise-draggable-answer > li",
  63. hoverClass: "ui-state-active",
  64. drop: function (e, ui) {
  65. DraggableAnswer.deleteItem(ui.draggable, $(this));
  66. }
  67. });
  68. DraggableAnswer.gallery.droppable({
  69. drop: function (e, ui) {
  70. DraggableAnswer.recycleItem(ui.draggable, $(this));
  71. }
  72. });
  73. }
  74. };
  75. var MatchingDraggable = {
  76. colorDestination: '#316B31',
  77. curviness: 0,
  78. connectorType: 'Straight',
  79. initialized: false,
  80. init: function (questionId) {
  81. var windowQuestionSelector = '.window' + questionId + '_question',
  82. countConnections = $(windowQuestionSelector).length,
  83. colorArray = [],
  84. colorArrayDestination = [];
  85. if (countConnections > 0) {
  86. colorArray = $.xcolor.analogous("#da0", countConnections);
  87. colorArrayDestination = $.xcolor.analogous("#51a351", countConnections);
  88. } else {
  89. colorArray = $.xcolor.analogous("#da0", 10);
  90. colorArrayDestination = $.xcolor.analogous("#51a351", 10);
  91. }
  92. jsPlumb.importDefaults({
  93. DragOptions: {cursor: 'pointer', zIndex: 2000},
  94. PaintStyle: {strokeStyle: '#000'},
  95. EndpointStyle: {strokeStyle: '#316b31'},
  96. Endpoint: 'Rectangle',
  97. Anchors: ['TopCenter', 'TopCenter']
  98. });
  99. var exampleDropOptions = {
  100. tolerance: 'touch',
  101. hoverClass: 'dropHover',
  102. activeClass: 'dragActive'
  103. };
  104. var destinationEndPoint = {
  105. endpoint: ["Dot", {radius: 15}],
  106. paintStyle: {fillStyle: MatchingDraggable.colorDestination},
  107. isSource: false,
  108. connectorStyle: {strokeStyle: MatchingDraggable.colorDestination, lineWidth: 8},
  109. connector: [
  110. MatchingDraggable.connectorType,
  111. {curviness: MatchingDraggable.curviness}
  112. ],
  113. maxConnections: 1000,
  114. isTarget: true,
  115. dropOptions: exampleDropOptions,
  116. beforeDrop: function (params) {
  117. jsPlumb.select({source: params.sourceId}).each(function (connection) {
  118. jsPlumb.detach(connection);
  119. });
  120. var selectId = params.sourceId + "_select";
  121. var value = params.targetId.split("_")[2];
  122. $("#" + selectId + " option")
  123. .removeAttr('selected')
  124. .filter(function (index) {
  125. return index === parseInt(value);
  126. })
  127. .attr("selected", true);
  128. return true;
  129. }
  130. };
  131. var count = 0;
  132. var sourceDestinationArray = [];
  133. $(windowQuestionSelector).each(function (index) {
  134. var windowId = $(this).attr("id");
  135. var scope = windowId + "scope";
  136. var destinationColor = colorArray[count].getHex();
  137. var sourceEndPoint = {
  138. endpoint: [
  139. "Dot",
  140. {radius: 15}
  141. ],
  142. paintStyle: {
  143. fillStyle: destinationColor
  144. },
  145. isSource: true,
  146. connectorStyle: {
  147. strokeStyle: "#8a8888",
  148. lineWidth: 8
  149. },
  150. connector: [
  151. MatchingDraggable.connectorType,
  152. {curviness: MatchingDraggable.curviness}
  153. ],
  154. maxConnections: 1,
  155. isTarget: false,
  156. dropOptions: exampleDropOptions,
  157. scope: scope
  158. };
  159. sourceDestinationArray[count + 1] = sourceEndPoint;
  160. count++;
  161. jsPlumb.addEndpoint(
  162. windowId,
  163. {
  164. anchor: ['RightMiddle', 'RightMiddle', 'RightMiddle', 'RightMiddle']
  165. },
  166. sourceEndPoint
  167. );
  168. var destinationCount = 0;
  169. $(windowQuestionSelector).each(function (index) {
  170. var windowDestinationId = $(this).attr("id");
  171. destinationEndPoint.scope = scope;
  172. destinationEndPoint.paintStyle.fillStyle = colorArrayDestination[destinationCount].getHex();
  173. destinationCount++;
  174. jsPlumb.addEndpoint(
  175. windowDestinationId + "_answer",
  176. {
  177. anchors: ['LeftMiddle', 'LeftMiddle', 'LeftMiddle', 'LeftMiddle']
  178. },
  179. destinationEndPoint
  180. );
  181. });
  182. });
  183. MatchingDraggable.attachBehaviour();
  184. },
  185. attachBehaviour: function () {
  186. if (!MatchingDraggable.initialized) {
  187. MatchingDraggable.initialized = true;
  188. }
  189. }
  190. };
  191. jsPlumb.ready(function () {
  192. if ($(".drag_question").length > 0) {
  193. MatchingDraggable.init();
  194. $(document).scroll(function () {
  195. jsPlumb.repaintEverything();
  196. });
  197. $(window).resize(function () {
  198. jsPlumb.repaintEverything();
  199. });
  200. }
  201. });
  202. $(document).on('ready', function () {
  203. DraggableAnswer.init(
  204. $(".exercise-draggable-answer"),
  205. $(".droppable")
  206. );
  207. });
  208. </script>