var $StoryStream = {}; //Global var to contain story stream logic $StoryStream.stories = []; //Container array for cached content items. //Will be filled continuously with story objects. $StoryStream.stories_to_print = []; //Container array for content items about to be appended to the DOM. $StoryStream.today = new Date(); //Today's date, will use it to generate initial Month/Year values. $StoryStream.month = $StoryStream.today.getMonth(); $StoryStream.year = $StoryStream.today.getFullYear(); $StoryStream.is_finished = false; //Flag to tell whether or not to keep trying to append stories. $StoryStream.scroll_continue_done = true; //Whether the continue_stream from a scroll is finished $StoryStream.position = 0; //Current position in the global feed array $StoryStream.limit = 10; //Number of stories to display at a time $StoryStream.filter = ['entry', 'article', 'tweet', 'image', 'video', 'poll']; // The types to include $StoryStream.topic = "clinton"; $StoryStream.container; $StoryStream.print_type = 'stories'; // Can also be 'widget' $StoryStream.fail_count = 0; // Counts how many 404s have happened $StoryStream.fail_limit = 24; // How many 404s until it quits. $StoryStream.date_checker = [];//keeps track of months pulled to prevent the same month from being pulled $StoryStream.item_tracker = {};//keeps track of double added items to json archives $StoryStream.month_num_to_text = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; $StoryStream.finished_callback = function() { // Called when finished loading (and 404 limit reached) // Meant to be overridden }; $StoryStream.content_filter = function(type_array) { //Filter feed content //Take a content type array //and refresh DOM items based on the new rules. this.filter = type_array; this.visible = 0; this.position = 0; this.container.html(""); this.is_finished = false; this.continue_stream(); }; $StoryStream.fetch_article = function() { //Gets the JSON archive at /json/trending_articles.json //YYYY and MM are determined by $StoryStream.year and $StoryStream.month //Then, decrement month so next call will grab the previous month. var dateparts = new Date(this.year,this.month,01).toISOString().split("-"); $.ajax({ dataType: 'json', url: "/json/trending_articles.json", context: this, success: function(data, textStatus, jqXHR) { this.decrement_month(); //Prepare for the next month for(var i=0;i= this.fail_limit) { this.is_finished=true; this.finished_callback(); } else { this.decrement_month(); this.fetch_and_continue_stream(); } }; $StoryStream.print_stories = function(stories_array) { //Adds entries to DOM var story_strings = []; var display = "block"; for(var i=0; i" ); var heading1 = typeof(stories_array[i].heading1)!=="undefined"?stories_array[i].heading1:""; var heading2 = typeof(stories_array[i].heading2)!=="undefined"?stories_array[i].heading2:""; var date = typeof(stories_array[i].date)!=="undefined"?stories_array[i].date:""; var content_url = typeof(stories_array[i].content_url)!=="undefined"?stories_array[i].content_url:""; var img_url = typeof(stories_array[i].img_url)!=="undefined"?stories_array[i].img_url:""; var body = typeof(stories_array[i].body)!=="undefined"?stories_array[i].body:""; var type = typeof(stories_array[i].type)!=="undefined"?stories_array[i].type:""; var tags = typeof(stories_array[i].tags)!=="undefined"?stories_array[i].tags:""; var score = typeof(stories_array[i].score)!=="undefined"?stories_array[i].score:""; story_strings.push("

"+type+": "+ heading1+" - "+date+"

"); if(img_url) { story_strings.push(""); } story_strings.push(""); } } this.container.append(story_strings.join("")); }; $StoryStream.print_stories_widget = function(stories_array) { var story_strings = []; for(var i=0; i'); story_strings.push('
'); story_strings.push('
'); if(type == 'tweet') { story_strings.push(''); story_strings.push(''); } else { story_strings.push(''); } if(img_url && type != 'tweet') { story_strings.push(''+heading1+''); } story_strings.push('
'); story_strings.push(''); } } this.container.append(story_strings.join("")); }; $StoryStream.decrement_month = function() { var newdate = new Date(this.year, this.month-1, 1); this.year = newdate.getFullYear(); this.month = newdate.getMonth(); }; $StoryStream.continue_stream = function() { //Append items from the feed, up to 10 for(var i=this.position; i= this.limit) { break; } //Quit the loop early if enough are found } //Otherwise get the next feed and repeat // (unless there are no more feeds) if(!this.is_finished && this.stories_to_print.length < this.limit) { this.fetch_and_continue_stream(); } else { this.scroll_continue_done = true; } if(this.print_type == 'stories') { this.print_stories(this.stories_to_print); //Prints and increments "visible" } else if(this.print_type == 'widget') { this.print_stories_widget(this.stories_to_print); //Prints and increments "visible" } this.stories_to_print = []; this.finished_callback(); };