From 9fb0fe9b7ef614d2248b00ea2b964205f3453f41 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 10 Jun 2014 12:00:24 -0400 Subject: split up builder functionality --- public/assets/javascripts/ui/AlertModal.js | 25 ---- public/assets/javascripts/ui/BuilderView.js | 23 ---- public/assets/javascripts/ui/ConfirmModal.js | 25 ---- public/assets/javascripts/ui/DocumentModal.js | 49 ------- public/assets/javascripts/ui/EditProfileModal.js | 56 -------- public/assets/javascripts/ui/EditProjectModal.js | 49 ------- public/assets/javascripts/ui/EditorView.js | 13 -- public/assets/javascripts/ui/NewProjectModal.js | 17 --- public/assets/javascripts/ui/Router.js | 151 --------------------- public/assets/javascripts/ui/SignInModal.js | 12 -- public/assets/javascripts/ui/SignUpModal.js | 37 ----- public/assets/javascripts/ui/SiteRouter.js | 133 ++++++++++++++++++ .../javascripts/ui/builder/BuilderSettings.js | 18 +++ .../javascripts/ui/builder/BuilderToolbar.js | 42 ++++++ .../assets/javascripts/ui/builder/BuilderView.js | 17 +++ public/assets/javascripts/ui/editor/EditorView.js | 13 ++ public/assets/javascripts/ui/lib/AlertModal.js | 25 ++++ public/assets/javascripts/ui/lib/ConfirmModal.js | 25 ++++ public/assets/javascripts/ui/lib/ModalFormView.js | 96 +++++++++++++ public/assets/javascripts/ui/lib/ModalView.js | 28 ++++ public/assets/javascripts/ui/lib/Router.js | 28 ++++ public/assets/javascripts/ui/lib/view.js | 130 ++++++++++++++++++ public/assets/javascripts/ui/site/DocumentModal.js | 49 +++++++ .../assets/javascripts/ui/site/EditProfileModal.js | 56 ++++++++ .../assets/javascripts/ui/site/EditProjectModal.js | 49 +++++++ .../assets/javascripts/ui/site/NewProjectModal.js | 17 +++ public/assets/javascripts/ui/site/SignInModal.js | 12 ++ public/assets/javascripts/ui/site/SignUpModal.js | 37 +++++ 28 files changed, 775 insertions(+), 457 deletions(-) delete mode 100644 public/assets/javascripts/ui/AlertModal.js delete mode 100644 public/assets/javascripts/ui/BuilderView.js delete mode 100644 public/assets/javascripts/ui/ConfirmModal.js delete mode 100644 public/assets/javascripts/ui/DocumentModal.js delete mode 100644 public/assets/javascripts/ui/EditProfileModal.js delete mode 100644 public/assets/javascripts/ui/EditProjectModal.js delete mode 100644 public/assets/javascripts/ui/EditorView.js delete mode 100644 public/assets/javascripts/ui/NewProjectModal.js delete mode 100644 public/assets/javascripts/ui/Router.js delete mode 100644 public/assets/javascripts/ui/SignInModal.js delete mode 100644 public/assets/javascripts/ui/SignUpModal.js create mode 100644 public/assets/javascripts/ui/SiteRouter.js create mode 100644 public/assets/javascripts/ui/builder/BuilderSettings.js create mode 100644 public/assets/javascripts/ui/builder/BuilderToolbar.js create mode 100644 public/assets/javascripts/ui/builder/BuilderView.js create mode 100644 public/assets/javascripts/ui/editor/EditorView.js create mode 100644 public/assets/javascripts/ui/lib/AlertModal.js create mode 100644 public/assets/javascripts/ui/lib/ConfirmModal.js create mode 100644 public/assets/javascripts/ui/lib/ModalFormView.js create mode 100644 public/assets/javascripts/ui/lib/ModalView.js create mode 100644 public/assets/javascripts/ui/lib/Router.js create mode 100644 public/assets/javascripts/ui/lib/view.js create mode 100644 public/assets/javascripts/ui/site/DocumentModal.js create mode 100644 public/assets/javascripts/ui/site/EditProfileModal.js create mode 100644 public/assets/javascripts/ui/site/EditProjectModal.js create mode 100644 public/assets/javascripts/ui/site/NewProjectModal.js create mode 100644 public/assets/javascripts/ui/site/SignInModal.js create mode 100644 public/assets/javascripts/ui/site/SignUpModal.js (limited to 'public/assets/javascripts/ui') diff --git a/public/assets/javascripts/ui/AlertModal.js b/public/assets/javascripts/ui/AlertModal.js deleted file mode 100644 index c5693ad..0000000 --- a/public/assets/javascripts/ui/AlertModal.js +++ /dev/null @@ -1,25 +0,0 @@ - - -var AlertModal = ModalFormView.extend({ - el: ".mediaDrawer.alert", - - events: { - "click .ok": "advance", - "click .close": "advance", - }, - - alert: function(message, callback){ - this.$(".message").html(message) - this.callback = callback - this.show() - }, - - advance: function(e){ - e && e.preventDefault() - this.hide() - this.callback && this.callback() - this.callback = null - } - -}) - diff --git a/public/assets/javascripts/ui/BuilderView.js b/public/assets/javascripts/ui/BuilderView.js deleted file mode 100644 index c91d9ee..0000000 --- a/public/assets/javascripts/ui/BuilderView.js +++ /dev/null @@ -1,23 +0,0 @@ - -var BuilderView = View.extend({ - el: "#builderView", - - events: { - "click [data-role='toggle-map-view']": 'toggleMap', - "click [data-role='toggle-layout-settings']": 'toggleSettings', - }, - - load: function(){ - $("#map").show() - }, - - toggleMap: function(){ - map.toggle() - }, - - toggleSettings: function(){ - this.$(".settings").toggleClass("active") - }, - -}) - diff --git a/public/assets/javascripts/ui/ConfirmModal.js b/public/assets/javascripts/ui/ConfirmModal.js deleted file mode 100644 index 868ce8e..0000000 --- a/public/assets/javascripts/ui/ConfirmModal.js +++ /dev/null @@ -1,25 +0,0 @@ - - -var ConfirmModal = ModalFormView.extend({ - el: ".mediaDrawer.confirm", - - events: { - "click .yes": "advance", - "click .no": "hide", - }, - - confirm: function(question, callback){ - this.$(".question").html(question) - this.callback = callback - this.show() - }, - - advance: function(e){ - e && e.preventDefault() - this.hide() - this.callback && this.callback() - this.callback = null - } - -}) - diff --git a/public/assets/javascripts/ui/DocumentModal.js b/public/assets/javascripts/ui/DocumentModal.js deleted file mode 100644 index 6f16169..0000000 --- a/public/assets/javascripts/ui/DocumentModal.js +++ /dev/null @@ -1,49 +0,0 @@ - -var DocumentModal = ModalFormView.extend({ - el: ".mediaDrawer.editDocument", - createAction: "/api/docs/new", - updateAction: "/api/docs/edit", - destroyAction: "/api/docs/destroy", - - load: function(name, isNew){ - this.reset() - - if (isNew || name === "new") { - name = sanitize(name) - if (name !== "new") { - this.$("[name='new_name']").val( name.replace(/\s+/g,"-") ) - this.$("[name='displayName']").val( capitalize(name.replace(/-/g," ")) ) - } - this.action = this.createAction - return this.show() - } - - this.action = this.updateAction - - $.get("/api/docs", { name: name }, $.proxy(function(data){ - if (data.isNew) { - this.action = this.createAction - } - - for (var i in data) { - this.$("[name='" + i + "']").val(data[i]) - } - this.$("[name='new_name']").val(name) - - this.show() - }, this)) - }, - - success: function(res){ - window.location.pathname = "/about/" + res.name - }, - - destroy: function(name, cb){ - $.ajax({ - type: "delete", - url: this.destroyAction, - data: { name: name, _csrf: $("[name=_csrf]").val() } - }).complete(cb || function(){}) - }, - -}) diff --git a/public/assets/javascripts/ui/EditProfileModal.js b/public/assets/javascripts/ui/EditProfileModal.js deleted file mode 100644 index 6b89ad8..0000000 --- a/public/assets/javascripts/ui/EditProfileModal.js +++ /dev/null @@ -1,56 +0,0 @@ - -var EditProfileModal = ModalFormView.extend({ - el: ".mediaDrawer.editProfile", - action: "/api/profile", - method: "put", - - load: function(){ - this.reset() - $.get("/api/profile", $.proxy(function(data){ - console.log(data) - - for (var i in data) { - 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/EditProjectModal.js b/public/assets/javascripts/ui/EditProjectModal.js deleted file mode 100644 index 356d8b7..0000000 --- a/public/assets/javascripts/ui/EditProjectModal.js +++ /dev/null @@ -1,49 +0,0 @@ - -var EditProjectModal = ModalView.extend({ - el: ".mediaDrawer.editProject", - action: "/project/edit", - - 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() - }, - - submit: function(e){ - e.preventDefault() - - this.$errors.hide(); - this.$errorList.empty() - - var fields = this.$form.serializeArray() - - var request = $.post(this.action, $.param(fields)); - request.done($.proxy(function (response) { - if (response.error) { - this.$errors.show(); - for (var key in response.error.errors) { - this.$errorList.append('
' + response.error.errors[key].message + '
'); - } - return - } - else { - window.location.href = "/profile" - } - }, this)); - } - -}) - diff --git a/public/assets/javascripts/ui/EditorView.js b/public/assets/javascripts/ui/EditorView.js deleted file mode 100644 index 91329de..0000000 --- a/public/assets/javascripts/ui/EditorView.js +++ /dev/null @@ -1,13 +0,0 @@ - -var EditorView = View.extend({ -// el: "#editorControls", - - events: { - }, - - load: function(){ - $("#map").hide() - } - -}) - diff --git a/public/assets/javascripts/ui/NewProjectModal.js b/public/assets/javascripts/ui/NewProjectModal.js deleted file mode 100644 index cf2044f..0000000 --- a/public/assets/javascripts/ui/NewProjectModal.js +++ /dev/null @@ -1,17 +0,0 @@ - - -var NewProjectModal = ModalFormView.extend({ - el: ".mediaDrawer.newProject", - action: "/project/new", - - load: function(){ - this.reset() - this.show() - }, - - success: function(){ - // - } - -}) - diff --git a/public/assets/javascripts/ui/Router.js b/public/assets/javascripts/ui/Router.js deleted file mode 100644 index 5b75162..0000000 --- a/public/assets/javascripts/ui/Router.js +++ /dev/null @@ -1,151 +0,0 @@ - -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', - "click [data-role='new-document-modal']": 'newDocument', - "click [data-role='edit-document-modal']": 'editDocument', - "click [data-role='delete-document-modal']": 'destroyDocument', - }, - - routes: { - "/login": 'signin', - "/signup": 'signup', - "/project/new": 'newProject', - "/profile": 'profile', - "/profile/edit": 'editProfile', - "/about/:name/edit": 'editDocument', - "/about/new": 'newDocument', - "/editor": 'launchEditor', - "/builder": 'launchBuilder', - }, - - initialize: function(){ - this.builderView = new BuilderView() - this.editorView = new EditorView() - this.signUpModal = new SignUpModal() - this.signInModal = new SignInModal() - this.newProjectModal = new NewProjectModal() - this.editProjectModal = new EditProjectModal() - this.editProfileModal = new EditProfileModal() - this.documentModal = new DocumentModal() - this.confirmModal = new ConfirmModal() - this.alertModal = new AlertModal() - - this.originalPath = window.location.pathname - - var path = window.location.pathname.split("/") - // console.log(path) - for (var route in this.routes) { - var routePath = route.split("/") - if (routePath[1] == path[1]) { - if (routePath[2] && routePath[2].indexOf(":") !== -1 && path[2] && (path[3] === routePath[3]) ) { - this[this.routes[route]](null, path[2]) - break; - } - else if (routePath[2] == path[2]) { - this[this.routes[route]](null) - break; - } - else if (! routePath[2] && (! path[2].length || ! path[2])) { - this[this.routes[route]](null) - break; - } - } - } - - $("body").removeClass("loading") - }, - - launchBuilder: function(){ - app.mode.builder = true - app.launch() - this.builderView.load() - }, - - launchEditor: function(){ - app.mode.editor = true - app.launch() - this.editorView.load() - }, - - 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() - }, - - profile: function(e){ - var classes = ['one', 'two', 'three', 'four', - 'five', 'six', 'seven', 'eight', - 'nine', 'ten', 'eleven', 'twelve', - 'thirteen']; - $(".bio").addClass(choice(classes)); - }, - - editProfile: function(e){ - e && e.preventDefault() - window.history.pushState(null, document.title, "/profile/edit") - - this.editProfileModal.load() - }, - - newDocument: function(e){ - e && e.preventDefault() - - var name = e ? $(e.currentTarget).data("name") : "new" - - window.history.pushState(null, document.title, "/about/new") - this.documentModal.load(name, true) - }, - - editDocument: function(e, name){ - e && e.preventDefault() - - var name = e ? $(e.currentTarget).data("name") : name - window.history.pushState(null, document.title, "/about/" + name + "/edit") - this.documentModal.load(name, false) - }, - - destroyDocument: function(e, name){ - e && e.preventDefault() - - var name = e ? $(e.currentTarget).data("name") : name - - this.confirmModal.confirm("Are you sure you want to delete " + name + "?", $.proxy(function(){ - this.documentModal.destroy(name, $.proxy(function(){ - this.alertModal.alert("Document deleted!", $.proxy(function(){ - window.location.href = "/about" - }, this)) - }, this)) - }, this)) - - // this.documentModal.destroy(name) - }, - -}) - diff --git a/public/assets/javascripts/ui/SignInModal.js b/public/assets/javascripts/ui/SignInModal.js deleted file mode 100644 index 4c91b54..0000000 --- a/public/assets/javascripts/ui/SignInModal.js +++ /dev/null @@ -1,12 +0,0 @@ - - -var SignInModal = ModalFormView.extend({ - el: ".mediaDrawer.signin", - action: "/auth/signin", - - success: function(res){ - window.location.href = "/profile" - } - -}) - diff --git a/public/assets/javascripts/ui/SignUpModal.js b/public/assets/javascripts/ui/SignUpModal.js deleted file mode 100644 index 5c651ee..0000000 --- a/public/assets/javascripts/ui/SignUpModal.js +++ /dev/null @@ -1,37 +0,0 @@ -var SignUpModal = ModalFormView.extend({ - el: ".mediaDrawer.signup", - action: "/auth/signup", - - validate: function(){ - var errors = [] - - var username = this.$("#usernameInput").val() - var email = this.$("#emailInput").val() - var pw1 = this.$("#passwordInput1").val() - var pw2 = this.$("#passwordInput2").val() - - if (! username.length) { - errors.push("Please enter a username"); - } - if (! pw1.length) { - errors.push("Please enter a password"); - } - if (! email.length) { - errors.push("Please enter an email address"); - } - else if (email.indexOf("@") === -1) { - errors.push("Please enter a valid email address"); - } - if (pw1 !== pw2) { - errors.push("Passwords don't match"); - } - - return errors - }, - - success: function(res){ - window.location.href = "/profile" - } - -}) - diff --git a/public/assets/javascripts/ui/SiteRouter.js b/public/assets/javascripts/ui/SiteRouter.js new file mode 100644 index 0000000..c02b19c --- /dev/null +++ b/public/assets/javascripts/ui/SiteRouter.js @@ -0,0 +1,133 @@ + +var SiteRouter = Router.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', + "click [data-role='new-document-modal']": 'newDocument', + "click [data-role='edit-document-modal']": 'editDocument', + "click [data-role='delete-document-modal']": 'destroyDocument', + }, + + routes: { + "/login": 'signin', + "/signup": 'signup', + "/project/new": 'newProject', + "/profile": 'profile', + "/profile/edit": 'editProfile', + "/about/:name/edit": 'editDocument', + "/about/new": 'newDocument', + "/editor": 'launchEditor', + "/builder": 'launchBuilder', + }, + + initialize: function(){ + this.signUpModal = new SignUpModal() + this.signInModal = new SignInModal() + this.newProjectModal = new NewProjectModal() + this.editProjectModal = new EditProjectModal() + this.editProfileModal = new EditProfileModal() + this.documentModal = new DocumentModal() + this.confirmModal = new ConfirmModal() + this.alertModal = new AlertModal() + + this.route() + + $("body").removeClass("loading") + }, + + launchBuilder: function(){ + app.mode.builder = true + app.launch() + + this.builderView = new BuilderView() + this.builderView.load() + }, + + launchEditor: function(){ + app.mode.editor = true + app.launch() + + this.editorView = new EditorView() + this.editorView.load() + }, + + 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() + }, + + profile: function(e){ + var classes = ['one', 'two', 'three', 'four', + 'five', 'six', 'seven', 'eight', + 'nine', 'ten', 'eleven', 'twelve', + 'thirteen']; + $(".bio").addClass(choice(classes)); + }, + + editProfile: function(e){ + e && e.preventDefault() + window.history.pushState(null, document.title, "/profile/edit") + + this.editProfileModal.load() + }, + + newDocument: function(e){ + e && e.preventDefault() + + var name = e ? $(e.currentTarget).data("name") : "new" + + window.history.pushState(null, document.title, "/about/new") + this.documentModal.load(name, true) + }, + + editDocument: function(e, name){ + e && e.preventDefault() + + var name = e ? $(e.currentTarget).data("name") : name + window.history.pushState(null, document.title, "/about/" + name + "/edit") + this.documentModal.load(name, false) + }, + + destroyDocument: function(e, name){ + e && e.preventDefault() + + var name = e ? $(e.currentTarget).data("name") : name + + this.confirmModal.confirm("Are you sure you want to delete " + name + "?", $.proxy(function(){ + this.documentModal.destroy(name, $.proxy(function(){ + this.alertModal.alert("Document deleted!", $.proxy(function(){ + window.location.href = "/about" + }, this)) + }, this)) + }, this)) + + // this.documentModal.destroy(name) + }, + +}) + diff --git a/public/assets/javascripts/ui/builder/BuilderSettings.js b/public/assets/javascripts/ui/builder/BuilderSettings.js new file mode 100644 index 0000000..b598a18 --- /dev/null +++ b/public/assets/javascripts/ui/builder/BuilderSettings.js @@ -0,0 +1,18 @@ + + + + +var BuilderSettings = View.extend({ + el: "#builderSettings", + + events: { + }, + + initialize: function(){ + }, + + toggle: function(){ + this.$el.toggleClass("active") + } + +}) diff --git a/public/assets/javascripts/ui/builder/BuilderToolbar.js b/public/assets/javascripts/ui/builder/BuilderToolbar.js new file mode 100644 index 0000000..42d4306 --- /dev/null +++ b/public/assets/javascripts/ui/builder/BuilderToolbar.js @@ -0,0 +1,42 @@ + +var BuilderToolbar = View.extend({ + el: "#builderToolbar", + + events: { + "click [data-role='toggle-map-view']": 'toggleMap', + "click [data-role='toggle-layout-settings']": 'toggleSettings', + "click [data-role='undo']": 'undo', + "click [data-role='draw']": 'draw', + "click [data-role='resize']": 'resize', + "click [data-role='move']": 'move', + "click [data-role='delete']": 'delete', + }, + + initialize: function(opt){ + this.parent = opt.parent + }, + + toggleMap: function(){ + map.toggle() + }, + + toggleSettings: function(){ + this.parent.settings.toggle() + }, + + undo: function(){ + }, + + draw: function(){ + }, + + resize: function(){ + }, + + move: function(){ + }, + + delete: function(){ + }, + +}) diff --git a/public/assets/javascripts/ui/builder/BuilderView.js b/public/assets/javascripts/ui/builder/BuilderView.js new file mode 100644 index 0000000..d0659bb --- /dev/null +++ b/public/assets/javascripts/ui/builder/BuilderView.js @@ -0,0 +1,17 @@ + +var BuilderView = View.extend({ + el: "#builderView", + + events: { + }, + + initialize: function(){ + this.toolbar = new BuilderToolbar ({ parent: this }) + this.settings = new BuilderSettings ({ parent: this }) + }, + + load: function(){ + $("#map").show() + }, + +}) diff --git a/public/assets/javascripts/ui/editor/EditorView.js b/public/assets/javascripts/ui/editor/EditorView.js new file mode 100644 index 0000000..91329de --- /dev/null +++ b/public/assets/javascripts/ui/editor/EditorView.js @@ -0,0 +1,13 @@ + +var EditorView = View.extend({ +// el: "#editorControls", + + events: { + }, + + load: function(){ + $("#map").hide() + } + +}) + diff --git a/public/assets/javascripts/ui/lib/AlertModal.js b/public/assets/javascripts/ui/lib/AlertModal.js new file mode 100644 index 0000000..c5693ad --- /dev/null +++ b/public/assets/javascripts/ui/lib/AlertModal.js @@ -0,0 +1,25 @@ + + +var AlertModal = ModalFormView.extend({ + el: ".mediaDrawer.alert", + + events: { + "click .ok": "advance", + "click .close": "advance", + }, + + alert: function(message, callback){ + this.$(".message").html(message) + this.callback = callback + this.show() + }, + + advance: function(e){ + e && e.preventDefault() + this.hide() + this.callback && this.callback() + this.callback = null + } + +}) + diff --git a/public/assets/javascripts/ui/lib/ConfirmModal.js b/public/assets/javascripts/ui/lib/ConfirmModal.js new file mode 100644 index 0000000..868ce8e --- /dev/null +++ b/public/assets/javascripts/ui/lib/ConfirmModal.js @@ -0,0 +1,25 @@ + + +var ConfirmModal = ModalFormView.extend({ + el: ".mediaDrawer.confirm", + + events: { + "click .yes": "advance", + "click .no": "hide", + }, + + confirm: function(question, callback){ + this.$(".question").html(question) + this.callback = callback + this.show() + }, + + advance: function(e){ + e && e.preventDefault() + this.hide() + this.callback && this.callback() + this.callback = null + } + +}) + diff --git a/public/assets/javascripts/ui/lib/ModalFormView.js b/public/assets/javascripts/ui/lib/ModalFormView.js new file mode 100644 index 0000000..d084031 --- /dev/null +++ b/public/assets/javascripts/ui/lib/ModalFormView.js @@ -0,0 +1,96 @@ + +var ModalFormView = ModalView.extend({ + + method: "post", + + events: { + "submit form": "submit" + }, + + initialize: function(){ + this.$form = this.$("form") + this.$errors = this.$(".errors") + this.$errorList = this.$(".errorList") + }, + + reset: function(){ + this.$("input,textarea").not("[type='submit']").not("[type='hidden']").val("") + }, + + load: function(){ + this.reset() + this.show() + }, + + showErrors: function(errors){ + if (errors && errors.length) { + this.$errorList.empty(); + for (var i in errors) { + this.$errorList.append('
' + errors[i] + '
'); + } + this.$errors.css("opacity", 1.0); + setTimeout($.proxy(function(){ + this.$errors.show().css("opacity", 1.0); + }, this), 200) + } + }, + + serialize: function(){ + var fd = new FormData() + + this.$("input[name], select[name], textarea[name]").each( function(){ + if (this.type == "file") { + if (this.files.length > 0) { + fd.append(this.name, this.files[0]); + } + } + else if (this.type == "password") { + if (this.value.length > 0) { + fd.append(this.name, SHA1.hex('lol$' + this.value + '$vvalls')) + } + } + else { + fd.append(this.name, this.value); + } + }); + + return fd + }, + + submit: function(e){ + e.preventDefault() + + this.$errors.hide().css("opacity", 0.0); + + if (this.validate) { + var errors = this.validate() + if (errors && errors.length) { + this.showErrors(errors) + return + } + } + + var request = $.ajax({ + url: this.action, + type: this.method, + data: this.serialize(), + dataType: "json", + processData: false, + contentType: false, + }); + request.done($.proxy(function (response) { + if (response.error) { + 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/lib/ModalView.js b/public/assets/javascripts/ui/lib/ModalView.js new file mode 100644 index 0000000..b90b3c4 --- /dev/null +++ b/public/assets/javascripts/ui/lib/ModalView.js @@ -0,0 +1,28 @@ + +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.router.originalPath) + this.hide() + } + } +}) diff --git a/public/assets/javascripts/ui/lib/Router.js b/public/assets/javascripts/ui/lib/Router.js new file mode 100644 index 0000000..d06c07a --- /dev/null +++ b/public/assets/javascripts/ui/lib/Router.js @@ -0,0 +1,28 @@ +var Router = View.extend({ + + route: function(){ + + this.originalPath = window.location.pathname + + var path = window.location.pathname.split("/") + // console.log(path) + for (var route in this.routes) { + var routePath = route.split("/") + if (routePath[1] == path[1]) { + if (routePath[2] && routePath[2].indexOf(":") !== -1 && path[2] && (path[3] === routePath[3]) ) { + this[this.routes[route]](null, path[2]) + break; + } + else if (routePath[2] == path[2]) { + this[this.routes[route]](null) + break; + } + else if (! routePath[2] && (! path[2].length || ! path[2])) { + this[this.routes[route]](null) + break; + } + } + } + } + +}) diff --git a/public/assets/javascripts/ui/lib/view.js b/public/assets/javascripts/ui/lib/view.js new file mode 100644 index 0000000..823a75b --- /dev/null +++ b/public/assets/javascripts/ui/lib/view.js @@ -0,0 +1,130 @@ +var View = (function($, _){ + + var View = function(options) { + this.cid = _.uniqueId('view'); + options || (options = {}); + _.extend(this, _.pick(options, viewOptions)); + this._ensureElement(); + this.initialize.apply(this, arguments); + this.delegateEvents(); + }; + + var delegateEventSplitter = /^(\S+)\s*(.*)$/; + + var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']; + + _.extend(View.prototype, { + + // The default `tagName` of a View's element is `"div"`. + tagName: 'div', + + $: function(selector) { + return this.$el.find(selector); + }, + + initialize: function(){}, + + setElement: function(element, delegate) { + if (this.$el) this.undelegateEvents(); + this.$el = element instanceof $ ? element : $(element); + this.el = this.$el[0]; + if (delegate !== false) this.delegateEvents(); + return this; + }, + + // Set callbacks, where `this.events` is a hash of + // + // *{"event selector": "callback"}* + // + // { + // 'mousedown .title': 'edit', + // 'click .button': 'save', + // 'click .open': function(e) { ... } + // } + // + // pairs. Callbacks will be bound to the view, with `this` set properly. + // Uses event delegation for efficiency. + // Omitting the selector binds the event to `this.el`. + // This only works for delegate-able events: not `focus`, `blur`, and + // not `change`, `submit`, and `reset` in Internet Explorer. + delegateEvents: function(events) { + if (!(events || (events = _.result(this, 'events')))) return this; + this.undelegateEvents(); + for (var key in events) { + var method = events[key]; + if (!_.isFunction(method)) method = this[events[key]]; + if (!method) continue; + + var match = key.match(delegateEventSplitter); + var eventName = match[1], selector = match[2]; + method = _.bind(method, this); + eventName += '.delegateEvents' + this.cid; + if (selector === '') { + this.$el.on(eventName, method); + } else { + this.$el.on(eventName, selector, method); + } + } + return this; + }, + + // Clears all callbacks previously bound to the view with `delegateEvents`. + undelegateEvents: function() { + this.$el.off('.delegateEvents' + this.cid); + return this; + }, + + // Ensure that the View has a DOM element to render into. + // If `this.el` is a string, pass it through `$()`, take the first + // matching element, and re-assign it to `el`. Otherwise, create + // an element from the `id`, `className` and `tagName` properties. + _ensureElement: function() { + this.setElement(_.result(this, 'el'), false); + } + + }); + + + var extend = function(protoProps, staticProps) { + var staticProps = staticProps || {} + var parent = this; + var child; + var childEvents = {}; + + // The constructor function for the new subclass is either defined by you + // (the "constructor" property in your `extend` definition), or defaulted + // by us to simply call the parent's constructor. + if (protoProps && _.has(protoProps, 'constructor')) { + child = protoProps.constructor; + } else { + child = function(){ return parent.apply(this, arguments); }; + } + + // Extend events so we can subclass views + _.extend(childEvents, parent.prototype.events, protoProps.events) + + // Add static properties to the constructor function, if supplied. + _.extend(child, parent, staticProps); + + // Set the prototype chain to inherit from `parent`, without calling + // `parent`'s constructor function. + var Surrogate = function(){ this.constructor = child; }; + Surrogate.prototype = parent.prototype; + child.prototype = new Surrogate; + + // Add prototype properties (instance properties) to the subclass, + // if supplied. + if (protoProps) _.extend(child.prototype, protoProps); + + // Set a convenience property in case the parent's prototype is needed + // later. + child.prototype.__super__ = parent.prototype; + child.prototype.events = childEvents + + return child; + }; + + View.extend = extend; + + return View; +})(jQuery, _) diff --git a/public/assets/javascripts/ui/site/DocumentModal.js b/public/assets/javascripts/ui/site/DocumentModal.js new file mode 100644 index 0000000..6f16169 --- /dev/null +++ b/public/assets/javascripts/ui/site/DocumentModal.js @@ -0,0 +1,49 @@ + +var DocumentModal = ModalFormView.extend({ + el: ".mediaDrawer.editDocument", + createAction: "/api/docs/new", + updateAction: "/api/docs/edit", + destroyAction: "/api/docs/destroy", + + load: function(name, isNew){ + this.reset() + + if (isNew || name === "new") { + name = sanitize(name) + if (name !== "new") { + this.$("[name='new_name']").val( name.replace(/\s+/g,"-") ) + this.$("[name='displayName']").val( capitalize(name.replace(/-/g," ")) ) + } + this.action = this.createAction + return this.show() + } + + this.action = this.updateAction + + $.get("/api/docs", { name: name }, $.proxy(function(data){ + if (data.isNew) { + this.action = this.createAction + } + + for (var i in data) { + this.$("[name='" + i + "']").val(data[i]) + } + this.$("[name='new_name']").val(name) + + this.show() + }, this)) + }, + + success: function(res){ + window.location.pathname = "/about/" + res.name + }, + + destroy: function(name, cb){ + $.ajax({ + type: "delete", + url: this.destroyAction, + data: { name: name, _csrf: $("[name=_csrf]").val() } + }).complete(cb || function(){}) + }, + +}) diff --git a/public/assets/javascripts/ui/site/EditProfileModal.js b/public/assets/javascripts/ui/site/EditProfileModal.js new file mode 100644 index 0000000..6b89ad8 --- /dev/null +++ b/public/assets/javascripts/ui/site/EditProfileModal.js @@ -0,0 +1,56 @@ + +var EditProfileModal = ModalFormView.extend({ + el: ".mediaDrawer.editProfile", + action: "/api/profile", + method: "put", + + load: function(){ + this.reset() + $.get("/api/profile", $.proxy(function(data){ + console.log(data) + + for (var i in data) { + 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/site/EditProjectModal.js b/public/assets/javascripts/ui/site/EditProjectModal.js new file mode 100644 index 0000000..356d8b7 --- /dev/null +++ b/public/assets/javascripts/ui/site/EditProjectModal.js @@ -0,0 +1,49 @@ + +var EditProjectModal = ModalView.extend({ + el: ".mediaDrawer.editProject", + action: "/project/edit", + + 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() + }, + + submit: function(e){ + e.preventDefault() + + this.$errors.hide(); + this.$errorList.empty() + + var fields = this.$form.serializeArray() + + var request = $.post(this.action, $.param(fields)); + request.done($.proxy(function (response) { + if (response.error) { + this.$errors.show(); + for (var key in response.error.errors) { + this.$errorList.append('
' + response.error.errors[key].message + '
'); + } + return + } + else { + window.location.href = "/profile" + } + }, this)); + } + +}) + diff --git a/public/assets/javascripts/ui/site/NewProjectModal.js b/public/assets/javascripts/ui/site/NewProjectModal.js new file mode 100644 index 0000000..cf2044f --- /dev/null +++ b/public/assets/javascripts/ui/site/NewProjectModal.js @@ -0,0 +1,17 @@ + + +var NewProjectModal = ModalFormView.extend({ + el: ".mediaDrawer.newProject", + action: "/project/new", + + load: function(){ + this.reset() + this.show() + }, + + success: function(){ + // + } + +}) + diff --git a/public/assets/javascripts/ui/site/SignInModal.js b/public/assets/javascripts/ui/site/SignInModal.js new file mode 100644 index 0000000..4c91b54 --- /dev/null +++ b/public/assets/javascripts/ui/site/SignInModal.js @@ -0,0 +1,12 @@ + + +var SignInModal = ModalFormView.extend({ + el: ".mediaDrawer.signin", + action: "/auth/signin", + + success: function(res){ + window.location.href = "/profile" + } + +}) + diff --git a/public/assets/javascripts/ui/site/SignUpModal.js b/public/assets/javascripts/ui/site/SignUpModal.js new file mode 100644 index 0000000..5c651ee --- /dev/null +++ b/public/assets/javascripts/ui/site/SignUpModal.js @@ -0,0 +1,37 @@ +var SignUpModal = ModalFormView.extend({ + el: ".mediaDrawer.signup", + action: "/auth/signup", + + validate: function(){ + var errors = [] + + var username = this.$("#usernameInput").val() + var email = this.$("#emailInput").val() + var pw1 = this.$("#passwordInput1").val() + var pw2 = this.$("#passwordInput2").val() + + if (! username.length) { + errors.push("Please enter a username"); + } + if (! pw1.length) { + errors.push("Please enter a password"); + } + if (! email.length) { + errors.push("Please enter an email address"); + } + else if (email.indexOf("@") === -1) { + errors.push("Please enter a valid email address"); + } + if (pw1 !== pw2) { + errors.push("Passwords don't match"); + } + + return errors + }, + + success: function(res){ + window.location.href = "/profile" + } + +}) + -- cgit v1.2.3-70-g09d2 From e400a34e8a06dcb761959ab126db007ba6832a8a Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 10 Jun 2014 13:00:48 -0400 Subject: refactor mongoose connection to avoid warnings --- .../javascripts/ui/builder/BuilderSettings.js | 3 - server/index.js | 176 +++++++++++---------- server/lib/auth.js | 5 +- 3 files changed, 98 insertions(+), 86 deletions(-) (limited to 'public/assets/javascripts/ui') diff --git a/public/assets/javascripts/ui/builder/BuilderSettings.js b/public/assets/javascripts/ui/builder/BuilderSettings.js index b598a18..ff8a71b 100644 --- a/public/assets/javascripts/ui/builder/BuilderSettings.js +++ b/public/assets/javascripts/ui/builder/BuilderSettings.js @@ -1,7 +1,4 @@ - - - var BuilderSettings = View.extend({ el: "#builderSettings", diff --git a/server/index.js b/server/index.js index 84d9818..21a1859 100644 --- a/server/index.js +++ b/server/index.js @@ -20,87 +20,101 @@ var http = require('http'), // var io = websocket.listen(config.socketPort) -var app = express(); +var app = express() +var SessionStore var DATABASE_URI = process.env.MONGOLAB_URI || ('mongodb://' + config.hostName + '/vvalls') -mongoose.connect(DATABASE_URI); -var SessionStore = new MongoStore({ mongoose_connection: mongoose.connection }) - -// Configure things -app.set('port', config.port); -app.set('views', path.join(__dirname, '../views')); -app.set('view engine', 'ejs'); -app.use(express.static(path.join(__dirname, '../public'))); -app.use(express.logger('dev')); -app.use(express.json()); -app.use(express.urlencoded()); -app.use(express.cookieParser()); -app.use(express.session({ - key: 'vvalls.sid', - secret: 'flibbertigibbet', - cookie: { domain: '.' + config.hostName, maxAge: 43200000 }, - store: SessionStore -})); -app.use(bodyParser()); -app.use(multer()); -app.use(express.query()); -app.use(express.csrf()); -app.use(express.methodOverride()); -app.use(passport.initialize()); -app.use(passport.session()); -// app.use('/api', middleware.ensureAuthenticated, mers({conn: mongoose.connection}).rest()); -app.use(app.router); -app.get('env') === 'development' && app.use(express.errorHandler()); - - -// Configure user authentication -auth.init() - -// Essential middleware -// app.all('*', middleware.enableCORS); - -app.all('*', middleware.ensureLocals); - -// Initialize views -app.get('/', views.home); -app.get('/about', views.docs); -app.get('/about/:name/edit', views.docs); -app.get('/about/:name', views.docs); -app.get('/login', views.modal); -app.get('/signup', views.modal); -app.post('/auth/signin', auth.loggedInLocal); -app.post('/auth/signup', auth.signup); -app.get('/logout', auth.logout); -app.get('/auth/twitter', auth.login('twitter')); -app.get('/auth/twitter/callback', auth.loggedIn('twitter')); -app.get('/auth/facebook', auth.login('facebook')); -app.get('/auth/facebook/callback', auth.loggedIn('facebook')); -app.get('/profile', views.profile) -app.get('/profile/edit', views.profile) -app.get('/profile/:name', views.profile) - -app.get('/api/profile', middleware.ensureAuthenticated, api.profile.show) -app.put('/api/profile', middleware.ensureAuthenticated, api.profile.update) - -app.get('/project/new', middleware.ensureAuthenticated, views.modal); - -app.get('/staff', middleware.ensureAuthenticated, middleware.ensureIsStaff, views.staff.index); -app.get('/staff/bless', middleware.ensureAuthenticated, views.staff.bless); - -app.get('/api/docs', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.show) -app.post('/api/docs/new', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.create) -app.post('/api/docs/edit', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.update) -app.delete('/api/docs/destroy', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.destroy) - -app.use('/builder', middleware.ensureAuthenticated) -app.get('/builder', views.builder) -app.get('/builder/:name', views.builder) - -/* -auth.initSockets(io) -*/ - -http.createServer(app).listen(app.get('port'), function () { - console.log('Express server listening on port ' + app.get('port')); -}); +// + +var site = {} + +site.init = function(){ + mongoose.connect(DATABASE_URI, {}, site.ready); +} + +site.ready = function(){ + auth.init() + site.setup() + site.route() + site.listen() +} + +site.setup = function(){ + var SessionStore = new MongoStore({ mongoose_connection: mongoose.connection }) + + app.set('port', config.port); + app.set('views', path.join(__dirname, '../views')); + app.set('view engine', 'ejs'); + app.use(express.static(path.join(__dirname, '../public'))); + app.use(express.logger('dev')); + app.use(express.json()); + app.use(express.urlencoded()); + app.use(express.cookieParser()); + app.use(express.session({ + key: 'vvalls.sid', + secret: 'flibbertigibbet', + cookie: { domain: '.' + config.hostName, maxAge: 43200000 }, + store: SessionStore + })); + app.use(bodyParser()); + app.use(multer()); + app.use(express.query()); + app.use(express.csrf()); + app.use(express.methodOverride()); + app.use(passport.initialize()); + app.use(passport.session()); + // app.use('/api', middleware.ensureAuthenticated, mers({conn: mongoose.connection}).rest()); + app.use(app.router); + app.get('env') === 'development' && app.use(express.errorHandler()); + + // Essential middleware + // app.all('*', middleware.enableCORS); + app.all('*', middleware.ensureLocals); + + // auth.initSockets(io, SessionStore) +} + +site.route = function () { + app.get('/', views.home); + app.get('/about', views.docs); + app.get('/about/:name/edit', views.docs); + app.get('/about/:name', views.docs); + app.get('/login', views.modal); + app.get('/signup', views.modal); + app.post('/auth/signin', auth.loggedInLocal); + app.post('/auth/signup', auth.signup); + app.get('/logout', auth.logout); + app.get('/auth/twitter', auth.login('twitter')); + app.get('/auth/twitter/callback', auth.loggedIn('twitter')); + app.get('/auth/facebook', auth.login('facebook')); + app.get('/auth/facebook/callback', auth.loggedIn('facebook')); + app.get('/profile', views.profile) + app.get('/profile/edit', views.profile) + app.get('/profile/:name', views.profile) + + app.get('/api/profile', middleware.ensureAuthenticated, api.profile.show) + app.put('/api/profile', middleware.ensureAuthenticated, api.profile.update) + + app.get('/project/new', middleware.ensureAuthenticated, views.modal); + + app.get('/staff', middleware.ensureAuthenticated, middleware.ensureIsStaff, views.staff.index); + app.get('/staff/bless', middleware.ensureAuthenticated, views.staff.bless); + + app.get('/api/docs', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.show) + app.post('/api/docs/new', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.create) + app.post('/api/docs/edit', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.update) + app.delete('/api/docs/destroy', middleware.ensureAuthenticated, middleware.ensureIsStaff, api.docs.destroy) + + app.use('/builder', middleware.ensureAuthenticated) + app.get('/builder', views.builder) + app.get('/builder/:name', views.builder) +} + +site.listen = function(){ + http.createServer(app).listen(app.get('port'), function () { + console.log('Express server listening on port ' + app.get('port')); + }); +} + +site.init() diff --git a/server/lib/auth.js b/server/lib/auth.js index b710a22..018c9ad 100644 --- a/server/lib/auth.js +++ b/server/lib/auth.js @@ -5,6 +5,7 @@ var passport = require('passport'), TwitterStrategy = require('passport-twitter').Strategy, LocalStrategy = require('passport-local').Strategy, passportSocketIo = require("passport.socketio"), + cookieParser = require('express').cookieParser, crypto = require('crypto'), _ = require('lodash'), util = require('./util'), @@ -33,9 +34,9 @@ var auth = { }, auth.insertFacebookUser)); }, - initSockets: function (io, cookieParser, SessionStore) { + initSockets: function (io, express, SessionStore) { io.set('authorization', passportSocketIo.authorize({ - cookieParser: express.cookieParser, + cookieParser: cookieParser, passport: passport, key: 'vvalls.sid', // the name of the cookie where express/connect stores its session_id secret: 'flibbertigibbet', // the session_secret to parse the cookie -- cgit v1.2.3-70-g09d2 From 08421d8921fcc9842ea70d2e5c6439d1154e6d73 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 10 Jun 2014 16:34:52 -0400 Subject: serialization functions --- public/assets/javascripts/rectangles/_env.js | 24 ++++++------- .../javascripts/rectangles/engine/map/draw.js | 2 ++ .../javascripts/rectangles/engine/rooms/_rooms.js | 42 +++++++++++++++++++++- .../assets/javascripts/rectangles/models/rect.js | 3 ++ .../assets/javascripts/rectangles/models/room.js | 12 +++++-- .../assets/javascripts/rectangles/models/vec2.js | 3 ++ .../javascripts/ui/builder/BuilderSettings.js | 5 +++ 7 files changed, 76 insertions(+), 15 deletions(-) (limited to 'public/assets/javascripts/ui') diff --git a/public/assets/javascripts/rectangles/_env.js b/public/assets/javascripts/rectangles/_env.js index 86aebdc..46b43a9 100644 --- a/public/assets/javascripts/rectangles/_env.js +++ b/public/assets/javascripts/rectangles/_env.js @@ -22,18 +22,18 @@ environment.init = function(){ map.center.a = 0 map.center.b = 0 - Rooms.add( new Room ({ - rect: new Rect(-500,-500, 500,500), - height: 500, - })) - Rooms.add( new Room ({ - rect: new Rect(600,0, 1100,500), - height: 500, - })) - Rooms.add( new Room ({ - rect: new Rect(450,150, 650,350), - height: 300, - })) +// Rooms.add( new Room ({ +// rect: new Rect(-500,-500, 500,500), +// height: 500, +// })) +// Rooms.add( new Room ({ +// rect: new Rect(600,0, 1100,500), +// height: 500, +// })) +// Rooms.add( new Room ({ +// rect: new Rect(450,150, 650,350), +// height: 300, +// })) app.movements.gravity(true) diff --git a/public/assets/javascripts/rectangles/engine/map/draw.js b/public/assets/javascripts/rectangles/engine/map/draw.js index f4cb1d2..75e8ad1 100644 --- a/public/assets/javascripts/rectangles/engine/map/draw.js +++ b/public/assets/javascripts/rectangles/engine/map/draw.js @@ -41,6 +41,8 @@ var MapDraw = function(map){ for (var i = 0; i < regions.length; i++) { if (regions[i].dupe) continue ctx.fillStyle = colors[i % colors.length] + ctx.strokeStyle = "#000" + ctx.lineWidth = (1 / map.zoom) fill_region(regions[i]) stroke_sides(regions[i]) } diff --git a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js index efde370..ce1fae7 100644 --- a/public/assets/javascripts/rectangles/engine/rooms/_rooms.js +++ b/public/assets/javascripts/rectangles/engine/rooms/_rooms.js @@ -22,7 +22,7 @@ var Rooms = new function(){ base.add_with_rect = function(rect){ var room = new Room({ rect: rect, - height: quantize(randrange(300,800), 50), + height: 500 // quantize(randrange(300,800), 50), }) base.add(room) } @@ -38,9 +38,49 @@ var Rooms = new function(){ base.forEach = function(f){ return base.values().forEach(f) } + base.map = function(f){ + return base.values().map(f) + } + base.values = function(){ return _.values(base.list) } + + base.serialize = function(){ + var rooms = base.map(function(room){ + return room.serialize() + }) + var rooms_data = { rooms: rooms } + return rooms_data + } + + base.deserialize = function(rooms_data){ + rooms_data.rooms.forEach(function(data){ + var rect = new Rect(data.rect.x[0], data.rect.y[0], data.rect.x[1], data.rect.y[1]) + var room = new Room({ + id: data.id, + rect: rect, + height: data.height + }) + base.add(room) + }) + } + + base.uid = (function(){ + var id = 0 + return function(s){ + s = s || "" + var ss + while (1) { + ss = s + (id++) + if (! (ss in base.list)) { + console.log(ss) + return ss + } + } + } + })() + base.sorted_by_position = function(){ return sort_rooms_by_position( base.values() ) diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js index 7a2ac6f..cb14e66 100644 --- a/public/assets/javascripts/rectangles/models/rect.js +++ b/public/assets/javascripts/rectangles/models/rect.js @@ -94,6 +94,9 @@ window.Rect = (function(){ var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides return s } + Rect.prototype.serialize = function(){ + return { x: this.x.serialize(), y: this.y.serialize() } + } Rect.prototype.quantize = function(n){ this.x.quantize(n) this.y.quantize(n) diff --git a/public/assets/javascripts/rectangles/models/room.js b/public/assets/javascripts/rectangles/models/room.js index 256889d..d0478b3 100644 --- a/public/assets/javascripts/rectangles/models/room.js +++ b/public/assets/javascripts/rectangles/models/room.js @@ -1,7 +1,7 @@ window.Room = (function(){ var Room = function(opt){ - this.id = opt.id || _.uniqueId("room") + this.id = opt.id || Rooms.uid("room_") this.rect = opt.rect this.regions = [] this.walls = [] @@ -15,7 +15,15 @@ window.Room = (function(){ Room.prototype.toString = function(){ return this.rect.toString() } - + + Room.prototype.serialize = function(){ + return { + id: this.id, + rect: this.rect.serialize(), + height: ~~this.height, + } + } + Room.prototype.reset = function(){ var copy = this.rect.clone() copy.id = this.id diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js index 9b0447c..e56a010 100644 --- a/public/assets/javascripts/rectangles/models/vec2.js +++ b/public/assets/javascripts/rectangles/models/vec2.js @@ -91,6 +91,9 @@ vec2.prototype.intersection = function(v){ vec2.prototype.toString = function(){ return "[" + ~~this.a + " " + ~~this.b + "]" } +vec2.prototype.serialize = function(){ + return [ ~~this.a, ~~this.b ] +} vec2.prototype.quantize = function(n){ n = n || 10 this.a = quantize(this.a, n) diff --git a/public/assets/javascripts/ui/builder/BuilderSettings.js b/public/assets/javascripts/ui/builder/BuilderSettings.js index ff8a71b..004c1c4 100644 --- a/public/assets/javascripts/ui/builder/BuilderSettings.js +++ b/public/assets/javascripts/ui/builder/BuilderSettings.js @@ -10,6 +10,11 @@ var BuilderSettings = View.extend({ toggle: function(){ this.$el.toggleClass("active") + }, + + save: function(){ + var data = Rooms.serialize() + console.log(data) } }) -- cgit v1.2.3-70-g09d2