chooser.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * This example features a window with a DataView from which the user can select images to add to a <div> on the page.
  3. * To create the example we create simple subclasses of Window, DataView and Panel. When the user selects an image
  4. * we just add it to the page using the insertSelectedImage function below.
  5. *
  6. * Our subclasses all sit under the Ext.chooser namespace so the first thing we do is tell Ext's class loader that it
  7. * can find those classes in this directory (InfoPanel.js, IconBrowser.js and Window.js). Then we just need to require
  8. * those files and pass in an onReady callback that will be called as soon as everything is loaded.
  9. */
  10. Ext.Loader.setConfig({enabled: true});
  11. Ext.Loader.setPath('Ext.chooser', '.');
  12. Ext.Loader.setPath('Ext.ux', '../../ux');
  13. Ext.require([
  14. 'Ext.button.Button',
  15. 'Ext.data.proxy.Ajax',
  16. 'Ext.chooser.InfoPanel',
  17. 'Ext.chooser.IconBrowser',
  18. 'Ext.chooser.Window',
  19. 'Ext.ux.DataView.Animated',
  20. 'Ext.toolbar.Spacer'
  21. ]);
  22. Ext.onReady(function() {
  23. /*
  24. * This button just opens the window. We render it into the 'buttons' div and set its
  25. * handler to simply show the window
  26. */
  27. var insertButton = Ext.create('Ext.button.Button', {
  28. text: "Insert Image",
  29. renderTo: 'buttons',
  30. handler : function() {
  31. win.show();
  32. }
  33. });
  34. /*
  35. * Here is where we create the window from which the user can select images to insert into the 'images' div.
  36. * This window is a simple subclass of Ext.window.Window, and you can see its source code in Window.js.
  37. * All we do here is attach a listener for when the 'selected' event is fired - when this happens it means
  38. * the user has double clicked an image in the window so we call our insertSelectedImage function to add it
  39. * to the DOM (see below).
  40. */
  41. var win = Ext.create('Ext.chooser.Window', {
  42. id: 'img-chooser-dlg',
  43. animateTarget: insertButton.getEl(),
  44. listeners: {
  45. selected: insertSelectedImage
  46. }
  47. });
  48. /*
  49. * This function is called whenever the user double-clicks an image inside the window. It creates
  50. * a new <img> tag inside the 'images' div and immediately hides it. We then call the show() function
  51. * with a duration of 500ms to fade the image in. At the end we call .frame() to give a visual cue
  52. * to the user that the image has been inserted
  53. */
  54. function insertSelectedImage(image) {
  55. //create the new image tag
  56. var image = Ext.fly('images').createChild({
  57. tag: 'img',
  58. src: 'icons/' + image.get('thumb')
  59. });
  60. //hide it straight away then fade it in over 500ms, finally use the frame animation to give emphasis
  61. image.hide().show({duration: 500}).frame();
  62. //this will make the window animate back to the newly inserted image element
  63. win.animateTarget = image;
  64. }
  65. });