<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script>
<script src="//jashkenas.github.io/backbone/backbone-min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="inputHolder">
<div class="messageBubble">
<div class="formLabel">Zip Code</div>
<input class="input" value="00000">
<div class="optionDropDown">
</div>
</div>
<div class="value"></div>
</div>
</body>
</html>
#qunit-tests{
height: 450px;
overflow: scroll;
}
#x{
position:fixed;
top:0px;
background-color: #0d3349;
color: #8699a4;
border-radius:5px;
padding: 0.5em 0.5em;
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
cursor:pointer;
margin:10px;
}
#qunit{
position:fixed;
bottom:0px;
width:100%;
height:600px;
left:0px;
}
#fails{
position: absolute;
top: -5px;
right: -5px;
background-color: red;
border-radius: 15px;
height: 15px;
width: 19px;
font-size: 9px;
text-align: center;
color: #fff;
padding-top: 3px;
}
/* Reset Styles */
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
div {
outline: none;
}
/* Main Styles */
#ca_container{
width:100%;
margin: 0 auto;
}
html,body,#ca_container{
height: 100%;
min-height: 100%;
position: relative;
}
body {
line-height: 1;
margin: 0 auto;
text-align: center;
font-size: medium;
font-family: helvetica;
height: 100%;
}
#ca_container {
margin: 0 auto;
text-align: center;
display: block;
}
h1{
font-size: 3em;
}
.messageBubble {
background-color: #fff;
width: 12em;
min-height: 3em;
line-height: 3em;
font-size: 1em;
border: 0px solid #999;
box-shadow: 0px 5px 18px rgba(0,0,0,0.2);
margin: 0 auto;
z-index:2;
}
.step,.header{
position: fixed;
}
.stepLine {
height: .25em;
width: 100%;
border-radius: 1em;
text-align: center;
background-color: #fff;
vertical-align: middle;
line-height: 2em;
bottom: 2em;
font-weight: bold;
position: fixed;
}
.ca_step{
height: 2em;
width: 2em;
border-radius: 1em;
text-align: center;
background-color: #fff;
vertical-align: middle;
line-height: 2em;
bottom: 2em;
font-weight: bold;
}
.ca_step .label{
line-height: 1em;
text-align: center;
width: 3em;
font-size: .75em;
}
.logo{
display: block;
text-indent: -9999px;
width: 100%;
height: 84px;
/* background-size: 534px 84px;*/
background-color: #fff;
background-repeat: no-repeat;
background-position: center center;
background-image: url(../images/trade_up.jpg);
/*background: url(../images/trade_up.svg);*/
}
.warning{
top:0px;
background-color: #fff;
color:#ff0000;
z-index: 10;
width: 100%;
height: 35px;
}
.intro {
font-size: 1em;
line-height: 1.5em;
width: 50%;
text-align: center;
margin: 0 auto;
padding: 1.15em;
}
.formLabel {
background-color: #8FBAC0;
height: 1.25em;
font-size: 0.35em;
position: absolute;
width: 6em;
line-height: 1.25em;
text-transform: uppercase;
letter-spacing: 0.15em;
left: -.1em;
color: #fff;
border-radius: 1.25em 0 0 1.25em;
text-indent: 0em;
box-shadow: inset -19px 1px 32px -21px rgba(0,0,0,0.2);
box-shadow: inset -19px 1px 32px -21px rgba(0,0,0,0.2);
box-shadow: inset -19px 1px 32px -21px rgba(0,0,0,0.2);
}
.input{
width: 12em;
right: 0em;
position: absolute;
color: #666;
outline: none;
background-color: transparent;
border: none;
font-size: 1em;
text-align: center;
line-height: 3em;
}
.value {
display: block;
top: -2.5em;
left: 8em;
width: 100%;
color: #999;
background-color: green;
height: 2em;
width: 2em;
border-radius: 1.25em;
text-align: center;
margin: 0 auto;
z-index: 1;
min-height: 2em;
line-height: 2em;
font-size: 1em;
}
.optionDropDown {
position: absolute;
width: 12em;
display: block;
top: 3em;
background-color: #fff;
outline: 2px solid #eee;
max-height: 14em;
overflow: scroll;
}
.option{
display: block;
border-top: 1px solid #ccc;
line-height: 1.25em;
width: 12em;
right: 0em;
color: #666;
outline: none;
background-color: transparent;
border: none;
font-size: 1em;
text-align: center;
line-height: 1.5em;
}
.option:last-child{
display: block;
border-bottom: 1px solid #ccc;
}
.option:hover{
background-color: #e5e5e5;
}
.inputHolder,.value,.warning,.messageBubble,body,.option{
position: relative;
}
.highlight{
background-color: #ccc;
}
$(document).ready(function(){
$('.warning').remove();
/* Session Model */
var Session = Backbone.Model.extend({
urlRoot : 'rest/rest/checkZip',
methodToURL: {
create: 'rest/rest/checkZip'
},
sync: function(method, model, options) {
options = options || {};
var end = '';
if(method == "create"){
end = "?zip="+$('.input').val();
};
options.url = model.methodToURL[method.toLowerCase()]+end;
Backbone.sync(method, model, options);
}
});
/* Session View */
var SessionUser = Backbone.View.extend({
events:{
'click .messageBubble': 'checkZip'
},
checkZip: function(ev,e){
if(this.validate(ev)){
var zipCheck = new Session({zip:parseInt(e.currentTarget.value)});
$('.messageBubble').css('backgroundColor','#fff');
} else {
$('.messageBubble').css('backgroundColor','rgba(255,0,0,.3)');
return;
};
var options = '';
response = [
{ZipCode:21654,State:'MD',City:'Oxford'},
{ZipCode:33442,State:'FL',City:'Deerfield Beach'},
{ZipCode:90210,State:'CA',City:'Hollywood'}
];
for (var i = response.length-1; i > -1; i--) {
console.log(response,i);
options += '<div class="option" zip="'+response[i].ZipCode+'" state="'+response[i].State+'" city="'+response[i].City+'">'+response[i].ZipCode+'</div>';
};
$('.optionDropDown').html('').append(options);
/*
This would request the restful api and return back a list of the
the most relevant zip codes to what you searched, I hard coded the response
here to show the functionality
*/
// zipCheck.save({},{
// wait:true,
// success: function(model, response, options) {
// if(typeof response != 'undefined'){
// console.log(response,model,response);
// $('.value').text(response[0].City+', '+response[0].State);
// var options = '';
// for (var i = response.length-1; i > 0; i--) {
// options += '<div class="option" zip="'+response[i].ZipCode+'" state="'+response[i].State+'" city="'+response[i].City+'">'+response[i].ZipCode+'</div>';
// };
// $('.optionDropDown').html('').append(options);
// } else {
// $('.value').text("We didn't recognize that zip code");
// };
// //$('.value').text(response[0]\.city+', '+response[0].state);
// },
// error: function(model, error) {
// /* The user does not have a session/valid session. */
// console.log(error);
// }
// });
return false;
},
validate : function(num){
var num = num.split('');
var regex = new RegExp(/^[\d-\s]/);
for (var i = 0; i < num.length; i++) {
if(regex.test(num[i]) == false){
return false;
};
};
return true;
},
validatePressed: function(){
/* We weren't intereseted in some characters, since
this was looking for zip codes */
var checked = arguments[0];
for (var i = arguments.length-1; i != 1; i--) {
var negativeCheck = false;
if(arguments[i].indexOf('!') != -1){
arguments[i] = arguments[i].substring(1);
var negativeCheck = true;
};
if(constants.keys[arguments[i]] == checked){
if(negativeCheck){
return true; // matched an invalid key
};
return true; // matched a valid key
};
};
return false;
},
checkPressed : function(max,e,press){
if(constants.bodyListener.active){
return
};
var keys = constants.keys,
pshBtn = e.which,
parTarg = $('input'),
leng = parTarg.val().length;
if(press == 'keyup'){
this.checkZip(parTarg.val(),e);
return;
};
if( true !== (this.validatePressed(pshBtn,'!up','!down','!left','!right','!delete','enter')) && leng > max){
e.preventDefault();
} else if(pshBtn !== keys.up){
if(this.validatePressed(pshBtn,'enter','down','tab')){
parTarg.blur();
constants.bodyListener.active = true;
} else if(this.validatePressed(pshBtn,'!right','!left')){
$('.optionDropDown').html('');
};
};
},
jumpOptions : function(e,pos,value){
if(!constants.bodyListener.active){
/* You aren't going through your current options */
return;
};
var keys = constants.keys,
pshBtn = e.which,
targ = $('.highlight'),
parTarg = $('.input');
if(targ.length == 0){
parTarg.next('.optionDropDown').children('div').first().addClass('highlight');
return;
};
parTarg.blur();
if((pshBtn == keys.down || pshBtn == keys.tab) && targ.next('div').length !== 0){
//Hit down // tab && not on last option
targ.removeClass('highlight').next('div').addClass('highlight');
} else if (pshBtn == keys.up && targ.prev('div').length == 0) {
//Hit up on the first option
parTarg.focus();
targ.removeClass('highlight');
constants.bodyListener.active = false;
} else if (pshBtn == keys.up && targ.prev('div').length !== 0) {
//Hit up on not the top one
targ.removeClass('highlight').prev('div').addClass('highlight');
} else if (pshBtn == keys.enter){
//Hit enter
constants.bodyListener.active = false;
constants.zip = targ.attr('zip'),
constants.state = targ.attr('state'),
constants.city = targ.attr('city');
$('.value').text(constants.city+', '+constants.state+' '+constants.zip);
$('.input').val(constants.zip).focus();
$(".optionDropDown").html('');
};
var height = $(".highlight").height();
var pos = $(".highlight").index();
$(".optionDropDown").stop(false,true).animate({scrollTop: pos*height},500);
}
});
var constants = {
zip : '',
state : 'ca',
city: '',
views : new SessionUser(),
max: {
zip:4}
,
keys:{
enter:13,
down:40,
up:38,
left:37,
right:39,
tab:9,
delete:8,
backspace:8
},
bodyListener:{
active:false
}
};
$('body').keydown(function(e){
constants.views.jumpOptions(e,'454');
});
$('.input').keydown(function(e){
constants.views.checkPressed(constants.max.zip, e,'keydown');
});
$('.input').keyup(function(e){
constants.views.checkPressed(constants.max.zip, e,'keyup');
});
$('.input').on('click',function(){
if($('.input').val() == 00000){
$('.input').val('');return;
}; // Initial reset (Make this into a click / unbind?)
$(this).unbind('click');
});
});
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |