Skip to content

Instantly share code, notes, and snippets.

@webdawe
Created July 15, 2016 06:29
Show Gist options
  • Save webdawe/55db5c3795598496c959d4d10e9ae154 to your computer and use it in GitHub Desktop.
Save webdawe/55db5c3795598496c959d4d10e9ae154 to your computer and use it in GitHub Desktop.
Script for Start Reading
jQuery(document).ready(
function($)
{
var isRandomize = 0;
var randomize;
var retrievedData = localStorage.getItem("sr_extraitems");
var extraItems = JSON.parse(retrievedData);
if (extraItems == undefined)
{
var extraItems = [];
}
else
{
showExtraCollections();
}
function showExtraCollections()
{
for (var i = 0; i< extraItems.length ; i++)
{
if (extraItems[i] !='')
{
$('.portfolio-items').append(extraItems[i]);
}
}
}
$(document).on('click', '.portfolio-item',
function()
{
$(this).find('.word-link').trigger('click');
return false;
}
);
$(document).on('click', '.word-link',
function()
{
var words = $(this).attr('data-words');
var wordsArray = words.split(',');
var colour = $(this).attr('data-colour');
var wordsLi = '';
var trashLink = '';
isRandomize = 0;
for (var i=0;i < wordsArray.length;i++)
{
wordsLi += '<li><div id="word-'+ i+'"><span>'+ wordsArray[i] +'</span></div></li>';
}
if ($(this).attr('data-item-index') != undefined)
{
trashLink = ' | <a href="#" class="fa fa-trash-o trash-item" data-item-index="' + $(this).attr('data-item-index')+ '"></a>';
}
$('#show-words').html('<h2>' + $(this).prev('h3').html() + '<span>' +wordsArray.length + ' Words</span></h2><p><a href="#" class="fa fa-random randomize"></a> ' + trashLink+'| <a href="#" class="fa fa-times-circle close-words"></a></p>').append($('<ul></ul>')
.append(wordsLi));
$('#show-words').css('background', $(this).attr('data-colour'));
//textFillLi();
$('#show-words').slideDown('slow');
return false;
}
);
$(document).on('click', 'a.trash-item',
function()
{
extraItems[$(this).attr('data-item-index')]= '';
localStorage.setItem("sr_extraitems", JSON.stringify(extraItems));
$('.portfolio-item[data-item-index="'+ $(this).attr('data-item-index')+'"]').remove();
$('a.close-words').trigger('click');
}
);
$(document).on('click', 'a.randomize',
function()
{
$('#show-words').find('ul').hide('slow');
isRandomize = 1;
clearInterval(randomize);
randomizeWords();
randomize = setInterval(randomizeWords, 3000);
return false;
}
);
function randomizeWords()
{
var randomIndex = getRandomInt(0, $("#show-words li" ).length - 1);
if(isRandomize == 1)
{
$('#random-word').html('').hide();
$('#random-word').html($('#word-'+ randomIndex).html());
$('#random-word').show();
}
}
$(document).on('click', 'a.close-words',
function()
{
if(isRandomize == 1)
{
$('#random-word').hide()
clearInterval(randomize);
isRandomize = 0;
$('#show-words').find('ul').show('slow');
}
else
{
isRandomize = 0;
$('#show-words').html('');
$('#show-words').slideUp('slow');
}
return false;
}
);
$('.show-collection-form').click(
function()
{
$("#collection-form").show('slow');
$(this).hide();
}
);
$('.create-collection').click(
function()
{
$("#collection-form").hide('slow');
addCollection($('#colour').val(), $('#collection-name').val(), $('#collection-words').val());
$('.show-collection-form').show();
}
);
function addCollection(colour,collectionName,words)
{
var collectionItem = '<div class="portfolio-item" style="background:'+ colour +'" data-item-index="'+ extraItems.length+'"><div class="portfolio-item-inner"><div class="portfolio-info"><h3>'+ collectionName+'</h3><a class="preview word-link" data-words="'+ words +'" data-colour="'+ colour +'" data-item-index="'+ extraItems.length+'" href="#"><i class="fa fa-eye"></i></a></div></div></div>';
extraItems[extraItems.length]= collectionItem;
localStorage.setItem("sr_extraitems", JSON.stringify(extraItems));
$('.portfolio-items').append(collectionItem);
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
$('.flatcolorpicker').flatcolorpicker({
});
// process the form
$('#custom-collection-form-id').submit(function(event) {
var formData = $(this).serialize();
// process the form
$.ajax({
type : 'POST',
url : 'generate-collection-url.php',
data : formData,
dataType : 'json',
encode : true
})
// using the done promise callback
.done(function(data) {
// log data to the console so we can see
if ( ! data.success) {
// handle errors for name ---------------
if (data.errors.name) {
$('.name-group').addClass('has-error'); // add the error class to show red input
$('.name-group').append('<div class="help-block">' + data.errors.name + '</div>'); // add the actual error message under our input
}
// handle errors for email ---------------
if (data.errors.colour) {
$('.colour-group').addClass('has-error'); // add the error class to show red input
$('.colour-group').append('<div class="help-block">' + data.errors.colour + '</div>'); // add the actual error message under our input
}
// handle errors for email ---------------
if (data.errors.words) {
$('.words-group').addClass('has-error'); // add the error class to show red input
$('.words-group').append('<div class="help-block">' + data.errors.words + '</div>'); // add the actual error message under our input
}
} else {
var facebookShareUrl = 'https://www.facebook.com/dialog/share?app_id=198981276787061&display=popup&href=' + data.message + '&redirect_uri=http://startreading.com.au';
// ALL GOOD! just show the success message!
$('#custom-collection-form-id').prepend('<div class="alert alert-success">Save this url for future use <a href="' + data.message + '">' + data.message + '</a><a href="' +facebookShareUrl + '" target="_blank"><img src="http://startreading.com.au/images/facebook_button.png"><a></div>');
}
});
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment