// Add Options Module
// ==================
// add
// ---
// Adds drop down options
// using JSON data, an array,
// a single object, or valid HTML string
selectBoxIt.add = function(data, callback) {
this._populate(data, function(data) {
var self = this,
dataType = $.type(data),
value,
x = 0,
dataLength,
elems = [],
isJSON = self._isJSON(data),
parsedJSON = isJSON && self._parseJSON(data);
// If the passed data is a local or JSON array
if(data && (dataType === "array" || (isJSON && parsedJSON.data && $.type(parsedJSON.data) === "array")) || (dataType === "object" && data.data && $.type(data.data) === "array")) {
// If the data is JSON
if(self._isJSON(data)) {
// Parses the JSON and stores it in the data local variable
data = parsedJSON;
}
// If there is an inner `data` property stored in the first level of the JSON array
if(data.data) {
// Set's the data to the inner `data` property
data = data.data;
}
// Loops through the array
for(dataLength = data.length; x <= dataLength - 1; x += 1) {
// Stores the currently traversed array item in the local `value` variable
value = data[x];
// If the currently traversed array item is an object literal
if($.isPlainObject(value)) {
// Adds an option to the elems array
elems.push($("", value));
}
// If the currently traversed array item is a string
else if($.type(value) === "string") {
// Adds an option to the elems array
elems.push($("", { text: value, value: value }));
}
}
// Appends all options to the drop down (with the correct object configurations)
self.selectBox.append(elems);
}
// if the passed data is an html string and not a JSON string
else if(data && dataType === "string" && !self._isJSON(data)) {
// Appends the html string options to the original select box
self.selectBox.append(data);
}
else if(data && dataType === "object") {
// Appends an option to the original select box (with the object configurations)
self.selectBox.append($("", data));
}
else if(data && self._isJSON(data) && $.isPlainObject(self._parseJSON(data))) {
// Appends an option to the original select box (with the object configurations)
self.selectBox.append($("", self._parseJSON(data)));
}
// If the dropdown property exists
if(self.dropdown) {
// Rebuilds the dropdown
self.refresh(function() {
// Provide callback function support
self._callbackSupport(callback);
}, true);
} else {
// Provide callback function support
self._callbackSupport(callback);
}
// Maintains chainability
return self;
});
};
// parseJSON
// ---------
// Detects JSON support and parses JSON data
selectBoxIt._parseJSON = function(data) {
return (JSON && JSON.parse && JSON.parse(data)) || $.parseJSON(data);
};
// isjSON
// ------
// Determines if a string is valid JSON
selectBoxIt._isJSON = function(data) {
var self = this,
json;
try {
json = self._parseJSON(data);
// Valid JSON
return true;
} catch (e) {
// Invalid JSON
return false;
}
};
// _populate
// --------
// Handles asynchronous and synchronous data
// to populate the select box
selectBoxIt._populate = function(data, callback) {
var self = this;
data = $.isFunction(data) ? data.call() : data;
if(self.isDeferred(data)) {
data.done(function(returnedData) {
callback.call(self, returnedData);
});
}
else {
callback.call(self, data);
}
// Maintains chainability
return self;
};