canvas-lms/public/javascripts/question_banks.js

138 lines
5.1 KiB
JavaScript
Raw Normal View History

/*
* Copyright (C) 2011 - present Instructure, Inc.
2011-02-01 09:57:29 +08:00
*
* This file is part of Canvas.
*
* Canvas is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, version 3 of the License.
*
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2011-02-01 09:57:29 +08:00
*/
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
import I18n from 'i18n!question_banks'
import $ from 'jquery'
import './jquery.ajaxJSON'
import './jquery.instructure_date_and_time' /* datetimeString */
import './jquery.instructure_forms' /* formSubmit, fillFormData, formErrors */
import './jquery.instructure_misc_plugins' /* confirmDelete */
import './jquery.keycodes'
import './jquery.loadingImg'
import './jquery.templateData'
AMD Conversion "Trivial" JavaScript / CoffeeScript changes -------------------------------------------------- For the most part, all javascript was simply wrapped in `require` or `define`. The dependencies were found with a script that matched regexes in the files, it errs on the side of listing too many dependencies, so its worth double checking each file's dependencies (over time, anyway). i18n API changes -------------------------------------------------- No longer have to do I18n.scoped calls, just list i18n as a dependency with the scope and it's imported already scoped require ['i18n!some_scope'], (I18n) -> I18n.t 'im_scoped', 'I'm scoped!' JS bundling now done with r.js, not Jammit -------------------------------------------------- We don't use jammit to bundle JS anymore. Simply list dependencies for your JS modules in the file and RequireJS handles the rest. To optimize the JavaScript, first make sure you have node.js 0.4.12+ installed and then run: $ rake js:build The app defaults to the optimized build in production. You can use non-optimized in production by putting ?debug_assets=true in the url just like before. You can also test the optimized JavaScript in development with ?optimized_js=true. Significant changes -------------------------------------------------- These files have "real" changes to them (unlike the JavaScript that is simply wrapped in require and define). Worth taking a really close look at: - app/helpers/application_helper.rb - app/views/layouts/application.html.erb - config/assets.yml - config/build.js - lib/handlebars/handlebars.rb - lib/i18n_extraction/js_extractor.rb - lib/tasks/canvas.rake - lib/tasks/i18n.rake - lib/tasks/js.rake Change-Id: I4bc5ecb1231f331aaded0fef2bcc1f3a9fe482a7 Reviewed-on: https://gerrit.instructure.com/6986 Tested-by: Hudson <hudson@instructure.com> Reviewed-by: Ryan Florence <ryanf@instructure.com>
2011-11-11 00:31:45 +08:00
2011-02-01 09:57:29 +08:00
$(document).ready(function() {
$(".add_bank_link").click(function(event) {
event.preventDefault();
var $bank = $("#question_bank_blank").clone(true).attr('id', 'question_bank_new');
$("#questions").prepend($bank.show());
$bank.find(".edit_bank_link").click();
});
$(".question_bank .delete_bank_link").click(function(event) {
event.preventDefault();
$(this).parents(".question_bank").confirmDelete({
url: $(this).attr('href'),
message: I18n.t("delete_question_bank_prompt", "Are you sure you want to delete this bank of questions?"),
2011-02-01 09:57:29 +08:00
success: function() {
$(this).slideUp(function() {
$(this).remove();
});
}
});
});
$(".question_bank .bookmark_bank_link").click(function(event) {
event.preventDefault();
var $link = $(this);
var $bank = $link.parents(".question_bank");
$.ajaxJSON($(this).attr('href'), 'POST', {}, function(data) {
$bank.find(".bookmark_bank_link").toggle();
$bank.find(".bookmark_bank_link:visible:first").focus();
2011-02-01 09:57:29 +08:00
});
});
$(".question_bank .edit_bank_link").click(function(event) {
event.preventDefault();
var $bank = $(this).parents(".question_bank");
var data = $bank.getTemplateData({textValues: ['title']});
$bank.find(".header_content").hide();
var $form = $("#edit_bank_form");
$bank.find(".header").prepend($form.show());
$form.attr('action', $(this).attr('href'));
$form.attr('method', 'PUT');
if($bank.attr('id') == 'question_bank_new') {
$form.attr('action', $("#bank_urls .add_bank_url").attr('href'));
$form.attr('method', 'POST');
}
$form.fillFormData(data, {object_name: 'assessment_question_bank'});
$form.find(":text:visible:first").focus().select();
});
$("#edit_bank_form .bank_name_box").keycodes('return esc tab', function(event) {
2011-02-01 09:57:29 +08:00
if(event.keyString == 'esc') {
$(this).parents(".question_bank").addClass('dont_save')
$(this).blur();
} else if(event.keyString == 'return') {
$("#edit_bank_form").submit();
} else if(event.keyString == 'tab') {
$('nav#breadcrumbs a:visible:first').focus()
event.preventDefault();
2011-02-01 09:57:29 +08:00
}
});
$("#edit_bank_form .bank_name_box").blur(function() {
var $bank = $(this).parents(".question_bank");
if(!$bank.hasClass('dont_save') && !$bank.hasClass('save_in_progress') && $bank.attr('id') != 'question_bank_new') {
2011-02-01 09:57:29 +08:00
$("#edit_bank_form").submit();
return;
}
$bank.removeClass('dont_save');
$bank.find(".header_content").show();
$("body").append($("#edit_bank_form").hide());
if($bank.attr('id') == 'question_bank_new') {
$bank.remove();
}
});
$("#edit_bank_form").formSubmit({
object_name: 'assessment_question_bank',
beforeSubmit: function(data) {
var $bank = $(this).parents(".question_bank");
$bank.attr('id', 'question_bank_adding');
try {
$bank.addClass('save_in_progress')
2011-02-01 09:57:29 +08:00
$bank.find(".bank_name_box").blur();
} catch(e) { }
$bank.fillTemplateData({
data: data
});
$bank.loadingImage();
return $bank;
},
success: function(data, $bank) {
$bank.loadingImage('remove');
$bank.removeClass('save_in_progress')
2011-02-01 09:57:29 +08:00
var bank = data.assessment_question_bank;
bank.last_updated_at = $.datetimeString(bank.updated_at);
2011-02-01 09:57:29 +08:00
$bank.fillTemplateData({
data: bank,
hrefValues: ['id']
})
// if you can convince fillTemplateData to do this, please be my guest
$bank.find('.links a').each(function(_, link) {
link.setAttribute('title', link.getAttribute('title').replace('{{ title }}', bank.title));
});
$bank.find('.links a span').each(function(_, span) {
span.textContent = span.textContent.replace('{{ title }}', bank.title);
});
$bank.find('a.title')[0].focus()
2011-02-01 09:57:29 +08:00
},
error: function(data, $bank) {
$bank.loadingImage('remove');
$bank.removeClass('save_in_progress')
2011-02-01 09:57:29 +08:00
$bank.find(".edit_bank_link").click();
$("#edit_bank_form").formErrors(data);
}
});
});