Last active
December 18, 2015 16:29
-
-
Save simpleshadow/5811749 to your computer and use it in GitHub Desktop.
/convesrations/:convesration_id tempalte does not render when loaded directly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*jshint expr: true, curly: false, unused: false, browser: true*/ | |
| /*global $, Ember, iScroll, ExpandScroll*/ | |
| 'use strict'; | |
| var App = Ember.Application.create({ | |
| LOG_TRANSITIONS: true, | |
| ready: function () { | |
| document.addEventListener('touchmove', function (e) { | |
| e.preventDefault(); | |
| return false; | |
| }, false); | |
| } | |
| }); | |
| App.Router = Em.Router.extend({ | |
| // location: 'history' | |
| }); | |
| App.Router.map(function () { | |
| this.resource('welcome'); | |
| this.resource('signup'); | |
| this.resource('login'); | |
| this.resource('conversations', function () { | |
| this.route('new'); | |
| this.resource('conversation', { path: '/:conversation_id' }, function () { | |
| this.resource('message', { path: '/:message_id' }, function () {}); | |
| }); | |
| }); | |
| this.resource('camera'); | |
| this.resource('friends', function () { | |
| this.resource('friend', { path: ':friend_id' }, function () {}); | |
| }); | |
| this.resource('settings', function () { | |
| this.resource('profile-pic', { path: 'profile-pic' }, function () {}); | |
| this.resource('notifications', { path: 'notifications' }, function () {}); | |
| this.resource('networks', { path: 'networks' }, function () {}); | |
| }); | |
| this.resource('invite'); | |
| this.resource('users', function () { | |
| this.resource('user', { path: ':user_id' }, function () {}); | |
| }); | |
| }); | |
| App.IndexRoute = Ember.Route.extend({ | |
| redirect: function () { | |
| this.transitionTo('welcome'); | |
| // this.transitionTo('conversations'); | |
| } | |
| }); | |
| App.ConversationsRoute = Ember.Route.extend({ | |
| model: function () { | |
| return App.Conversation.find({"users": {"$in": ["2hbsnf5YkM"]}}); | |
| // Todo: Pull logged in user's ID from controller when ready | |
| /*if (App.AccountController.get('content').get('id') != null) { | |
| return App.Conversation.find({"users": {"$in": [App.AccountController.get('content').get('id')]}}); | |
| } else { | |
| Ember.run.next(this, function() { | |
| this.model(); | |
| }); | |
| }*/ | |
| } | |
| }); | |
| App.ConversationRoute = Ember.Route.extend({ | |
| model: function (param) { | |
| /*var conversation = App.Conversation.find(param.conversation_id); | |
| console.log('ConversationRoute::model', conversation); | |
| return conversation;*/ | |
| var conversation = this.controllerFor('conversations').get('content').filterProperty('id', param.conversation_id); | |
| console.log('ConversationRoute::model', conversation); | |
| Ember.run.next(this, function() { | |
| this.controllerFor('conversation').set('content', conversation.get('firstObject')); | |
| }); | |
| }, | |
| serialize: function(model, params) { | |
| console.log('ConversationRoute::serialize', model, params); | |
| if (model) { | |
| return {conversation_id: model.get('id')}; | |
| } | |
| } | |
| }); | |
| App.SignupRoute = Ember.Route.extend({ | |
| model: function () { | |
| return App.Account.all() | |
| } | |
| }); | |
| App.LoginRoute = Ember.Route.extend({ | |
| model: function () { | |
| return App.Account.all() | |
| } | |
| }); | |
| App.ConversationsView = Ember.View.extend({ | |
| didInsertElement: function () { | |
| var self = this; | |
| if (this.$().find('.js-capture-container__imagewrapper img').length >= App.Capture.all().get('length') && App.Capture.all().get('length') != 0) { | |
| this.bodyScroll = new iScroll($('.js-conversations')[0], { | |
| scrollbarClass: 'bodyScrollbar' | |
| }); | |
| ExpandScroll.bind(this.bodyScroll); | |
| } else { | |
| setTimeout(function () { | |
| Ember.run.next(self, function() { | |
| self.didInsertElement(); | |
| }); | |
| }, 250); | |
| } | |
| } | |
| }); | |
| App.ConversationView = Ember.View.extend({ | |
| templateName: 'conversation', | |
| didInsertElement: function () { | |
| if (this.$() && this.$().find("#pics .wrapper .image").length >= App.Capture.all().get('length')) { | |
| var conversationScroll = new iScroll($('.js-conversation')[0], { | |
| vScrollbar: false | |
| }); | |
| var picScroll = new iScroll('pics', { | |
| vScrollbar: false | |
| }); | |
| setTimeout(function () { | |
| conversationScroll.scrollTo(0, -$('.js-conversation').height(), 1); | |
| conversationScroll.refresh(); | |
| }, 0); | |
| setTimeout(function () { | |
| picScroll.scrollTo(0, -$('#pics').height()+150, 1); | |
| picScroll.refresh(); | |
| }); | |
| var lastConvo = $('.js-conversation .conversation:last-of-type'); | |
| var lastConvoRoundedUpHeight = (Math.ceil(lastConvo.height() / 10) * 10)+2; | |
| lastConvo.height() > 95 && lastConvo.height(lastConvoRoundedUpHeight); | |
| } else { | |
| Ember.run.next(this, function () { | |
| this.didInsertElement(); | |
| }); | |
| } | |
| } | |
| }); | |
| App.SignupController = Ember.ObjectController.extend({ | |
| submit: function () { | |
| var self = this, | |
| signupAccount = App.Account.createRecord({ | |
| username: self.get('username'), | |
| password: self.get('password'), | |
| email: self.get('email'), | |
| phoneNumber: self.get('phoneNumber'), | |
| description: self.get('description') | |
| }); | |
| signupAccount.one('didCreate', function () { | |
| var account = App.Account.find({ | |
| username: self.get('username'), | |
| password: self.get('password') | |
| }); | |
| account.one('didLoad', function () { | |
| App.AccountController.set('content', this.filterProperty('username').get('firstObject')); | |
| self.get('target').transitionTo('conversations'); | |
| }); | |
| }); | |
| signupAccount.get("store").commit(); | |
| } | |
| }); | |
| App.LoginController = Ember.ObjectController.extend({ | |
| submit: function() { | |
| var self = this, | |
| account = App.Account.createRecord(); | |
| account.login({ | |
| username: self.get('username'), | |
| password: self.get('password') | |
| }); | |
| account.one('didCreate', function () { | |
| Ember.run.next(function() { | |
| App.AccountController.set('content', account); | |
| if (account.get('id') != null) { | |
| App.AccountController.set('user', App.User.find({objectId: account.get('id')})); | |
| } | |
| self.get('target').transitionTo('conversations'); | |
| }); | |
| }); | |
| } | |
| }); | |
| App.AccountController = Ember.ObjectController.create(); | |
| App.ConversationController = Ember.ObjectController.extend({ | |
| capture: function () { | |
| var self = this; | |
| var message = this.get('newMessage'); | |
| if (!message.trim()) { return; } | |
| var postCaptureFile = function () { | |
| var promise = new Ember.RSVP.Promise(function (resolve, reject) { | |
| forge.file.getImage({width: 500, height: 500, source: 'camera'}, function (file) { | |
| /*forge.file.URL(file, function (url) { | |
| $('body').prepend($('<img>').attr('src', url)); | |
| });*/ | |
| if (file) { | |
| var date = (new Date()).getTime(); | |
| forge.request.ajax({ | |
| url: 'https://api.parse.com/1/files/' + date + '.jpg', | |
| headers: { | |
| 'X-Parse-Application-Id': DEFAULT.PARSE.APP_ID, | |
| 'X-Parse-REST-API-Key': DEFAULT.PARSE.REST_API_KEY | |
| }, | |
| type: 'POST', | |
| files: [file], | |
| fileUploadMethod: 'raw', | |
| dataType: 'json', | |
| success: function (json) { | |
| resolve(json); | |
| }, | |
| error: function (json) { | |
| reject(json); | |
| } | |
| }); | |
| } else { | |
| resolve({name: 'dummy.png', url: 'http://placehold.it/125x125'}); | |
| } | |
| }); | |
| }); | |
| return promise; | |
| }; | |
| if ( $('body .js-conversation-input-box__input').val() != '') { | |
| postCaptureFile().then(function (json) { | |
| var message, capture; | |
| if (typeof data === 'string') { | |
| json = JSON.parse(json); | |
| } | |
| message = App.Message.createRecord({ | |
| text: $('body .js-conversation-input-box__input').val(), | |
| private: true | |
| }); | |
| message.one('didCreate', function() { | |
| Ember.run.next(function() { | |
| capture = App.Capture.createRecord({ | |
| file: { | |
| '__type': 'File', | |
| name: json.name | |
| }, | |
| geolocation: '', | |
| type: 'Image', | |
| url: json.url | |
| }); | |
| capture.one('didCreate', function() { | |
| Ember.run.next(function() { | |
| message.get('captures').addObject(capture); | |
| message.set('conversation', self.get('content')); | |
| message.get('users').addObject(App.AccountController.get('user.firstObject')); | |
| message.get('store').commit(); | |
| self.set('newMessage', ''); | |
| }); | |
| }); | |
| capture.get("store").commit(); | |
| }); | |
| }); | |
| message.get("store").commit(); | |
| }, function (error) { | |
| alert('Problem creating your capchat.'); | |
| console.log('[ERROR]', error); | |
| }); | |
| } | |
| } | |
| }); | |
| App.Store = DS.Store.extend({ | |
| revision: 12, | |
| adapter: DS.ParseAdapter.create({ | |
| applicationId: 'secret', | |
| restApiId: 'secret', | |
| javascriptId: 'secret' | |
| }) | |
| }); | |
| App.Conversation = DS.ParseModel.extend({ | |
| messages: DS.hasMany('App.Message') | |
| }); | |
| App.Message = DS.ParseModel.extend({ | |
| conversation: DS.belongsTo('App.Conversation'), | |
| captures: DS.hasMany('App.Capture'), | |
| users: DS.hasMany('App.User'), | |
| text: DS.attr('string'), | |
| private: DS.attr('boolean') | |
| }); | |
| App.Capture = DS.ParseModel.extend({ | |
| message: DS.belongsTo('App.Message'), | |
| file: DS.attr('object'), | |
| type: DS.attr('string'), | |
| url: DS.attr('string'), | |
| geolocation: DS.attr('string') | |
| }); | |
| App.User = DS.ParseUser.extend({ | |
| messages: DS.hasMany('App.Message'), | |
| phoneNumber: DS.attr('string'), | |
| description: DS.attr('string'), | |
| facebookId: DS.attr('string'), | |
| twitterId: DS.attr('string') | |
| }); | |
| App.Account = DS.ParseUser.extend({ | |
| friends: DS.hasMany('App.User'), | |
| phoneNumber: DS.attr('string'), | |
| description: DS.attr('string'), | |
| facebookId: DS.attr('string'), | |
| twitterId: DS.attr('string') | |
| }); | |
| Ember.Handlebars.registerBoundHelper('date', function (date) { | |
| return moment(date).fromNow(); | |
| }); | |
| Ember.Handlebars.registerHelper('back', function (text) { | |
| var button = $('<a/>').text(text).attr('onclick', 'window.history.back();'); | |
| return new Handlebars.SafeString($('<div/>').append(button).html()); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment