Main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. Ext.define('KitchenSink.controller.Main', {
  2. extend: 'Ext.app.Controller',
  3. stores: [
  4. 'Examples',
  5. 'Companies',
  6. 'Restaurants',
  7. 'States',
  8. 'TreeStore'
  9. ],
  10. views: [
  11. 'Viewport',
  12. 'Header'
  13. ],
  14. refs: [
  15. {
  16. ref: 'examplePanel',
  17. selector: '#examplePanel'
  18. },
  19. {
  20. ref: 'exampleList',
  21. selector: 'exampleList'
  22. }
  23. ],
  24. init: function() {
  25. this.control({
  26. 'viewport exampleList': {
  27. 'select': function(me, record, item, index, e) {
  28. if (!record.isLeaf()) {
  29. return;
  30. }
  31. this.setActiveExample(this.classNameFromRecord(record), record.get('text'));
  32. },
  33. afterrender: function(){
  34. var me = this,
  35. className, exampleList, name, record;
  36. setTimeout(function(){
  37. className = location.hash.substring(1);
  38. exampleList = me.getExampleList();
  39. if (className) {
  40. name = className.replace('-', ' ');
  41. record = exampleList.view.store.find('text', name);
  42. } else {
  43. record = exampleList.view.store.find('text', 'grouped header grid');
  44. }
  45. exampleList.view.select(record);
  46. }, 0);
  47. }
  48. }
  49. });
  50. },
  51. setActiveExample: function(className, title) {
  52. var examplePanel = this.getExamplePanel(),
  53. path, example, className;
  54. if (!title) {
  55. title = className.split('.').reverse()[0];
  56. }
  57. //update the title on the panel
  58. examplePanel.setTitle(title);
  59. //remember the className so we can load up this example next time
  60. location.hash = title.toLowerCase().replace(' ', '-');
  61. //set the browser window title
  62. document.title = document.title.split(' - ')[0] + ' - ' + title;
  63. //create the example
  64. example = Ext.create(className);
  65. //remove all items from the example panel and add new example
  66. examplePanel.removeAll();
  67. examplePanel.add(example);
  68. },
  69. // Will be used for source file code
  70. // loadExample: function(path) {
  71. // Ext.Ajax.request({
  72. // url: path,
  73. // success: function() {
  74. // console.log(Ext.htmlEncode(response.responseText));
  75. // }
  76. // });
  77. // },
  78. filePathFromRecord: function(record) {
  79. var parentNode = record.parentNode,
  80. path = record.get('text');
  81. while (parentNode && parentNode.get('text') != "Root") {
  82. path = parentNode.get('text') + '/' + Ext.String.capitalize(path);
  83. parentNode = parentNode.parentNode;
  84. }
  85. return this.formatPath(path);
  86. },
  87. classNameFromRecord: function(record) {
  88. var path = this.filePathFromRecord(record);
  89. path = 'KitchenSink.view.examples.' + path.replace('/', '.');
  90. return path;
  91. },
  92. formatPath: function(string) {
  93. var result = string.split(' ')[0].charAt(0).toLowerCase() + string.split(' ')[0].substr(1),
  94. paths = string.split(' '),
  95. ln = paths.length,
  96. i;
  97. for (i = 1; i < ln; i++) {
  98. result = result + Ext.String.capitalize(paths[i]);
  99. }
  100. return result;
  101. }
  102. });