moveMultipleQuestionBanks.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C) 2016 - present Instructure, Inc.
  3. *
  4. * This file is part of Canvas.
  5. *
  6. * Canvas is free software: you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License as published by the Free
  8. * Software Foundation, version 3 of the License.
  9. *
  10. * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
  11. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. import I18n from 'i18n!question_bank'
  19. import $ from 'jquery'
  20. import moveQuestionTemplate from 'jst/quiz/move_question'
  21. import htmlEscape from 'str/htmlEscape'
  22. import loadBanks from 'jsx/quizzes/question_bank/loadBanks'
  23. import 'jquery.ajaxJSON'
  24. import 'jquery.instructure_forms' /* formSubmit, getFormData, formErrors */
  25. import 'jqueryui/dialog'
  26. import 'jquery.instructure_misc_helpers' /* replaceTags */
  27. import 'jquery.instructure_misc_plugins' /* confirmDelete, showIf, .dim */
  28. import 'jquery.keycodes' /* keycodes */
  29. import 'jquery.loadingImg' /* loadingImage */
  30. import 'jquery.templateData' /* fillTemplateData, getTemplateData */
  31. var moveQuestions = {
  32. elements: {
  33. $dialog: () => {return $('#move_question_dialog')},
  34. $loadMessage: $('<li />').append(htmlEscape(I18n.t('load_questions', 'Loading Questions...'))),
  35. $questions: $('#move_question_dialog .questions')
  36. },
  37. messages: {
  38. move_copy_questions: I18n.t('title.move_copy_questions', "Move/Copy Questions"),
  39. move_questions: I18n.t('move_questions', 'Move Questions'),
  40. multiple_questions: I18n.t('multiple_questions', 'Multiple Questions')
  41. },
  42. page: 1,
  43. addEvents: function(){
  44. $('.move_questions_link').bind('click.moveQuestions', $.proxy(this.onClick, this))
  45. return this
  46. },
  47. onClick: function(e){
  48. e.preventDefault()
  49. this.prepDialog()
  50. this.showDialog()
  51. this.loadData()
  52. this.elements
  53. .$dialog()
  54. .parent()
  55. .find('.ui-dialog-titlebar-close')[0]
  56. .focus()
  57. },
  58. prepDialog: function(){
  59. this.elements.$dialog().find('.question_text').hide()
  60. this.elements.$questions.show()
  61. this.elements.$questions.find('.list_question:not(.blank)').remove()
  62. this.elements.$dialog().find('.question_name').text(this.messages.multiple_questions)
  63. this.elements.$dialog().find('.copy_option').hide().find(':checkbox').attr('checked', false)
  64. this.elements.$dialog().find('.submit_button').text(this.messages.move_questions)
  65. this.elements.$dialog().find('.multiple_questions').val('1')
  66. this.elements.$dialog().data('question', null)
  67. },
  68. showDialog: function(){
  69. if (!this.elements.$dialog().hasClass('loaded')){
  70. loadBanks(this.elements.$dialog())
  71. } else {
  72. this.elements.$dialog().find('li message').hide()
  73. }
  74. this.elements.$dialog().dialog({
  75. title: this.messages.move_copy_questions,
  76. width: 600
  77. })
  78. },
  79. loadData: function(){
  80. this.elements.$questions.append(this.elements.$loadMessage)
  81. $.ajaxJSON(window.location.href + '/questions?page=' + this.page, 'GET', {}, $.proxy(this.onData, this))
  82. },
  83. onData: function(data){
  84. const html = moveQuestionTemplate(data)
  85. this.elements.$loadMessage.remove()
  86. this.elements.$questions.append(html)
  87. if (this.page < data.pages){
  88. this.elements.$questions.append(this.elements.$loadMessage)
  89. this.page += 1
  90. this.loadData()
  91. } else {
  92. this.page = 1
  93. }
  94. }
  95. }
  96. export default moveQuestions