Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get a property from an existing object.
- *
- * This tests each property level as you go so that you don't have to.
- *
- * Example: getProperty(someObj, 'myData.user.email');
- *
- * @param obj - Object you are testing
- * @param property - Property you are looking for (can be a string of property levels)
- * @returns {*} - The content of that property.
- */
- function getProperty(obj, property) {
- if (typeof obj != 'undefined' && typeof obj == 'object') {
- var propertyChildren = null;
- //Break down a string of properties into an array
- if (typeof property == 'string' && property.indexOf('.')) {
- propertyChildren = property.split('.');
- property = propertyChildren.shift();
- }
- else if (typeof property == 'object' && property.length > 0) {
- propertyChildren = property;
- property = propertyChildren.shift();
- }
- if (typeof obj[property] != 'undefined') {
- if (propertyChildren != null && propertyChildren.length > 0) {
- return getProperty(obj[property], propertyChildren);
- }
- return obj[property];
- }
- }
- return null;
- }
- /**
- * Get a setting from Drupal.settings.
- *
- * This searches recursively and makes sure we don't throw any JS errors along the way.
- * Pass into it the var name like you were calling the object param normally.
- *
- * Example: getSetting('company.user_info.uid');
- *
- * @param setting
- * @returns {*}
- */
- function getSetting(setting) {
- if (typeof Drupal != 'undefined' && typeof Drupal.settings != 'undefined') {
- var settingChildren = null;
- var currentLevel = null;
- if (typeof setting == 'string' && setting.indexOf('.')) {
- settingChildren = setting.split('.');
- setting = settingChildren.shift();
- }
- if (typeof Drupal.settings[setting] != 'undefined') {
- currentLevel = Drupal.settings[setting];
- if (settingChildren != null) {
- settingChildren.forEach(function(elem, index, arr) {
- if (typeof currentLevel[elem] != 'undefined') {
- currentLevel = currentLevel[elem];
- }
- else {
- return null;
- }
- });
- }
- return currentLevel;
- }
- }
- return null;
- }
- /**
- * Gets a default value for empty vars.
- *
- * @param item
- * @returns {*}
- */
- function getVar(item) {
- if (item !== null) {
- return item;
- }
- return '';
- }
- /**
- * Gets a clean var from settings.
- *
- * @param setting
- * @returns {*}
- */
- function getSettingVar(setting) {
- return getVar( getSetting(setting) );
- }
Advertisement
Add Comment
Please, Sign In to add comment