From e5dc46846ce9fb1c990d291ef852c0cad3f74957 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Mon, 9 Jun 2014 11:26:04 -0400 Subject: editing profile --- public/assets/javascripts/ui/EditProfileModal.js | 34 ++++++++++- public/assets/javascripts/ui/MasterView.js | 77 ------------------------ public/assets/javascripts/ui/ModalFormView.js | 70 --------------------- public/assets/javascripts/ui/ModalView.js | 28 --------- public/assets/javascripts/ui/Router.js | 77 ++++++++++++++++++++++++ public/assets/javascripts/ui/SignUpModal.js | 2 - 6 files changed, 110 insertions(+), 178 deletions(-) delete mode 100644 public/assets/javascripts/ui/MasterView.js delete mode 100644 public/assets/javascripts/ui/ModalFormView.js delete mode 100644 public/assets/javascripts/ui/ModalView.js create mode 100644 public/assets/javascripts/ui/Router.js (limited to 'public/assets/javascripts/ui') diff --git a/public/assets/javascripts/ui/EditProfileModal.js b/public/assets/javascripts/ui/EditProfileModal.js index 5b79a31..6b89ad8 100644 --- a/public/assets/javascripts/ui/EditProfileModal.js +++ b/public/assets/javascripts/ui/EditProfileModal.js @@ -2,6 +2,7 @@ var EditProfileModal = ModalFormView.extend({ el: ".mediaDrawer.editProfile", action: "/api/profile", + method: "put", load: function(){ this.reset() @@ -12,13 +13,44 @@ var EditProfileModal = ModalFormView.extend({ this.$("[name='" + i + "']").val(data[i]) } + this.$("#profile_username").html(data.username) + + if (data.photo && data.photo.length) { + this.$("#load_avatar").attr("src", data.photo) + } + else { + this.$("#load_avatar").hide() + } + this.show() }, this)) }, + + validate: function(){ + var errors = [] + + var email = this.$("#profile_email").val() + var pw0 = this.$("#profile_old_password").val() + var pw1 = this.$("#profile_new_password").val() + var pw2 = this.$("#profile_new_password2").val() + + if (pw1.length) { + if (! pw0.length) { + errors.push("Please enter your old password.") + } + if (pw1 !== pw2) { + errors.push("New passwords don't match"); + } + } + if (email.length && email.indexOf("@") === -1) { + errors.push("Please enter a valid email address"); + } + + return errors + }, success: function(){ window.location.href = "/profile" } }) - diff --git a/public/assets/javascripts/ui/MasterView.js b/public/assets/javascripts/ui/MasterView.js deleted file mode 100644 index 5aca4e0..0000000 --- a/public/assets/javascripts/ui/MasterView.js +++ /dev/null @@ -1,77 +0,0 @@ - -var MasterView = View.extend({ - el: "body", - - events: { - "click [data-role='show-signup-modal']": 'signup', - "click [data-role='show-signin-modal']": 'signin', - "click [data-role='new-project-modal']": 'newProject', - "click [data-role='edit-project-modal']": 'editProject', - "click [data-role='edit-profile-modal']": 'editProfile', - }, - - routes: { - "/login": 'signin', - "/signup": 'signup', - "/project/new": 'newProject', - "/profile/edit": 'editProfile', - "/app": 'launch', - }, - - initialize: function(){ - this.signUpModal = new SignUpModal() - this.signInModal = new SignInModal() - this.newProjectModal = new NewProjectModal() - this.editProjectModal = new EditProjectModal() - this.editProfileModal = new EditProfileModal() - - this.originalPath = window.location.pathname - - for (var route in this.routes) { - if (window.location.pathname.indexOf(route) === 0) { - this[this.routes[route]]() - break; - } - } - - $("body").removeClass("loading") - }, - - launch: function(){ - app.launch() - }, - - signup: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/signup") - this.signUpModal.load() - }, - - signin: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/login") - this.signInModal.load() - }, - - newProject: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/project/new") - - this.newProjectModal.load() - }, - - editProject: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/project/edit") - this.editProjectModal.load() - }, - - editProfile: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/profile/edit") - - this.editProfileModal.load() - }, - -}) - diff --git a/public/assets/javascripts/ui/ModalFormView.js b/public/assets/javascripts/ui/ModalFormView.js deleted file mode 100644 index 608b8c1..0000000 --- a/public/assets/javascripts/ui/ModalFormView.js +++ /dev/null @@ -1,70 +0,0 @@ - -var ModalFormView = ModalView.extend({ - - events: { - "submit form": "submit" - }, - - initialize: function(){ - this.$form = this.$("form") - this.$errors = this.$(".errors") - this.$errorList = this.$(".errorList") - }, - - reset: function(){ - this.$("input").not("[type='submit']").not("[type='hidden']").val("") - }, - - load: function(){ - this.reset() - this.show() - }, - - showErrors: function(errors){ - if (errors && errors.length) { - this.$errors.show(); - for (var i in errors) { - this.$errorList.append('
' + errors[i] + '
'); - } - } - }, - - submit: function(e){ - e.preventDefault() - - this.$errors.hide(); - this.$errorList.empty(); - - if (this.validate) { - var errors = this.validate() - if (errors && errors.length) { - this.showErrors(errors) - return - } - } - - var fields = this.$form.serializeArray() - fields.forEach(function(pair){ - if (pair.name == "password" && pair.value.length > 0) { - pair.value = SHA1.hex('lol$' + pair.value + '$vvalls') - } - }) - - var request = $.post(this.action, $.param(fields)); - request.done($.proxy(function (response) { - if (response.error) { - this.$errors.show(); - var errors = [] - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); - } - this.showErrors(errors) - return - } - else { - this.success && this.success(response) - } - }, this)); - } - -}) diff --git a/public/assets/javascripts/ui/ModalView.js b/public/assets/javascripts/ui/ModalView.js deleted file mode 100644 index 80ce8d0..0000000 --- a/public/assets/javascripts/ui/ModalView.js +++ /dev/null @@ -1,28 +0,0 @@ - -var ModalView = View.extend({ - events: { - "click .close": 'close', - }, - - show: function(){ - $(".mediaDrawer").removeClass("active"); - this.$el.addClass("active"); - $("body").addClass("noOverflow"); - }, - - hide: function(){ - // $(".mediaDrawer, .room1").removeClass("active editing"); - this.$el.removeClass("active"); - $("body").removeClass("noOverflow"); - }, - - close: function(){ - if (window.isModalView) { - window.location.pathname = "/" - } - else { - history.pushState(null, document.title, app.master.originalPath) - this.hide() - } - } -}) diff --git a/public/assets/javascripts/ui/Router.js b/public/assets/javascripts/ui/Router.js new file mode 100644 index 0000000..a518e27 --- /dev/null +++ b/public/assets/javascripts/ui/Router.js @@ -0,0 +1,77 @@ + +var Router = View.extend({ + el: "body", + + events: { + "click [data-role='show-signup-modal']": 'signup', + "click [data-role='show-signin-modal']": 'signin', + "click [data-role='new-project-modal']": 'newProject', + "click [data-role='edit-project-modal']": 'editProject', + "click [data-role='edit-profile-modal']": 'editProfile', + }, + + routes: { + "/login": 'signin', + "/signup": 'signup', + "/project/new": 'newProject', + "/profile/edit": 'editProfile', + "/app": 'launch', + }, + + initialize: function(){ + this.signUpModal = new SignUpModal() + this.signInModal = new SignInModal() + this.newProjectModal = new NewProjectModal() + this.editProjectModal = new EditProjectModal() + this.editProfileModal = new EditProfileModal() + + this.originalPath = window.location.pathname + + for (var route in this.routes) { + if (window.location.pathname.indexOf(route) === 0) { + this[this.routes[route]]() + break; + } + } + + $("body").removeClass("loading") + }, + + launch: function(){ + app.launch() + }, + + signup: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/signup") + this.signUpModal.load() + }, + + signin: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/login") + this.signInModal.load() + }, + + newProject: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/project/new") + + this.newProjectModal.load() + }, + + editProject: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/project/edit") + this.editProjectModal.load() + }, + + editProfile: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/profile/edit") + + this.editProfileModal.load() + }, + +}) + diff --git a/public/assets/javascripts/ui/SignUpModal.js b/public/assets/javascripts/ui/SignUpModal.js index 95b5837..5c651ee 100644 --- a/public/assets/javascripts/ui/SignUpModal.js +++ b/public/assets/javascripts/ui/SignUpModal.js @@ -1,5 +1,3 @@ - - var SignUpModal = ModalFormView.extend({ el: ".mediaDrawer.signup", action: "/auth/signup", -- cgit v1.2.3-70-g09d2