Академический Документы
Профессиональный Документы
Культура Документы
*
*
-------------------------------------------------------------------------*
## Astrology - A javascript class for sharing astrological information ##
*
-----------------------------------------------------------------------*
*
Author: Johan Wedfelt
*
=====================
*
Project Repo: https://github.com/DaWoody/Astrology-JS.git
*
============
*
Version 0.4
*
============
*
License Url: http://opensource.org/licenses/GPL-3.0
*
============
*
Description:
*
============
*
A javascript class that initiates by loading some Astrological data to t
he instantiated class
*
then there are a number of public methods available to do searches on bi
rthdate etc to get
*
astrological information back to be printed or used within a DOM structu
re on a web-page
*
*
*/
var Astrology = function(connectionObject){
//On class initiation
this.init(connectionObject);
};
Astrology.prototype.xhrAndLoadJsonToDataArray = function(url, variable){
//XHR request help method for loading the data
var _that = this,
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
var response = xhr.response;
_that.zodiacSignsData[variable] = response[variable];
}
}
};
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.send();
};
Astrology.prototype.addMonth = function(monthNumberOrMonthName){
//Add the month name or number
var monthNumber = 0,
parsedNumber;
if(typeof monthNumberOrMonthName === "number"){
monthNumber = monthNumberOrMonthName;
}
Astrology.prototype.fetchZodiacSign = function(){
//Fetches the zodiac sign from the data that has been loaded into the class
object
//and the data provided by the user
var monthNumber = this.currentSearch.monthNumber,
date = this.currentSearch.date,
twoSignsArray = [],
zodiacSign = '',
zodiacNumber = 0,
zodiacSigns = this.zodiacSignsData.signs;
for(var i = 0, length = zodiacSigns.length; i < length; i++){
// First lets compare the months, will always just be two possible mont
hs that can
// be passed in to the two arrays
if(monthNumber === zodiacSigns[i].start.monthNumber || monthNumber === z
odiacSigns[i].end.monthNumber){
//If the months end or start in the specific month number we have a
go and should
//save the date in our temporary array awesomeness ;)
twoSignsArray.push(zodiacSigns[i]);
}
}
//Ok now lets check the two different signs
for(var j = 0, length = twoSignsArray.length; j < length; j++){
//First lets see if the month is in the start or in the end
if(twoSignsArray[j].start.monthNumber === monthNumber){
if(date > twoSignsArray[j].start.date){
zodiacSign = twoSignsArray[j].zodiacName;
zodiacNumber = twoSignsArray[j].zodiacNumber;
}
}
else if(twoSignsArray[j].end.monthNumber === monthNumber){
if(date <= twoSignsArray[j].end.date){
zodiacSign = twoSignsArray[j].zodiacName;
zodiacNumber = twoSignsArray[j].zodiacNumber;
}
}
}
return zodiacSign;
};
Astrology.prototype.fetchZodiacSignDescription = function(){
//Fetches the zodiac sign description
var zodiacSign = this.fetchZodiacSign(),
returnDescription = "",
descriptions = this.zodiacSignsData.descriptions;
if(typeof zodiacSign === "number"){
//A number lets fetch the description through a zodiacNumber check
for(var i = 0, length = descriptions.length; i < length; i++){
if(descriptions[i].zodiacNumber === zodiacSign){
returnDescription = descriptions[i].description;
}
}
} else {
//We have a string we are checking against lets do this ;)
for(var i = 0, length = descriptions.length; i < length; i++){
if(descriptions[i].zodiacName === zodiacSign){
returnDescription = descriptions[i].description;
}
}
}
return returnDescription;
};
Astrology.prototype.sendToConsole = function(message){
//Helper message method
var consoleMessage = "=================================" + "\n";
consoleMessage += "# Astrology" + "\n";
consoleMessage += "# Version: " + this.classData.version + " \n";
consoleMessage += "# Message: " + message + " \n";
consoleMessage += "=================================" + "\n";
console.log(consoleMessage);
};
Astrology.prototype.sendErrorMessageToConsole = function(message, error){
//Helper message method
var consoleMessage = "=================================" + "\n";
consoleMessage += "# Astrology" + "\n";
consoleMessage += "# Version: " + this.classData.version + " \n";
consoleMessage += "# Error: " + message + " \n";
consoleMessage += "=================================" + "\n";
console.log(consoleMessage);
console.log(error);
};
Astrology.prototype.initConnectionObject = function(connectionObject){
//Lets initiate the connectionObject
var dateDataUrl = '/js/astrology_data/astrology_data.json',
descriptionDataUrl = '/js/astrology_data/astrology_data.json',
returnObject;
returnObject = {
dateDataUrl: dateDataUrl,
descriptionDataUrl: descriptionDataUrl
};
try{
if(connectionObject === undefined){
//Lets do nothing since we have already defined the paths to where t
o
}
else {
returnObject.dateDataUrl = connectionObject.dateDataUrl || dateDataU
rl;
returnObject.descriptionDataUrl = connectionObject.descriptionDataUr
l || descriptionDataUrl;
}
} catch(e){
//Do nothing since we already have the object instantiated with default
values
}
return returnObject;
}
Astrology.prototype.init = function(connectionObject){
//The init function, does the work and sets up the rest of the instantiated
class object
//Setup the connectionObject
var connectionObjectModified = this.initConnectionObject(connectionObject);
//Declare some objects and variables used within the instantiated class
this.connectionObject = {
dateDataUrl: connectionObjectModified.dateDataUrl,
descriptionDataUrl: connectionObjectModified.descriptionDataUrl
};
this.classData = {
version:'0.4'
};
this.zodiacSignsData = {
signs: [],
descriptions: []
};
this.currentSearch = {
date:0,
monthNumber:'',
zodiacName:'',
zodiacNumber:0
};
//Load the data
this.xhrAndLoadJsonToDataArray(this.connectionObject.descriptionDataUrl, 'de
scriptions');
this.xhrAndLoadJsonToDataArray(this.connectionObject.dateDataUrl, 'signs');
//Print an instantiation message to the console
this.sendToConsole('Object instantiated with dateData url ' + this.connecti
onObject.dateDataUrl + ' and descriptionData url: ' + this.connectionObject.des
criptionDataUrl);
};