Skip to content

Instantly share code, notes, and snippets.

@coquer
Created June 4, 2015 15:54
Show Gist options
  • Save coquer/d4f309bcb134b3e73507 to your computer and use it in GitHub Desktop.
Save coquer/d4f309bcb134b3e73507 to your computer and use it in GitHub Desktop.
Simple Widget
function proccessContent(){
/**
* Read JSON file to include as script
* @return {[void]} triggers function to populate content
*/
this.readFile = function (){
if (window.File && window.FileReader && window.FileList && window.Blob) {
var json_file_url = "http://blog.jorgerodriguez.dk/wp-content/themes/jycr753/json/reviews.json";
$.ajax({
url: json_file_url,
dataType: "script",
success: function(data, textStatus){
if (typeof reviews !== 'undefined'){
var content_object = new proccessContent();
content_object.populateContent(reviews);
}else{
throw "Error in file";
}
}
});
} else {
console.log('The File APIs are not fully supported in this browser.');
}
};
/**
* Populate content if reviews object is not undefined
* @param {[object]} _reviews
*/
this.populateContent = function(_reviews){
var content_id = {
header_review : document.getElementById('header_review'),
content_review : document.getElementById('content_review')
};
var review_object = {
first_name: null,
lastname : null,
full_name: null,
location: null,
review_title : null,
review_body: null,
start_rating: null,
rating_image_src: null,
profile_pic : null
};
var content_object = new proccessContent();
for(var loop_c = 0; loop_c < _reviews.length; loop_c ++){
review_object.first_name = $.trim(_reviews[loop_c].firstName);
review_object.lastname = $.trim(_reviews[loop_c].lastName);
review_object.full_name = $.trim(_reviews[loop_c].fullName);
review_object.location = $.trim(_reviews[loop_c].location);
review_object.review_title = $.trim(_reviews[loop_c].reviewTitle);
review_object.review_body = $.trim(_reviews[loop_c].reviewBody);
review_object.start_rating = $.trim(_reviews[loop_c].starRating);
review_object.rating_image_src = content_object.getRatingImage(review_object.start_rating);
review_object.profile_pic = content_object.getProfileImage(review_object.first_name);
var content_html = "<div class=\"panel panel-default\">"
+"<div class=\"panel-heading\">"
+"<h3 class=\"panel-title\">"
+ review_object.first_name
+"<span>"
+"<img src="
+review_object.rating_image_src
+ " alt=\"image\"/>"
+"</span>"
+"</h3>"
+"</div>"
+"<div class=\"show_more\">"
+"<div class=\"icon\">"
+"<div class=\"plus\"></div>"
+"</div>"
+"</div>"
+"<div class=\"show_content\">"
+"<div class=\"profile_pic\" style=\"background-image:url("+review_object.profile_pic+")\"></div>"
+"<h2>"
+review_object.full_name
+"</h2>"
+"<label>"
+review_object.location
+"</label>"
+"<p>"
+ review_object.review_body
+"</p>"
+"</div>"
+"<div class=\"panel-body\">"
+ review_object.review_title
+"</div>"
+"</div>";
$(content_id.content_review).append(content_html);
}
};
/**
* Get profile image depending on person's name
* @param {[string]} _name person name from document
* @return {[string]} obsolute path for file
*/
this.getProfileImage = function(_name){
var p_name = _name.toLowerCase();
var _URL = "http://blog.jorgerodriguez.dk/wp-content/themes/jycr753/frame/images/"
var profile_images = new Array();
profile_images[0] = _URL+'simon-lock.png';
profile_images[1] = _URL+'jason-wright.png';
profile_images[2] = _URL+'hugo-beja.png';
profile_images[3] = _URL+'gav.png';
profile_images[4] = _URL+'erika-wolfe.png';
switch(p_name){
case 'simon':
return profile_images[0];
break;
case 'justin':
return profile_images[1];
break;
case 'hugo':
return profile_images[2];
break;
case 'gav':
return profile_images[3];
break;
case 'erika':
return profile_images[4];
break;
default:
return profile_images[0];
break;
}
};
/**
* Get rating image
* @param {[string]} _rating Rating for current post
* @return {[string]} obsolute path for file
*/
this.getRatingImage = function(_rating) {
var image_array = new Array();
var _URL = "http://blog.jorgerodriguez.dk/wp-content/themes/jycr753/frame/images/"
image_array[0] = _URL+'1-star-260x48.png';
image_array[1] = _URL+'2-stars-260x48.png';
image_array[2] = _URL+'3-stars-260x48.png';
image_array[3] = _URL+'4-stars-260x48.png';
image_array[4] = _URL+'5-stars-260x48.png';
switch(_rating){
case '0':
return image_array[0];
break;
case '1':
return image_array[1];
break;
case '2':
return image_array[2];
break;
case '3':
return image_array[3];
break;
case '4':
return image_array[4];
break;
default:
return image_array[0];
break;
}
};
}
(function(){
var x = new proccessContent();
x.readFile();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment