Skip to content

Instantly share code, notes, and snippets.

@osnysantos

osnysantos/*.js Secret

Created Sep 18, 2015
Embed
What would you like to do?
var MySite = {
get_age: function(age){
switch(age) {
case 1:
return "below 20";
break;
default:
return "above 20";
}
},
get_interests: function(interest){
var lastIndex = interest.lastIndexOf(", ");
var s1 = interest.substring(0, lastIndex);
var s2 = interest.substring(lastIndex + 1);
return s1 + " and " + s2;
},
get_newletter: function(newsletter){
if (newsletter === "false") {
return "don't";
} else {
return "";
}
},
handleFileSelect: function(evt) {
var files = evt.target.files; // FileList object
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
image = e.target.result;
$('img#avatar').attr('attribute', 'value');
$('.profile-avatar').addClass('bgc').css("background-image", "url("+e.target.result+")");
};
})(f);
reader.readAsDataURL(f);
}
}
}
$(document).ready(function() {
$(document).on('keyup blur onload', '#user_interest', function(e) {
e.preventDefault();
last_char = $(this).val()[$(this).val().length - 1];
$('.interests').html("");
tags = $(this).val().split(", ").filter(function(e){return e});
$.each(tags,function(index, value){
$('.interests').append('<span class="label label-default">'+value.replace(',','')+'<span class="glyphicon glyphicon-remove glyphicon-white close-button"></span></span>\n');
});
});
$(document).on('click', '.interests .close-button', function(event) {
event.preventDefault();
text = $(this).parent().text();
if(text.indexOf(' ') >= 0){
text = text.replace(/\s+/, "");
} else {
text = text.replace(/\s+/, "")+", ";
}
if ($('#user_interest').val().indexOf(',') >= 0) {
$('#user_interest').val($('#user_interest').val().replace(text, ""));
} else {
$('#user_interest').val("");
}
$(this).parent().remove();
});
$('.slider').slider({
min: 1,
max: 4,
tooltip: 'hide',
handle: 'round',
selection: 'none'
});
$(".btn-upload").on('click', function(e){
e.preventDefault();
$("#avatar:hidden").trigger('click');
});
$("#avatar").change(function(e) {
e.preventDefault();
MySite.handleFileSelect(e);
});
$('#user_address').change(function(event) {
if($(this).val()) {
$('.dynamic-fields').hide().slideDown();
if($(this).val() === 'Home'){
}
} else {
$('.dynamic-fields').slideUp();
}
});
"first_name" in localStorage && $("input[name='user[first_name]'").val(localStorage.getItem("first_name"));
"last_name" in localStorage && $("input[name='user[last_name]'").val(localStorage.getItem("last_name"));
"age" in localStorage && $("input[name='user[age]'").val(localStorage.getItem("age"));
$("form").submit(function(event) {
if (typeof image !== 'undefined') {
localStorage.setItem("avatar", image);
}
var first_name = $("input[name='user[first_name]'").val();
var last_name = $("input[name='user[last_name]'").val();
var age = $("input[name='user[age]'").val();
localStorage.setItem("first_name", first_name);
localStorage.setItem("last_name", last_name);
localStorage.setItem("age", age);
});
$(".full_name").html(localStorage.getItem("first_name")+" "+ localStorage.getItem("last_name"));
$(".age").html(MySite.get_age(localStorage.getItem("age")));
$(".email").html(localStorage.getItem("email"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment