Skip to content

Instantly share code, notes, and snippets.

@chriswitko
Created November 11, 2014 09:11
Show Gist options
  • Save chriswitko/09c3ad9f4e7e62fdde58 to your computer and use it in GitHub Desktop.
Save chriswitko/09c3ad9f4e7e62fdde58 to your computer and use it in GitHub Desktop.
var SBB = $.inherit({
__constructor : function() { // constructor
console.log('init SBB');
},
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function(name){
return this.getUrlVars()[name];
},
// getURIParameter("id") // returns the last id or null if not present
// getURIParameter("id", true) // returns an array of all ids
getURIParameter: function(param, asArray) {
return document.location.search.substring(1).split('&').reduce(function(p,c) {
var parts = c.split('=', 2).map(function(param) { return decodeURIComponent(param); });
if(parts.length == 0 || parts[0] != param) return (p instanceof Array) && !asArray ? null : p;
return asArray ? p.concat(parts.concat(true)[1]) : parts.concat(true)[1];
}, []);
},
initHandlebars: function() {
Handlebars.registerHelper ("isFullfield", function (block) {
console.log('isFullfield', window.isFullfield)
if(window.isFullfield) return block.fn(this)
else return block.inverse(this)
});
Handlebars.registerHelper ("isLastDate", function (block) {
console.log('isLastDate', window.isLastDate)
if(window.isLastDate) return block.fn(this)
else return block.inverse(this)
});
Handlebars.registerHelper ("displayDay", function (context, block) {
var result = '';
if(moment(new Date(context)).format('YYYYMMDD')==moment().format('YYYYMMDD')) {
result = '<h3 class="day">' + 'TODAY' + '<small>' + moment(new Date(context)).format('MMMM Do') + '</small></h3>';
} else {
if(moment(new Date(context)).format('YYYYMMDD')==moment().subtract(1, 'day').format('YYYYMMDD')) {
result = '<h3 class="day">' + 'YESTERDAY' + '<small>' + moment(new Date(context)).format('MMMM Do') + '</small></h3>';
} else {
result = '<h3 class="day">' + moment(new Date(context)).format('dddd').toUpperCase() + '<small>' + moment(new Date(context)).format('MMMM Do') + '</small></h3>';
}
}
return new Handlebars.SafeString(result);
})
Handlebars.registerHelper ("lastDate", function (context, block) {
var difference = true; //NEW
var day = moment(new Date(context)).format('YYYYMMDD');
if (window.lastDay != day) {
difference = true ; //NEW
} else {
difference = false;//OLD
}
window.lastDay = day
window.isLastDate = difference;
window.isFullfield = true;
if(difference) {
return block.fn(this);
}
});
Handlebars.registerHelper('dateFormat', function(context, block) {
if (window.moment && context && moment(context).isValid()) {
var f = block.hash.format || "MMM Do, YYYY";
return moment(new Date(context)).format(f);
} else {
return context;
};
});
if($("#temp-product")) Handlebars.registerPartial("product", $("#temp-product").html());
if($("#temp-search")) Handlebars.registerPartial("search", $("#temp-search").html());
},
initResizeGrid: function() {
console.log('init', 'initResizeGrid');
$(window).resize(function(){
$('.grid-products').find('.grid-image').each(function(index) {
$(this).css({'height': (($(this).closest('.grid-element').width()))+'px'});
});
$('.grid-sections').find('.grid-image').each(function(index) {
$(this).css({'height': (($(this).closest('.grid-element').width()))+'px'});
$(this).find('.grid-overfly-txt').each(function(index) {
$(this).css({'top': (($(this).parent().height()/2)-($(this).height()/2))+'px'});
});
});
}).trigger('resize')
},
initBootstrap: function() {
$('[rel="tooltip"]').tooltip();
$('.nav-tabs').stickyTabs();
$('body').on('click', function (e) {
$('[rel="popover"]').each(function () {
if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
$(this).popover('hide');
}
});
});
},
initLinkBind: function() {
$('a.bind').click(function(e){ e.preventDefault(); });
$('form.bind').submit(function(e){ e.preventDefault(); });
},
initActionsBind: function() {
// observable.on('userFollow', SBBUser.hello);
},
initRefreshNotificationsBadge: function() {
// https://github.com/cowboy/jquery-dotimeout
$.doTimeout( 'getNotifications', 5000, function(){
console.log('updating notifs...');
return true;
});
},
init: function() {
this.initHandlebars();
this.initBootstrap();
this.initResizeGrid();
this.initLinkBind();
this.initActionsBind();
// this.initRefreshNotificationsBadge();
},
hello : function() {
alert('hello');
},
getStaticProperty : function() {
return this.__self.staticMember; // access to static
}
});
var SBBUser = $.inherit(SBB, {
__constructor : function(property) { // constructor
console.log('init SBBUser');
this.init();
},
userFollow : function(el, ctx) {
if(!ctx.follower_id) {
swal({title: "Please sign in...", text: "You need to sign in to proceed.", type: "error", confirmButtonText: "OK" });
return;
}
var api = new $.RestClient('/api/user/');
api.add('follow');
var request = api.follow.read({followee: ctx.followee_id, follower: ctx.follower_id});
request.done(function (data) {
$('#action-btn-follow-'+ctx.followee_id).addClass('hid');
$('#action-btn-unfollow-'+ctx.followee_id).removeClass('hid');
})
},
userUnfollow : function(el, ctx) {
var api = new $.RestClient('/api/user/');
api.add('unfollow');
var request = api.unfollow.read({followee: ctx.followee_id, follower: ctx.follower_id});
request.done(function (data) {
$('#action-btn-follow-'+ctx.followee_id).removeClass('hid');
$('#action-btn-unfollow-'+ctx.followee_id).addClass('hid');
})
},
initActionsBind: function() {
observable.on('userFollow', this.userFollow);
observable.on('userUnfollow', this.userUnfollow);
},
init: function() {
this.initActionsBind();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment