Merge pull request #12795 from eduNEXT/fmo/coffee_to_js_cms_main

Convert cms/coffee/src/main to js
This commit is contained in:
Andy Armstrong
2016-07-20 15:16:48 -04:00
committed by GitHub
22 changed files with 173 additions and 136 deletions

69
cms/static/cms/js/main.js Normal file
View File

@@ -0,0 +1,69 @@
(function(AjaxPrefix) {
'use strict';
define(['domReady', 'jquery', 'underscore.string', 'backbone', 'gettext',
'common/js/components/views/feedback_notification', 'coffee/src/ajax_prefix',
'jquery.cookie'],
function(domReady, $, str, Backbone, gettext, NotificationView) {
var main;
main = function() {
AjaxPrefix.addAjaxPrefix(jQuery, function() {
return $("meta[name='path_prefix']").attr('content');
});
window.CMS = window.CMS || {};
window.CMS.URL = window.CMS.URL || {};
window.onTouchBasedDevice = function() {
return navigator.userAgent.match(/iPhone|iPod|iPad|Android/i);
};
_.extend(window.CMS, Backbone.Events);
Backbone.emulateHTTP = true;
$.ajaxSetup({
headers: {
'X-CSRFToken': $.cookie('csrftoken')
},
dataType: 'json'
});
$(document).ajaxError(function(event, jqXHR, ajaxSettings) {
var message, msg;
if (ajaxSettings.notifyOnError === false) {
return;
}
if (jqXHR.responseText) {
try {
message = JSON.parse(jqXHR.responseText).error;
} catch (error) {
message = str.truncate(jqXHR.responseText, 300);
}
} else {
message = gettext('This may be happening because of an error with our server or your internet connection. Try refreshing the page or making sure you are online.'); //jshint ignore:line
}
msg = new NotificationView.Error({
'title': gettext("Studio's having trouble saving your work"),
'message': message
});
return msg.show();
});
$.postJSON = function(url, data, callback) {
if ($.isFunction(data)) {
callback = data;
data = undefined;
}
return $.ajax({
url: url,
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(data),
success: callback
});
};
return domReady(function() {
if (window.onTouchBasedDevice()) {
return $('body').addClass('touch-based-device');
}
});
};
main();
return main;
});
}).call(this, AjaxPrefix); //jshint ignore:line

View File

@@ -245,7 +245,7 @@
exports: 'XBlock',
deps: ['xblock/core']
},
'coffee/src/main': {
'cms/js/main': {
deps: ['coffee/src/ajax_prefix']
},
'js/src/logger': {

View File

@@ -197,7 +197,7 @@
'mock-ajax': {
deps: ['jquery']
},
'coffee/src/main': {
'cms/js/main': {
deps: ['coffee/src/ajax_prefix']
},
'coffee/src/ajax_prefix': {
@@ -209,8 +209,8 @@
jasmine.getFixtures().fixturesPath += 'coffee/fixtures';
testFiles = [
'cms/js/spec/main_spec',
'cms/js/spec/xblock/cms.runtime.v1_spec',
'coffee/spec/main_spec',
'coffee/spec/models/course_spec',
'coffee/spec/models/metadata_spec',
'coffee/spec/models/section_spec',

View File

@@ -0,0 +1,81 @@
(function(sandbox) {
'use strict';
require(["jquery", "backbone", "cms/js/main", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "jquery.cookie"],
function($, Backbone, main, AjaxHelpers) {
describe("CMS", function() {
it("should initialize URL", function() {
expect(window.CMS.URL).toBeDefined();
});
});
describe("main helper", function() {
beforeEach(function() {
this.previousAjaxSettings = $.extend(true, {}, $.ajaxSettings);
spyOn($, "cookie").and.callFake(function(param) {
if (param === "csrftoken") {
return "stubCSRFToken";
}
});
return main();
});
afterEach(function() {
$.ajaxSettings = this.previousAjaxSettings;
return $.ajaxSettings;
});
it("turn on Backbone emulateHTTP", function() {
expect(Backbone.emulateHTTP).toBeTruthy();
});
it("setup AJAX CSRF token", function() {
expect($.ajaxSettings.headers["X-CSRFToken"]).toEqual("stubCSRFToken");
});
});
describe("AJAX Errors", function() {
var server;
server = null;
beforeEach(function() {
appendSetFixtures(sandbox({
id: "page-notification"
}));
});
afterEach(function() {
return server && server.restore();
});
it("successful AJAX request does not pop an error notification", function() {
server = AjaxHelpers.server([
200, {
"Content-Type": "application/json"
}, "{}"
]);
expect($("#page-notification")).toBeEmpty();
$.ajax("/test");
expect($("#page-notification")).toBeEmpty();
server.respond();
expect($("#page-notification")).toBeEmpty();
});
it("AJAX request with error should pop an error notification", function() {
server = AjaxHelpers.server([
500, {
"Content-Type": "application/json"
}, "{}"
]);
$.ajax("/test");
server.respond();
expect($("#page-notification")).not.toBeEmpty();
expect($("#page-notification")).toContainElement('div.wrapper-notification-error');
});
it("can override AJAX request with error so it does not pop an error notification", function() {
server = AjaxHelpers.server([
500, {
"Content-Type": "application/json"
}, "{}"
]);
$.ajax({
url: "/test",
notifyOnError: false
});
server.respond();
expect($("#page-notification")).toBeEmpty();
});
});
});
}).call(this, sandbox); //jshint ignore:line

View File

@@ -172,7 +172,7 @@
exports: 'XBlock',
deps: ['xblock/core']
},
'coffee/src/main': {
'cms/js/main': {
deps: ['coffee/src/ajax_prefix']
},
'coffee/src/ajax_prefix': {