Merge pull request #12795 from eduNEXT/fmo/coffee_to_js_cms_main
Convert cms/coffee/src/main to js
This commit is contained in:
69
cms/static/cms/js/main.js
Normal file
69
cms/static/cms/js/main.js
Normal 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
|
||||
@@ -245,7 +245,7 @@
|
||||
exports: 'XBlock',
|
||||
deps: ['xblock/core']
|
||||
},
|
||||
'coffee/src/main': {
|
||||
'cms/js/main': {
|
||||
deps: ['coffee/src/ajax_prefix']
|
||||
},
|
||||
'js/src/logger': {
|
||||
|
||||
@@ -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',
|
||||
|
||||
81
cms/static/cms/js/spec/main_spec.js
Normal file
81
cms/static/cms/js/spec/main_spec.js
Normal 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
|
||||
@@ -172,7 +172,7 @@
|
||||
exports: 'XBlock',
|
||||
deps: ['xblock/core']
|
||||
},
|
||||
'coffee/src/main': {
|
||||
'cms/js/main': {
|
||||
deps: ['coffee/src/ajax_prefix']
|
||||
},
|
||||
'coffee/src/ajax_prefix': {
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
require ["jquery", "backbone", "coffee/src/main", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "jquery.cookie"],
|
||||
($, Backbone, main, AjaxHelpers) ->
|
||||
describe "CMS", ->
|
||||
it "should initialize URL", ->
|
||||
expect(window.CMS.URL).toBeDefined()
|
||||
|
||||
describe "main helper", ->
|
||||
beforeEach ->
|
||||
@previousAjaxSettings = $.extend(true, {}, $.ajaxSettings)
|
||||
spyOn($, "cookie").and.callFake(
|
||||
(param) ->
|
||||
if param == "csrftoken"
|
||||
return "stubCSRFToken"
|
||||
)
|
||||
|
||||
main()
|
||||
|
||||
afterEach ->
|
||||
$.ajaxSettings = @previousAjaxSettings
|
||||
|
||||
it "turn on Backbone emulateHTTP", ->
|
||||
expect(Backbone.emulateHTTP).toBeTruthy()
|
||||
|
||||
it "setup AJAX CSRF token", ->
|
||||
expect($.ajaxSettings.headers["X-CSRFToken"]).toEqual("stubCSRFToken")
|
||||
|
||||
describe "AJAX Errors", ->
|
||||
server = null
|
||||
beforeEach ->
|
||||
appendSetFixtures(sandbox({id: "page-notification"}))
|
||||
|
||||
afterEach ->
|
||||
server && server.restore()
|
||||
|
||||
it "successful AJAX request does not pop an error notification", ->
|
||||
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", ->
|
||||
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", ->
|
||||
server = AjaxHelpers.server([500, {"Content-Type": "application/json"}, "{}"])
|
||||
|
||||
$.ajax
|
||||
url: "/test"
|
||||
notifyOnError: false
|
||||
|
||||
server.respond()
|
||||
expect($("#page-notification")).toBeEmpty()
|
||||
@@ -1,4 +1,5 @@
|
||||
define ["backbone", "js/models/textbook", "js/collections/textbook", "js/models/chapter", "js/collections/chapter", "coffee/src/main"],
|
||||
|
||||
define ["backbone", "js/models/textbook", "js/collections/textbook", "js/models/chapter", "js/collections/chapter", "cms/js/main"],
|
||||
(Backbone, Textbook, TextbookSet, Chapter, ChapterSet, main) ->
|
||||
|
||||
describe "Textbook model", ->
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
define ["js/models/metadata", "js/collections/metadata", "js/views/metadata", "coffee/src/main"],
|
||||
define ["js/models/metadata", "js/collections/metadata", "js/views/metadata", "cms/js/main"],
|
||||
(MetadataModel, MetadataCollection, MetadataView, main) ->
|
||||
verifyInputType = (input, expectedType) ->
|
||||
# Some browsers (e.g. FireFox) do not support the "number"
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
define ["domReady", "jquery", "underscore.string", "backbone", "gettext",
|
||||
"common/js/components/views/feedback_notification",
|
||||
"coffee/src/ajax_prefix", "jquery.cookie"],
|
||||
(domReady, $, str, Backbone, gettext, NotificationView) ->
|
||||
main = ->
|
||||
AjaxPrefix.addAjaxPrefix jQuery, ->
|
||||
$("meta[name='path_prefix']").attr('content')
|
||||
|
||||
window.CMS = window.CMS or {}
|
||||
CMS.URL = CMS.URL or {}
|
||||
window.onTouchBasedDevice = ->
|
||||
navigator.userAgent.match /iPhone|iPod|iPad|Android/i
|
||||
|
||||
_.extend CMS, Backbone.Events
|
||||
Backbone.emulateHTTP = true
|
||||
|
||||
$.ajaxSetup
|
||||
headers : { 'X-CSRFToken': $.cookie 'csrftoken' }
|
||||
dataType: 'json'
|
||||
|
||||
$(document).ajaxError (event, jqXHR, ajaxSettings, thrownError) ->
|
||||
if ajaxSettings.notifyOnError is 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.")
|
||||
msg = new NotificationView.Error(
|
||||
"title": gettext("Studio's having trouble saving your work")
|
||||
"message": message
|
||||
)
|
||||
msg.show()
|
||||
|
||||
$.postJSON = (url, data, callback) ->
|
||||
# shift arguments if data argument was omitted
|
||||
if $.isFunction(data)
|
||||
callback = data
|
||||
data = `undefined`
|
||||
$.ajax
|
||||
url: url
|
||||
type: "POST"
|
||||
contentType: "application/json; charset=utf-8"
|
||||
dataType: "json"
|
||||
data: JSON.stringify(data)
|
||||
success: callback
|
||||
|
||||
domReady ->
|
||||
if onTouchBasedDevice()
|
||||
$('body').addClass 'touch-based-device'
|
||||
|
||||
main()
|
||||
return main
|
||||
@@ -6,7 +6,7 @@ define([
|
||||
'backbone-relational',
|
||||
'backbone.associations',
|
||||
'gettext',
|
||||
'coffee/src/main',
|
||||
'cms/js/main',
|
||||
'js/certificates/models/signatory',
|
||||
'js/certificates/collections/signatories'
|
||||
],
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
define(['js/base', 'coffee/src/main', 'js/src/logger', 'datepair', 'accessibility',
|
||||
define(['js/base', 'cms/js/main', 'js/src/logger', 'datepair', 'accessibility',
|
||||
'ieshim', 'tooltip_manager', 'lang_edx', 'js/models/course']);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
define([
|
||||
'jquery', 'underscore', 'js/models/xblock_container_info', 'js/views/pages/container',
|
||||
'js/collections/component_template', 'xmodule', 'coffee/src/main',
|
||||
'js/collections/component_template', 'xmodule', 'cms/js/main',
|
||||
'xblock/cms.runtime.v1'
|
||||
],
|
||||
function($, _, XBlockContainerInfo, ContainerPage, ComponentTemplates, xmoduleLoader) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
define([
|
||||
'js/models/explicit_url', 'js/views/tabs', 'xmodule', 'coffee/src/main', 'xblock/cms.runtime.v1'
|
||||
'js/models/explicit_url', 'js/views/tabs', 'xmodule', 'cms/js/main', 'xblock/cms.runtime.v1'
|
||||
], function (TabsModel, TabsEditView, xmoduleLoader) {
|
||||
'use strict';
|
||||
return function (courseLocation, explicitUrl) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
define([
|
||||
'jquery', 'underscore', 'js/models/xblock_info', 'js/views/pages/paged_container',
|
||||
'js/views/library_container', 'js/collections/component_template', 'xmodule', 'coffee/src/main',
|
||||
'js/views/library_container', 'js/collections/component_template', 'xmodule', 'cms/js/main',
|
||||
'xblock/cms.runtime.v1'
|
||||
],
|
||||
function($, _, XBlockInfo, PagedContainerPage, LibraryContainerView, ComponentTemplates, xmoduleLoader) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
define([
|
||||
'backbone', 'underscore', 'gettext', 'js/models/group', 'js/collections/group',
|
||||
'backbone.associations', 'coffee/src/main'
|
||||
'backbone.associations', 'cms/js/main'
|
||||
],
|
||||
function(Backbone, _, gettext, GroupModel, GroupCollection) {
|
||||
'use strict';
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
define(["backbone", "underscore", "gettext", "js/models/chapter", "js/collections/chapter",
|
||||
"backbone.associations", "coffee/src/main"],
|
||||
"backbone.associations", "cms/js/main"],
|
||||
function(Backbone, _, gettext, ChapterModel, ChapterCollection) {
|
||||
|
||||
var Textbook = Backbone.AssociatedModel.extend({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
define([
|
||||
'backbone', 'coffee/src/main', 'js/models/group_configuration',
|
||||
'backbone', 'cms/js/main', 'js/models/group_configuration',
|
||||
'js/models/group', 'js/collections/group', 'squire'
|
||||
], function (Backbone, main, GroupConfigurationModel, GroupModel, GroupCollection, Squire) {
|
||||
'use strict';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
define([ "jquery", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "js/spec_helpers/edit_helpers",
|
||||
"js/views/container", "js/models/xblock_info", "jquery.simulate",
|
||||
"xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
|
||||
"xmodule", "cms/js/main", "xblock/cms.runtime.v1"],
|
||||
function ($, AjaxHelpers, EditHelpers, ContainerView, XBlockInfo) {
|
||||
|
||||
describe("Container View", function () {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
define(["jquery", "URI", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers", "common/js/components/utils/view_utils",
|
||||
"js/views/xblock", "js/models/xblock_info", "xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
|
||||
"js/views/xblock", "js/models/xblock_info", "xmodule", "cms/js/main", "xblock/cms.runtime.v1"],
|
||||
function ($, URI, AjaxHelpers, ViewUtils, XBlockView, XBlockInfo) {
|
||||
"use strict";
|
||||
describe("XBlockView", function() {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
define(["jquery", "underscore", "edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers",
|
||||
"common/js/spec_helpers/template_helpers", "js/spec_helpers/modal_helpers", "js/views/modals/edit_xblock",
|
||||
"js/collections/component_template", "xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
|
||||
"js/collections/component_template", "xmodule", "cms/js/main", "xblock/cms.runtime.v1"],
|
||||
function($, _, AjaxHelpers, TemplateHelpers, modal_helpers, EditXBlockModal, ComponentTemplates) {
|
||||
|
||||
var installMockXBlock, uninstallMockXBlock, installMockXModule, uninstallMockXModule,
|
||||
|
||||
@@ -22,6 +22,7 @@ var options = {
|
||||
// Otherwise Istanbul which is used for coverage tracking will cause tests to not run.
|
||||
sourceFiles: [
|
||||
{pattern: 'coffee/src/**/!(*spec).js'},
|
||||
{pattern: 'cms/js/**/!(*spec|djangojs).js'},
|
||||
{pattern: 'js/**/!(*spec|djangojs).js'}
|
||||
],
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ REQUIREJS_WAIT = {
|
||||
# Unit page
|
||||
re.compile(r'^Unit \|'): [
|
||||
"jquery", "js/base", "js/models/xblock_info", "js/views/pages/container",
|
||||
"js/collections/component_template", "xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
|
||||
"js/collections/component_template", "xmodule", "cms/js/main", "xblock/cms.runtime.v1"],
|
||||
|
||||
# Content - Outline
|
||||
# Note that calling your org, course number, or display name, 'course' will mess this up
|
||||
@@ -49,18 +49,18 @@ REQUIREJS_WAIT = {
|
||||
# Dashboard
|
||||
re.compile(r'^Studio Home \|'): [
|
||||
"js/sock", "gettext", "js/base",
|
||||
"jquery.ui", "coffee/src/main", "underscore"],
|
||||
"jquery.ui", "cms/js/main", "underscore"],
|
||||
|
||||
# Upload
|
||||
re.compile(r'^\s*Files & Uploads'): [
|
||||
'js/base', 'jquery.ui', 'coffee/src/main', 'underscore',
|
||||
'js/base', 'jquery.ui', 'cms/js/main', 'underscore',
|
||||
'js/views/assets', 'js/views/asset'
|
||||
],
|
||||
|
||||
# Pages
|
||||
re.compile(r'^Pages \|'): [
|
||||
'js/models/explicit_url', 'js/views/tabs',
|
||||
'xmodule', 'coffee/src/main', 'xblock/cms.runtime.v1'
|
||||
'xmodule', 'cms/js/main', 'xblock/cms.runtime.v1'
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user