Navigation Menu

Skip to content
This repository has been archived by the owner on Aug 14, 2018. It is now read-only.

Commit

Permalink
Javascript upgrades, split into admin and frontend and cleaned up lot…
Browse files Browse the repository at this point in the history
…s of functions
  • Loading branch information
andrew committed May 2, 2010
1 parent 6a1c9b3 commit 401c369
Show file tree
Hide file tree
Showing 11 changed files with 845 additions and 6,297 deletions.
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -12,7 +12,9 @@
var AUTH_TOKEN=#{form_authenticity_token.inspect};
= stylesheet_link_tag('application')
= stylesheet_link_tag('print', :media => :print)
= javascript_include_tag 'jquery', 'jquery.validate', 'jquery.cookie', 'jquery.form', 'jquery.hint', 'jquery.livequery', 'ui.core', 'ui.tabs', 'jquery.jgrow', 'application', :cache => true
= javascript_include_tag 'jquery', 'jquery.validate', 'jquery.form', 'jquery.hint', 'jquery.autogrow', 'modernizr', 'application', :cache => 'all'
- if current_user
= javascript_include_tag 'jquery.cookie', 'jquery.ui.core', 'jquery.ui.widget', 'jquery.ui.tabs', 'admin'
- if iphone?
%meta{:name => "viewport", :content => "width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"}
= stylesheet_link_tag 'iphone'
Expand Down
95 changes: 95 additions & 0 deletions public/javascripts/admin.js
@@ -0,0 +1,95 @@
$(document).ready(function(){
// ajax delete
$('a.delete').live('click', function(){
var elem = $(this);
if (confirm("Are you sure?")){
$.post($(this).attr('href'), "_method=delete",function(){
elem.closest('div').slideUp();
});
}
return false;
}).attr("rel", "nofollow");

// new post form tabs
$("#new_post").tabs({cookie: {}});

if ($.cookie("hide_new_post") == 'false') {
$('#new_post').show();
$('a.open').hide();
}
else {
$('#new_post').hide();
$('a.open').show();
}

$('.ui-tabs-nav').bind('tabsselect', function(event, ui) {
$('label.error').remove();
$('a.more').show();
$('div.more').hide();
$('.ui-tabs-nav').hintFields();
});

$('a.open').click(function(){
$('a.open').fadeOut();
$('#new_post').slideDown();
$.cookie("hide_new_post", 'false');
return false;
});

$('a.close').click(function(){
$('#new_post').slideUp(function(){
$('a.open').fadeIn();
});
$.cookie("hide_new_post", 'true');
$('.preview').remove();
return false;
});

// extra post options
$('#new_post div.more').hide();
$('#new_post a.more').click(function(){
$('#new_post div.more').slideDown();
$(this).hide();
return false;
});

// setup validation and ajax post forms
$(".new_post").each(function(){
$(this).validate({
rules: {
'video[video_embed]': {
required: "#video_link_url:blank"
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
beforeSubmit: function(){
$('.new_post .submit').after('<img src="/images/loading.gif" class="loading" />');
$('.new_post :submit').attr('disabled', 'disabled');
},
complete: function(){
$('.loading').remove();
$('.new_post :submit').removeAttr('disabled');
},
success: function(data){
$('.preview').remove();
$('#posts').prepend(data);
if ($('.preview').size() === 0){
$(form).resetForm();
if ($('.pagination').size() > 0){
$('#posts .post:last').remove();
}
}
$(form).hintFields();
},
error:function(request, textStatus, errorThrown) {
var message = (request.status == 401 || request.status == 403) ?
request.responseText : "An unknown error occurred. Support has been contacted.";
alert(message);
}

});
}
});
});
});
114 changes: 13 additions & 101 deletions public/javascripts/application.js
@@ -1,6 +1,6 @@
jQuery.fn.hintFields = function(){
for (var i = 0; i < this.length; i++){
if (!$.browser.safari){ $('input:text', this[i]).hint();}
for (var i = 0, l = this.length; i < l; i++){
if (!Modernizr.input.placeholder){ $('input:text', this[i]).hint();}
$('textarea', this[i]).hint();
}
};
Expand All @@ -12,6 +12,10 @@ $(document).ready(function(){
ignoreTitle: true
});

if ($.browser.webkit){
$('input[type="search"]').attr({autosave: location.host, results: 10})
}

// rails ajax setup
jQuery.ajaxSetup({
'beforeSend': function(xhr){ xhr.setRequestHeader("Accept","text/javascript"); }
Expand All @@ -25,117 +29,21 @@ $(document).ready(function(){
settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

// ajax delete
$('a.delete').livequery('click', function(){
var elem = $(this);
if (confirm("Are you sure?")){
$.post($(this).attr('href'), "_method=delete",function(){
elem.closest('div').slideUp();
});
}
return false;
}).attr("rel", "nofollow");

// open external links in a new window
$('a[rel="external"]').click( function(){
window.open($(this).attr('href'));
return false;
});

// new post form tabs
$("#new_post > ul").tabs({cookie: {}});

if ($.cookie("hide_new_post") == 'false') {
$('#new_post').show();
$('a.open').hide();
}
else {
$('#new_post').hide();
$('a.open').show();
}

$('.ui-tabs-nav').bind('tabsselect', function(event, ui) {
$('label.error').remove();
$('a.more').show();
$('div.more').hide();
$('.ui-tabs-nav').hintFields();
});

$('a.open').click(function(){
$('a.open').fadeOut();
$('#new_post').slideDown();
$.cookie("hide_new_post", 'false');
return false;
});

$('a.close').click(function(){
$('#new_post').slideUp(function(){
$('a.open').fadeIn();
});
$.cookie("hide_new_post", 'true');
$('.preview').remove();
return false;
});

// extra post options
$('#new_post div.more').hide();
$('#new_post a.more').click(function(){
$('#new_post div.more').slideDown();
$(this).hide();
return false;
});

// text field hints
$('form').hintFields();


// slide open/closed the formatting help div
$('a.help').click(function(){
$('#formatting').toggle(100);
return false;
});

// expanding textareas
$('textarea').jGrow();

// setup validation and ajax post forms
$(".new_post").each(function(){
$(this).validate({
rules: {
'video[video_embed]': {
required: "#video_link_url:blank"
}
},
submitHandler: function(form){
$(form).ajaxSubmit({
beforeSubmit: function(){
$('.new_post .submit').after('<img src="/images/loading.gif" class="loading" />');
$('.new_post :submit').attr('disabled', 'disabled');
},
complete: function(){
$('.loading').remove();
$('.new_post :submit').removeAttr('disabled');
},
success: function(data){
$('.preview').remove();
$('#posts').prepend(data);
if ($('.preview').size() == 0){
$(form).resetForm();
if ($('.pagination').size() > 0){
$('#posts .post:last').remove();
}
}
$(form).hintFields();
},
error:function(request, textStatus, errorThrown) {
var message = (request.status == 401 || request.status == 403) ?
request.responseText : "An unknown error occurred. Support has been contacted.";
alert(message);
}

});
}
});
});

// setup validation and ajax comments forms
$(".new_comment").validate({
submitHandler: function(form){
Expand All @@ -148,14 +56,18 @@ $(document).ready(function(){
$(form).resetForm().hintFields();
},
error:function(request, textStatus, errorThrown) {
var message = (request.status == 401 || request.status == 403) ?
var message = (request.status == 401 || request.status == 403 || request.status == 406) ?
request.responseText : "An unknown error occurred. Support has been contacted.";
alert(message);
if (request.status == 406) {
$(form).resetForm().hintFields();
}
}
});
}
});

// fade out flash
setTimeout(function(){$(".flash").fadeOut(1000);},10000);

});

0 comments on commit 401c369

Please sign in to comment.