patrickt

Clean property loading for JavaScript and Drupal settings

Dec 21st, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Get a property from an existing object.
  3.  *
  4.  * This tests each property level as you go so that you don't have to.
  5.  *
  6.  * Example: getProperty(someObj, 'myData.user.email');
  7.  *
  8.  * @param obj - Object you are testing
  9.  * @param property - Property you are looking for (can be a string of property levels)
  10.  * @returns {*} - The content of that property.
  11.  */
  12. function getProperty(obj, property) {
  13.   if (typeof obj != 'undefined' && typeof obj == 'object') {
  14.     var propertyChildren = null;
  15.  
  16.     //Break down a string of properties into an array
  17.     if (typeof property == 'string' && property.indexOf('.')) {
  18.       propertyChildren = property.split('.');
  19.       property = propertyChildren.shift();
  20.     }
  21.     else if (typeof property == 'object' && property.length > 0) {
  22.       propertyChildren = property;
  23.       property = propertyChildren.shift();
  24.     }
  25.  
  26.     if (typeof obj[property] != 'undefined') {
  27.       if (propertyChildren != null && propertyChildren.length > 0) {
  28.         return getProperty(obj[property], propertyChildren);
  29.       }
  30.  
  31.       return obj[property];
  32.     }
  33.   }
  34.  
  35.   return null;
  36. }
  37.  
  38. /**
  39.  * Get a setting from Drupal.settings.
  40.  *
  41.  * This searches recursively and makes sure we don't throw any JS errors along the way.
  42.  * Pass into it the var name like you were calling the object param normally.
  43.  *
  44.  * Example: getSetting('company.user_info.uid');
  45.  *
  46.  * @param setting
  47.  * @returns {*}
  48.  */
  49. function getSetting(setting) {
  50.   if (typeof Drupal != 'undefined' && typeof Drupal.settings != 'undefined') {
  51.     var settingChildren = null;
  52.     var currentLevel = null;
  53.  
  54.     if (typeof setting == 'string' && setting.indexOf('.')) {
  55.       settingChildren = setting.split('.');
  56.       setting = settingChildren.shift();
  57.     }
  58.  
  59.     if (typeof Drupal.settings[setting] != 'undefined') {
  60.       currentLevel = Drupal.settings[setting];
  61.  
  62.       if (settingChildren != null) {
  63.         settingChildren.forEach(function(elem, index, arr) {
  64.           if (typeof currentLevel[elem] != 'undefined') {
  65.             currentLevel = currentLevel[elem];
  66.           }
  67.           else {
  68.             return null;
  69.           }
  70.         });
  71.       }
  72.  
  73.       return currentLevel;
  74.     }
  75.   }
  76.  
  77.   return null;
  78. }
  79.  
  80. /**
  81.  * Gets a default value for empty vars.
  82.  *
  83.  * @param item
  84.  * @returns {*}
  85.  */
  86. function getVar(item) {
  87.   if (item !== null) {
  88.     return item;
  89.   }
  90.  
  91.   return '';
  92. }
  93.  
  94. /**
  95.  * Gets a clean var from settings.
  96.  *
  97.  * @param setting
  98.  * @returns {*}
  99.  */
  100. function getSettingVar(setting) {
  101.   return getVar( getSetting(setting) );
  102. }
Advertisement
Add Comment
Please, Sign In to add comment