/* Model * * Model for JSON data */ (function (exports) { "use strict"; // the model for the Media Sample Data // {Object} appSettings are the user-defined settings from the index page var JSONMediaModel = function (appSettings) { this.mediaData = []; this.categoryData = []; this.currData = []; this.currentCategory = 0; this.currSubCategory = null; this.currentItem = 0; this.defaultTheme = "default"; this.currentlySearchData = false; this.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; /** * This function loads the initial data needed to start the app and calls the provided callback with the data when it is fully loaded * @param {function} the callback function to call with the loaded data */ this.loadInitialData = function (dataLoadedCallback) { $.ajax({ url: appSettings.dataURL, type: 'GET', crossDomain: true, dataType: 'json', context: this, cache: true, success: function () { var contentData = arguments[0]; this.handleJsonData(contentData); }, error: function () { console.log("error loading data:" + arguments); }, complete: function () { dataLoadedCallback(); } }); }.bind(this); /** * Handles requests that contain json data * @param {Object} jsonData data returned from request */ this.handleJsonData = function (jsonData) { this.currentCategory = 1; // Build categoryData array // Services[dynamic], CurrentSeries, PastSeries this.categoryData = []; this.categoryDataIndex = 0; if (jsonData["Services"] !== undefined && jsonData["Services"] !== null) { // Code to execute if "Services" property is undefined or null try { // Process Services if (typeof (jsonData["Services"].ActiveCampusList) !== "undefined") { // Process ActiveCampusList for (var videoIndex in jsonData["Services"].ActiveCampusList) { var currentCategory = jsonData["Services"].ActiveCampusList[videoIndex].Title.replace(/\s/g, ""); this.categoryData[this.categoryDataIndex] = {}; this.categoryData[this.categoryDataIndex].key = jsonData['Services'].ActiveCampusList[videoIndex].Title.replace(/\s/g, ""); this.categoryData[this.categoryDataIndex].title = jsonData['Services'].ActiveCampusList[videoIndex].BreadCrumbText; // Add the media data this.mediaData.push(this.createMedia(jsonData['Services'].ActiveCampusList[videoIndex].Details, currentCategory)); // Update Index this.categoryDataIndex++; } } } catch { console.log("Error processing Services.ActiveCampusList"); } } if (jsonData["CurrentSeries"] !== undefined && jsonData["CurrentSeries"] !== null) { // Code to execute if "CurrentSeries" property is undefined or null var currentCategory = "CurrentSeries"; try { // Process CurrentSeries this.categoryData[this.categoryDataIndex] = {}; this.categoryData[this.categoryDataIndex].key = currentCategory; this.categoryData[this.categoryDataIndex].title = jsonData[currentCategory].BreadCrumbText; // add title for current series this.categoryData[this.categoryDataIndex].seriesTitle = jsonData[currentCategory].Title; // process any sermons if (typeof (jsonData[currentCategory].Sermons) !== "undefined") { for (var videoIndex in jsonData[currentCategory].Sermons) { this.mediaData.push(this.createMedia(jsonData[currentCategory].Sermons[videoIndex], currentCategory)); } } // Update Index this.categoryDataIndex++; } catch { console.log("Error processing CurrentSeries"); } } if (jsonData["PastSeries"] !== undefined && jsonData["PastSeries"] !== null) { // Code to execute if "PastSeries" property is undefined or null var currentCategory = "PastSeries"; try { // Process PastSeries this.categoryData[this.categoryDataIndex] = {}; this.categoryData[this.categoryDataIndex].key = currentCategory; this.categoryData[this.categoryDataIndex].title = jsonData[currentCategory].BreadCrumbText; // process any series if (typeof (jsonData[currentCategory].Series) !== "undefined") { for (var seriesIndex in jsonData[currentCategory].Series) { this.mediaData.push(this.createSubcategory(jsonData[currentCategory].Series[seriesIndex], currentCategory)); } } // Update Index this.categoryDataIndex++; } catch { console.log("Error processing PastSeries"); } } }.bind(this); /*************************** * * Utility Methods * ***************************/ /** * Sort the data array alphabetically * This method is just a simple sorting example - but the * data can be sorted in any way that is optimal for your application */ this.sortAlphabetically = function (arr) { arr.sort(); }; /** * Convert unix timestamp to date * @param {Number} d unix timestamp * @return {Date} */ this.unixTimestampToDate = function (d) { var unixTimestamp = new Date(d * 1000); var year = unixTimestamp.getFullYear(); var month = this.months[unixTimestamp.getMonth()]; var date = unixTimestamp.getDate(); var hour = unixTimestamp.getHours(); var minute = unixTimestamp.getMinutes(); var second = unixTimestamp.getSeconds(); return date + ',' + month + ' ' + year + ' ' + hour + ':' + minute + ':' + second; }; this.createMedia = function (video, category) { var media = {}; media.description = video.Description; media.id = video.Id; media.imgURL = video.ImageUrl; media.pubDate = video.MediaDate; media.thumbURL = video.VideoThumbUrl; media.title = video.Title; media.videoURL = video.VideoUrl; if (category) { media.categories = [category]; } return media; }; this.createSubcategory = function (subcat, category) { var media = {}; media.description = subcat.Description; media.id = subcat.Id; media.imgURL = subcat.Image; media.thumbURL = subcat.Image; media.title = subcat.Title; media.type = "subcategory"; media.categories = [category]; media.contents = []; for (var videoIndex in subcat.Sermons) { media.contents.push(this.createMedia(subcat.Sermons[videoIndex])); } media.contents.sort(this.sortByDateAscending); return media; }; this.sortByDateAscending = function (obj1, obj2) { var date1 = new Date(obj1.pubDate), date2 = new Date(obj2.pubDate); if (date1 > date2) return 1; if (date1 < date2) return -1; return 0; }; /*************************** * * Media Data Methods * ***************************/ /** * For single views just send the whole media object */ this.getAllMedia = function () { return mediaData; }, /*************************** * * Category Methods * ***************************/ /** * Hang onto the index of the currently selected category * @param {Number} index the index into the categories array */ this.setCurrentCategory = function (index) { this.currentCategory = index; }; /** * Function to set the current subcategory object, this be used to return the subcategory resuts in the getSubCategory method * which can be modified in the model before being returned asynchrounously if the model wishes. * @param {Object} the currently selected subcategory object */ this.setCurrentSubCategory = function (data) { this.currSubCategory = data; }; /*************************** * * Content Item Methods * ***************************/ /** * Return the category items for the left-nav view */ this.getCategoryItems = function () { return this.categoryData; }; /** * Get and return data for a selected category * @param {Function} categoryCallback method to call with returned requested data */ this.getCategoryData = function (categoryCallback) { this.currData = []; for (var i = 0; i < this.mediaData.length; i++) { if ($.inArray(this.categoryData[this.currentCategory].key, this.mediaData[i].categories) > -1) { this.currData.push(this.mediaData[i]); } } categoryCallback(this.currData); }; /** * Get and return data for a selected sub category, modified however the model wishes. Uses an asynchrounous callback to return the data. * @param {Function} categoryCallback method to call with returned requested data */ this.getSubCategoryData = function (subCategoryCallback) { // clone the original object subCategoryCallback(this.currSubCategory); }; /** * Get and return data for a search term * @param {string} term to search for * @param {Function} searchCallback method to call with returned requested data */ this.getDataFromSearch = function (searchTerm, searchCallback) { this.currData = []; for (var i = 0; i < this.mediaData.length; i++) { if (this.mediaData[i].title.toLowerCase().indexOf(searchTerm) >= 0 || this.mediaData[i].description.toLowerCase().indexOf(searchTerm) >= 0) { this.currData.push(this.mediaData[i]); } } searchCallback(this.currData); }; /** * Get and return data for a search term * @param {string} term to search for * @param {Function} searchCallback method to call with returned requested data */ this.getDataFromSearch = function (searchTerm, searchCallback) { this.currData = []; for (var i = 0; i < this.mediaData.length; i++) { if (this.mediaData[i].title.toLowerCase().indexOf(searchTerm) >= 0 || this.mediaData[i].description.toLowerCase().indexOf(searchTerm) >= 0) { this.currData.push(this.mediaData[i]); } } searchCallback(this.currData); }; /** * Store the refrerence to the currently selected content item * @param {Number} index the index of the selected item */ this.setCurrentItem = function (index) { this.currentItem = index; this.currentItemData = this.currData[index]; }; /** * Retrieve the reference to the currently selected content item * @param {Number} index the index of the selected item */ this.getCurrentItemData = function () { return this.currentItemData; }; }; exports.JSONMediaModel = JSONMediaModel; })(window);