cleaning up the code a little, grouping helpers in helpers({...})

This commit is contained in:
Sacha Greif 2012-09-25 09:20:49 +09:00
parent fd990ea5c1
commit 56824622ca
11 changed files with 234 additions and 271 deletions

View file

@ -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 = '<br />';
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',
@ -255,6 +156,3 @@ SharrreOptions={
enableCounter: false,
enableTracking: true
}
Meteor.startup(function(){
});

27
client/js/mixpanel.js Normal file
View file

@ -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 || []);

66
client/js/proxino.js Normal file
View file

@ -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);
}
}

View file

@ -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);
}
}
})();

View file

@ -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});
}
};
}
};
})();

View file

@ -14,7 +14,7 @@
</a>
</div>
<div class="comment-meta">
<a class="comment-username" href="/user/{{user_id}}">{{author}}</a>
<a class="comment-username" href="/users/{{user_id}}">{{author}}</a>
<span class="comment-time">{{ago}},</span>
<span class="points">{{votes}} <span class="unit">points </span>
<a href="/comments/{{_id}}" class="comment-permalink icon-link goto-comment">link</a>

View file

@ -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);
@ -53,7 +28,14 @@ Template.footer.rendered = function(){
});
}
}
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();

View file

@ -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({
@ -38,30 +25,3 @@ Template.nav.events = {
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!")
}
};

View file

@ -36,7 +36,7 @@
{{/if}}
{{#if url}}<span class="post-domain">{{domain}}</span>{{/if}}
</h3>
<p class="post-meta"><span class="points">{{votes}} <span class="unit">points </span></span>by <a class="post-author" href="/user/{{user_id}}">{{author}}</a> <span class="post-time">{{ago}}</span><span class="comments">, <a class="go-to-comments" href="posts/{{_id}}">{{comments}} comments</a></span>
<p class="post-meta"><span class="points">{{votes}} <span class="unit">points </span></span>by <a class="post-author" href="/users/{{user_id}}">{{author}}</a> <span class="post-time">{{ago}}</span><span class="comments">, <a class="go-to-comments" href="posts/{{_id}}">{{comments}} comments</a></span>
{{#if can_edit}}
| <a href="/posts/{{_id}}/edit" class="edit-link goto-edit">Edit</a>
{{/if}}

View file

@ -5,7 +5,6 @@ Template.settings.events = {
var title= $('#title').val();
var theme = $('#theme').val();
var footerCode=$("#footer_code").val();
console.log(footerCode);
var analyticsCode = $('#analytics_code').val();
var tlkioChannel = $('#tlkio_channel').val();
var mixpanelId= $('#mixpanel_id').val();

32
lib/helpers.js Normal file
View file

@ -0,0 +1,32 @@
t=function(message){
var d=new Date();
console.log("### "+message+" rendered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds());
}
nl2br= function(str) {
var breakTag = '<br />';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
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);
}
}