fred.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var Fred = function (config) {
  2. config = config || {};
  3. Fred.superclass.constructor.call(this, config);
  4. };
  5. Ext.extend(Fred, Ext.Component, {
  6. page: {},
  7. window: {},
  8. grid: {},
  9. tree: {},
  10. panel: {},
  11. combo: {},
  12. config: {},
  13. button: {},
  14. loadPage: function (action, parameters) {
  15. if (!parameters) {
  16. parameters = 'namespace=fred';
  17. } else {
  18. if (typeof parameters === 'object') {
  19. var stringParams = [];
  20. for (var key in parameters) {
  21. if (parameters.hasOwnProperty(key)) {
  22. stringParams.push(key + '=' + parameters[key]);
  23. }
  24. }
  25. parameters = stringParams.join('&');
  26. }
  27. parameters += '&namespace=fred';
  28. }
  29. MODx.loadPage(action, parameters);
  30. },
  31. prependBaseUrl: function(url, theme_folder = '') {
  32. url = url.replace('{{assets_url}}', MODx.config.assets_url);
  33. if (theme_folder) {
  34. url = url.replace('{{theme_dir}}', MODx.config.assets_url + 'themes/' + theme_folder + '/');
  35. }
  36. if ((url.substr(0,7).toLowerCase() !== 'http://') && (url.substr(0,8).toLowerCase() !== 'https://') && (url.substr(0,2).toLowerCase() !== '//') && (url.substr(0,1).toLowerCase() !== '/')) {
  37. url = MODx.config.base_url + url;
  38. }
  39. return url;
  40. },
  41. getPageUrl: function(action, parameters) {
  42. if (!parameters) {
  43. parameters = 'namespace=fred';
  44. } else {
  45. if (typeof parameters === 'object') {
  46. var stringParams = [];
  47. for (var key in parameters) {
  48. if (parameters.hasOwnProperty(key)) {
  49. stringParams.push(key + '=' + parameters[key]);
  50. }
  51. }
  52. parameters = stringParams.join('&');
  53. }
  54. parameters += '&namespace=fred';
  55. }
  56. // Handles url, passed as first argument
  57. var parts = [];
  58. if (action) {
  59. if (isNaN(parseInt(action)) && (action.substr(0, 1) == '?' || (action.substr(0, "index.php?".length) == 'index.php?'))) {
  60. parts.push(action);
  61. } else {
  62. parts.push('?a=' + action);
  63. }
  64. }
  65. if (parameters) {
  66. parts.push(parameters);
  67. }
  68. return parts.join('&');
  69. },
  70. getHelp: function(path = '', handler = true) {
  71. if (handler === true) {
  72. return function() {
  73. var realPath = (typeof path === 'function') ? path() : path;
  74. var win = window.open('https://modxcms.github.io/fred/' + realPath, '_blank');
  75. win.focus();
  76. };
  77. } else {
  78. var realPath = (typeof path === 'function') ? path() : path;
  79. var win = window.open('https://modxcms.github.io/fred/' + realPath, '_blank');
  80. win.focus();
  81. }
  82. },
  83. globalEvents: new Ext.util.Observable()
  84. });
  85. Ext.reg('fred', Fred);
  86. fred = new Fred();