submit.js.tpl 8.0 KB

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