From 56824622caa320c2ba461b9e537c1c0047cf7588 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Tue, 25 Sep 2012 09:20:49 +0900 Subject: [PATCH] cleaning up the code a little, grouping helpers in helpers({...}) --- client/app.js | 106 +---------------------------- client/js/mixpanel.js | 27 ++++++++ client/js/proxino.js | 66 ++++++++++++++++++ client/templates/comment_edit.js | 80 +++++++++++----------- client/templates/comment_form.js | 69 +++++++++---------- client/templates/comment_item.html | 2 +- client/templates/footer.js | 62 ++++++----------- client/templates/nav.js | 58 +++------------- client/templates/post_item.html | 2 +- client/templates/settings.js | 1 - lib/helpers.js | 32 +++++++++ 11 files changed, 234 insertions(+), 271 deletions(-) create mode 100644 client/js/mixpanel.js create mode 100644 client/js/proxino.js create mode 100644 lib/helpers.js diff --git a/client/app.js b/client/app.js index c86e1cbcd..81dd46a9e 100644 --- a/client/app.js +++ b/client/app.js @@ -21,72 +21,6 @@ Meteor.subscribe('comments', function() { Settings = new Meteor.Collection('settings'); Meteor.subscribe('settings', function(){ if(proxinoKey=getSetting('proxinoKey')){ - Proxino = { - key:null, - log:function(obj){ - if(typeof(obj) === "string"){ - obj = {type:"Message", body:obj} - } - if(obj.type === undefined || obj.body === undefined){ - throw "Make sure object meets form:{type:_,body:_}" - } - if(Proxino.key === null){ - throw "Please set your API key." - } - obj.key = Proxino.key; - if(obj.url === undefined){ - var g_url; - try{ - g_url = Proxino.get_url(obj.body); - } - catch(e){ - g_url = null; - } - if(g_url !== null && g_url.length > 0){ - obj.url = g_url[0]; - } - else{ - obj.url = document.URL; - } - } - try{ - $.ajax({ - url:"https://p.proxino.com/message", - data:obj, - dataType:"jsonp", - success:function(data){ - //console.log(data); - } - }); - } - catch(exc){ - // Ignore failure to post, don't want recursive loop - } - }, - track_errors:function(){ - if(Proxino.key === null){ - throw "Please set your API key." - } - window.onerror = function(msg, url, lineno){ - var data = {type:"Exception",body:msg}; - if(msg === undefined){ - data.body = "No message"; - } - if(url !== undefined && url !== "undefined" && url !== ""){ - data.url = url; - } - if(lineno !== undefined && lineno !== 0){ - data.lineno = lineno; - } - //var body_str = msg + " at resource " + url + " in line " + lineno; - Proxino.log(data); - } - }, - get_url:function(text) { - var urlRegex = /(https?:\/\/[^\s]+)/g; - return text.match(urlRegex); - } - } Proxino.key = proxinoKey; Proxino.track_errors(); } @@ -106,7 +40,6 @@ if (Meteor.is_client) { return page; }, require_login: function(page) { - console.log(Meteor.user()); if (Meteor.user()) { return page; } else { @@ -131,7 +64,7 @@ if (Meteor.is_client) { 'users':'users', 'account':'user_edit', 'forgot_password':'forgot_password', - 'user/:id': 'user_profile', + 'users/:id': 'user_profile', 'users/:id/edit':'user_edit' }, top: function() { this.goto('posts_top'); }, @@ -191,42 +124,10 @@ if (Meteor.is_client) { }); } -t=function(message){ - var d=new Date(); - console.log("### "+message+" rendered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()); -} - -function nl2br (str) { -var breakTag = '
'; -return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2'); -} - $.fn.exists = function () { return this.length !== 0; } -currentUserIsAdmin = function(){ - return Session.get('currentUserIsAdmin'); -} - -getSetting = function(setting){ - var settings=Settings.find().fetch()[0]; - if(settings){ - return settings[setting]; - } - return ''; -} - -getCurrentUserEmail = function(){ - return Meteor.user() ? Meteor.user().emails[0].email : ''; -} - -trackEvent = function(event, properties){ - var properties= (typeof properties === 'undefined') ? {} : properties; - if(typeof mixpanel != 'undefined'){ - mixpanel.track(event, properties); - } -} EpicEditorOptions={ container: 'editor', basePath: '/editor', @@ -254,7 +155,4 @@ SharrreOptions={ enableHover: false, enableCounter: false, enableTracking: true -} -Meteor.startup(function(){ - -}); \ No newline at end of file +} \ No newline at end of file diff --git a/client/js/mixpanel.js b/client/js/mixpanel.js new file mode 100644 index 000000000..7da13da85 --- /dev/null +++ b/client/js/mixpanel.js @@ -0,0 +1,27 @@ +(function (c, a) { + window.mixpanel = a; + var b, d, h, e; + b = c.createElement("script"); + b.type = "text/javascript"; + b.async = !0; + b.src = ("https:" === c.location.protocol ? "https:" : "http:") + '//cdn.mxpnl.com/libs/mixpanel-2.1.min.js'; + d = c.getElementsByTagName("script")[0]; + d.parentNode.insertBefore(b, d); + a._i = []; + a.init = function (b, c, f) { + function d(a, b) { + var c = b.split("."); + 2 == c.length && (a = a[c[0]], b = c[1]); + a[b] = function () { + a.push([b].concat(Array.prototype.slice.call(arguments, 0))) + } + } + var g = a; + "undefined" !== typeof f ? g = a[f] = [] : f = "mixpanel"; + g.people = g.people || []; + h = "disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config people.identify people.set people.increment".split(" "); + for (e = 0; e < h.length; e++) d(g, h[e]); + a._i.push([b, c, f]) + }; + a.__SV = 1.1 +})(document, window.mixpanel || []); \ No newline at end of file diff --git a/client/js/proxino.js b/client/js/proxino.js new file mode 100644 index 000000000..159534b63 --- /dev/null +++ b/client/js/proxino.js @@ -0,0 +1,66 @@ +Proxino = { + key:null, + log:function(obj){ + if(typeof(obj) === "string"){ + obj = {type:"Message", body:obj} + } + if(obj.type === undefined || obj.body === undefined){ + throw "Make sure object meets form:{type:_,body:_}" + } + if(Proxino.key === null){ + throw "Please set your API key." + } + obj.key = Proxino.key; + if(obj.url === undefined){ + var g_url; + try{ + g_url = Proxino.get_url(obj.body); + } + catch(e){ + g_url = null; + } + if(g_url !== null && g_url.length > 0){ + obj.url = g_url[0]; + } + else{ + obj.url = document.URL; + } + } + try{ + $.ajax({ + url:"https://p.proxino.com/message", + data:obj, + dataType:"jsonp", + success:function(data){ + //console.log(data); + } + }); + } + catch(exc){ + // Ignore failure to post, don't want recursive loop + } + }, + track_errors:function(){ + if(Proxino.key === null){ + throw "Please set your API key." + } + window.onerror = function(msg, url, lineno){ + var data = {type:"Exception",body:msg}; + if(msg === undefined){ + data.body = "No message"; + } + if(url !== undefined && url !== "undefined" && url !== ""){ + data.url = url; + } + if(lineno !== undefined && lineno !== 0){ + data.lineno = lineno; + } + //var body_str = msg + " at resource " + url + " in line " + lineno; + Proxino.log(data); + } + }, + get_url:function(text) { + var urlRegex = /(https?:\/\/[^\s]+)/g; + return text.match(urlRegex); + } +} \ No newline at end of file diff --git a/client/templates/comment_edit.js b/client/templates/comment_edit.js index 207a31863..7dd93cc27 100644 --- a/client/templates/comment_edit.js +++ b/client/templates/comment_edit.js @@ -1,46 +1,48 @@ -(function(){ - var editor; +Template.comment_edit.helpers({ + comment:function(){ + var comment= Comments.findOne(Session.get('selected_comment_id')); + return comment; + } +}); - Template.comment_edit.events = { - 'click input[type=submit]': function(e){ - e.preventDefault(); - if(!Meteor.user()) throw 'You must be logged in.'; +Template.comment_edit.rendered = function(){ + var comment= Comments.findOne(Session.get('selected_comment_id')); + if(comment && Meteor.user() && !this.editor){ + this.editor = new EpicEditor(EpicEditorOptions).load(); + this.editor.importFile('editor',comment.body); + } +} - var selected_comment_id=Session.get("selected_comment_id"); - var selected_post_id=Comments.findOne(selected_comment_id).post; - var body = editor.exportFile(); +Template.comment_edit.events = { + 'click input[type=submit]': function(e, instance){ + e.preventDefault(); + if(!Meteor.user()) throw 'You must be logged in.'; - var comment_id = Comments.update(selected_comment_id, - { - $set: { - body: body - } - } - ); - Router.navigate("posts/"+selected_post_id, {trigger:true}); - } + var selected_comment_id=Session.get("selected_comment_id"); + var selected_post_id=Comments.findOne(selected_comment_id).post; + var content = instance.editor.exportFile(); + + var comment_id = Comments.update(selected_comment_id, + { + $set: { + body: content + } + } + ); + Router.navigate("posts/"+selected_post_id, {trigger:true}); + } + + , 'click .delete-link': function(e){ + e.preventDefault(); + if(confirm("Are you sure?")){ + var selected_comment_id=Session.get("selected_comment_id"); + Comments.remove(selected_comment_id); + Router.navigate("comments/deleted", {trigger:true}); + } + } + + }; - , 'click .delete-link': function(e){ - e.preventDefault(); - if(confirm("Are you sure?")){ - var selected_comment_id=Session.get("selected_comment_id"); - Comments.remove(selected_comment_id); - Router.navigate("comments/deleted", {trigger:true}); - } - } - }; - Template.comment_edit.comment = function(){ - var comment= Comments.findOne(Session.get('selected_comment_id')); - return comment; - }; - Template.comment_edit.rendered = function(){ - var comment= Comments.findOne(Session.get('selected_comment_id')); - if(comment){ - editor= new EpicEditor(epicEditorOptions).load(); - editor.importFile('editor',comment.body); - } - } -})(); diff --git a/client/templates/comment_form.js b/client/templates/comment_form.js index 94f9db5ba..0f85d4c76 100644 --- a/client/templates/comment_form.js +++ b/client/templates/comment_form.js @@ -1,41 +1,38 @@ -(function(){ - - // var editor; - - Template.comment_form.show_comment_form = function(){ - return Meteor.user() !== null; - }; - - Template.comment_form.rendered = function(){ - // t("post_page"); - if(Meteor.user() && !this.editor){ - this.editor = new EpicEditor(EpicEditorOptions).load(); - } +Template.comment_form.helpers({ + show_comment_form: function(){ + return Meteor.user() !== null; } +}); - Template.comment_form.events = { - 'click input[type=submit]': function(e, instance){ - e.preventDefault(); - var $comment = $('#comment'); - var content = instance.editor.exportFile(); +Template.comment_form.rendered = function(){ + if(Meteor.user() && !this.editor){ + this.editor = new EpicEditor(EpicEditorOptions).load(); + } +} - if(selected_comment_id=Session.get('selected_comment_id')){ - // post a child comment - var parent_comment_id =selected_comment_id; - var post_id=Comments.findOne(parent_comment_id).post; - var comment_id=Meteor.call('comment', post_id, parent_comment_id, content); - Session.set('selected_comment_id', null); - Router.navigate('posts/'+post_id, {trigger:true}); - }else{ - // post a root comment - var parent_comment_id=null; - var post_id=Session.get('selected_post_id'); - var comment_id=Meteor.call('comment', post_id, parent_comment_id, content); - $comment.val(''); - } +Template.comment_form.events = { + 'click input[type=submit]': function(e, instance){ + e.preventDefault(); + var $comment = $('#comment'); + var content = instance.editor.exportFile(); - trackEvent("new comment", {'post ID': post_id, 'comment ID': comment_id}); + if(selected_comment_id=Session.get('selected_comment_id')){ + // post a child comment + var parent_comment_id =selected_comment_id; + var post_id=Comments.findOne(parent_comment_id).post; + var comment_id=Meteor.call('comment', post_id, parent_comment_id, content); + Session.set('selected_comment_id', null); + Router.navigate('posts/'+post_id, {trigger:true}); + }else{ + // post a root comment + var parent_comment_id=null; + var post_id=Session.get('selected_post_id'); + var comment_id=Meteor.call('comment', post_id, parent_comment_id, content); + $comment.val(''); + } + + trackEvent("new comment", {'post ID': post_id, 'comment ID': comment_id}); + + } +}; - } - }; -})(); diff --git a/client/templates/comment_item.html b/client/templates/comment_item.html index 846029bfd..7f40946dc 100644 --- a/client/templates/comment_item.html +++ b/client/templates/comment_item.html @@ -14,7 +14,7 @@
- {{author}} + {{author}} {{ago}}, {{votes}} points link diff --git a/client/templates/footer.js b/client/templates/footer.js index 45144bfd4..8476a3cf5 100644 --- a/client/templates/footer.js +++ b/client/templates/footer.js @@ -1,43 +1,18 @@ -Template.footer.footerCode = function(){ - return getSetting('footerCode'); -} -Template.footer.analyticsCode = function(){ - return getSetting('analyticsCode'); -} -Template.footer.tlkioChannel = function(){ - return getSetting('tlkioChannel'); -} -Template.footer.rendered = function(){ - if((mixpanel_id=getSetting("mixpanel_id")) && !window.mixpanel){ - (function (c, a) { - window.mixpanel = a; - var b, d, h, e; - b = c.createElement("script"); - b.type = "text/javascript"; - b.async = !0; - b.src = ("https:" === c.location.protocol ? "https:" : "http:") + '//cdn.mxpnl.com/libs/mixpanel-2.1.min.js'; - d = c.getElementsByTagName("script")[0]; - d.parentNode.insertBefore(b, d); - a._i = []; - a.init = function (b, c, f) { - function d(a, b) { - var c = b.split("."); - 2 == c.length && (a = a[c[0]], b = c[1]); - a[b] = function () { - a.push([b].concat(Array.prototype.slice.call(arguments, 0))) - } - } - var g = a; - "undefined" !== typeof f ? g = a[f] = [] : f = "mixpanel"; - g.people = g.people || []; - h = "disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config people.identify people.set people.increment".split(" "); - for (e = 0; e < h.length; e++) d(g, h[e]); - a._i.push([b, c, f]) - }; - a.__SV = 1.1 - })(document, window.mixpanel || []); - mixpanel.init(mixpanel_id); +Template.footer.helpers({ + footerCode: function(){ + return getSetting('footerCode'); + } + ,analyticsCode: function(){ + return getSetting('analyticsCode'); + } + ,tlkioChannel: function(){ + return getSetting('tlkioChannel'); + } +}); +Template.footer.rendered = function(){ + if((mixpanel_id=getSetting("mixpanel_id")) && window.mixpanel.length==0){ + mixpanel.init(mixpanel_id); if(Meteor.user()){ var currentUserEmail=getCurrentUserEmail(); mixpanel.people.identify(currentUserEmail); @@ -52,8 +27,15 @@ Template.footer.rendered = function(){ 'email': currentUserEmail }); } - } + } + + document.title = getSetting("title"); + + if(Meteor.user() && !Meteor.user().loading && !Meteor.user().createdAt){ + throwError("Due to the Auth API update, please log out and then create a new account. Sorry!") + } } + Template.footer.events = { 'click .open-chat': function(e){ e.preventDefault(); diff --git a/client/templates/nav.js b/client/templates/nav.js index 974b70107..aaf21e046 100644 --- a/client/templates/nav.js +++ b/client/templates/nav.js @@ -1,30 +1,17 @@ +Template.nav.helpers({ + logged_in: function(){ + return Meteor.user() !== null; + } + ,is_admin: function(){ + return currentUserIsAdmin(); + } +}) + Template.nav.events = { 'click #logout': function(event){ event.preventDefault(); Meteor.logout(); } - - , 'click #signup': function(event){ - event.preventDefault(); - // Session.set('previous_state', Session.get('state')); - // Session.set('state', 'signup'); - Router.navigate('signup', {trigger: true}); - } - - , 'click #signin': function(event){ - event.preventDefault(); - // Session.set('previous_state', Session.get('state')); - // Session.set('state', 'signin'); - Router.navigate('signin', {trigger: true}); - } - - , 'click #submit': function(event){ - event.preventDefault(); - // Session.set('previous_state', Session.get('state')); - // Session.set('state', 'submit'); - Router.navigate('submit', {trigger: true}); - } - , 'click #mobile-menu': function(event){ event.preventDefault(); $.pageslide({ @@ -37,31 +24,4 @@ Template.nav.events = { e.preventDefault(); Router.navigate('account', {trigger:true}); } -}; - -Template.nav.logged_in = function(){ - return Meteor.user() !== null; -}; - -Template.nav.site_title = function(){ - var setting=Settings.find().fetch()[0]; - if(setting){ - return setting.title; - } -} - -Template.nav.is_admin = function(){ - return currentUserIsAdmin(); - }; - -Template.nav.rendered = function(){ - - var setting=Settings.find().fetch()[0]; - if(setting){ - document.title = setting.title; - } - - if(Meteor.user() && !Meteor.user().loading && !Meteor.user().createdAt){ - throwError("Due to the Auth API update, please log out and then create a new account. Sorry!") - } }; \ No newline at end of file diff --git a/client/templates/post_item.html b/client/templates/post_item.html index 3db094b5f..b27814cd7 100644 --- a/client/templates/post_item.html +++ b/client/templates/post_item.html @@ -36,7 +36,7 @@ {{/if}} {{#if url}}{{domain}}{{/if}} -