From 0c368011da23808218a4ad4c3ca600e9cfa95eba Mon Sep 17 00:00:00 2001 From: David Baumgold Date: Tue, 14 May 2013 12:41:49 -0400 Subject: [PATCH] Move underscore templates into separate files, included in Mako templates --- cms/static/js/models/section.js | 42 +++++ cms/static/js/views/section.js | 78 +++++++++ cms/templates/base.html | 63 +------ cms/templates/js/section-name-edit.underscore | 5 + cms/templates/js/system-feedback.underscore | 51 ++++++ cms/templates/overview.html | 157 ++---------------- .../templates/static_content.html | 5 + 7 files changed, 202 insertions(+), 199 deletions(-) create mode 100644 cms/static/js/models/section.js create mode 100644 cms/static/js/views/section.js create mode 100644 cms/templates/js/section-name-edit.underscore create mode 100644 cms/templates/js/system-feedback.underscore diff --git a/cms/static/js/models/section.js b/cms/static/js/models/section.js new file mode 100644 index 0000000000..6dcccf7f90 --- /dev/null +++ b/cms/static/js/models/section.js @@ -0,0 +1,42 @@ +CMS.Models.Section = Backbone.Model.extend({ + defaults: { + "name": "" + }, + validate: function(attrs, options) { + if (!attrs.name) { + return "You must specify a name"; + } + }, + url: "/save_item", + toJSON: function() { + return { + id: this.get("id"), + metadata: { + display_name: this.get("name") + } + }; + }, + initialize: function() { + this.listenTo(this, "request", this.showNotification); + this.listenTo(this, "sync", this.hideNotification); + this.listenTo(this, "error", this.hideNotification); + }, + showNotification: function() { + if(!this.msg) { + this.msg = new CMS.Models.SystemFeedback({ + type: "saving", + title: "Saving…", + icon: true, + status: true + }); + } + if(!this.msgView) { + this.msgView = new CMS.Views.Notification({model: this.msg}); + } + this.msg.show(); + }, + hideNotification: function() { + if(!this.msg) { return; } + this.msg.hide(); + } +}); diff --git a/cms/static/js/views/section.js b/cms/static/js/views/section.js new file mode 100644 index 0000000000..0bd48d618b --- /dev/null +++ b/cms/static/js/views/section.js @@ -0,0 +1,78 @@ +CMS.Views.SectionShow = Backbone.View.extend({ + template: _.template('<%= name %>'), + render: function() { + this.$el.html(this.template(this.model.attributes)); + this.delegateEvents(); + return this; + }, + events: { + "click": "switchToEditView" + }, + switchToEditView: function() { + if(!this.editView) { + this.editView = new CMS.Views.SectionEdit({ + model: this.model, el: this.el, showView: this}); + } + this.undelegateEvents(); + this.editView.render(); + } +}); + +CMS.Views.SectionEdit = Backbone.View.extend({ + template: _.template($("#section-name-edit-tpl").text()), + render: function() { + this.$el.html(this.template(this.model.attributes)); + this.delegateEvents(); + return this; + }, + initialize: function() { + this.listenTo(this.model, "invalid", this.showErrorMessage); + this.render(); + }, + events: { + "click .save-button": "saveName", + "submit": "saveName", + "click .cancel-button": "switchToShowView" + }, + saveName: function(e) { + if (e) { e.preventDefault(); } + var name = this.$("input[type=text]").val(); + var that = this; + this.model.save("name", name, { + success: function() { + analytics.track('Edited Section Name', { + 'course': course_location_analytics, + 'display_name': that.model.get('name'), + 'id': that.model.get('id') + }); + that.switchToShowView(); + } + }); + }, + switchToShowView: function() { + if(!this.showView) { + this.showView = new CMS.Views.SectionShow({ + model: this.model, el: this.el, editView: this}); + } + this.undelegateEvents(); + this.stopListening(); + this.showView.render(); + }, + showErrorMessage: function(model, error, options) { + var msg = new CMS.Models.ErrorMessage({ + title: "Validation Error", + message: error, + close: false, + icon: false, + actions: { + primary: { + text: "Dismiss", + click: function() { + this.hide(); + } + } + } + }); + new CMS.Views.Prompt({model: msg}); + } +}); diff --git a/cms/templates/base.html b/cms/templates/base.html index d6de3f59ae..d3dbdcd68c 100644 --- a/cms/templates/base.html +++ b/cms/templates/base.html @@ -30,6 +30,13 @@ <%include file="courseware_vendor_js.html"/> + + ## js templates + + + ## javascript @@ -51,62 +58,6 @@ - - <%text> - - + + + + @@ -128,149 +142,6 @@ -<%text> - - - -
diff --git a/common/djangoapps/pipeline_mako/templates/static_content.html b/common/djangoapps/pipeline_mako/templates/static_content.html index 302d4d7aa5..2a1308923a 100644 --- a/common/djangoapps/pipeline_mako/templates/static_content.html +++ b/common/djangoapps/pipeline_mako/templates/static_content.html @@ -28,3 +28,8 @@ except: % endfor %endif + +<%def name="include(path)"><% +from django.template.loaders.filesystem import _loader +source, template_path = _loader.load_template_source(path) +%>${source}