Preview.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. define("dojox/editor/plugins/Preview", [
  2. "dojo",
  3. "dijit",
  4. "dojox",
  5. "dijit/form/Button",
  6. "dijit/_editor/_Plugin",
  7. "dojo/_base/connect",
  8. "dojo/_base/declare",
  9. "dojo/i18n",
  10. "dojo/i18n!dojox/editor/plugins/nls/Preview"
  11. ], function(dojo, dijit, dojox) {
  12. dojo.declare("dojox.editor.plugins.Preview",dijit._editor._Plugin,{
  13. // summary:
  14. // This plugin provides Preview cabability to the editor. When
  15. // clicked, the document in the editor frame will displayed in a separate
  16. // window/tab
  17. // useDefaultCommand [protected]
  18. // Over-ride indicating that the command processing is done all by this plugin.
  19. useDefaultCommand: false,
  20. // styles: [public] String
  21. // A string of CSS styles to apply to the previewed content, if any.
  22. styles: "",
  23. // stylesheets: [public] Array
  24. // An array of stylesheets to import into the preview, if any.
  25. stylesheets: null,
  26. // iconClassPrefix: [const] String
  27. // The CSS class name for the button node icon.
  28. iconClassPrefix: "dijitAdditionalEditorIcon",
  29. _initButton: function(){
  30. // summary:
  31. // Over-ride for creation of the preview button.
  32. this._nlsResources = dojo.i18n.getLocalization("dojox.editor.plugins", "Preview");
  33. this.button = new dijit.form.Button({
  34. label: this._nlsResources["preview"],
  35. showLabel: false,
  36. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Preview",
  37. tabIndex: "-1",
  38. onClick: dojo.hitch(this, "_preview")
  39. });
  40. },
  41. setEditor: function(editor){
  42. // summary:
  43. // Over-ride for the setting of the editor.
  44. // editor: Object
  45. // The editor to configure for this plugin to use.
  46. this.editor = editor;
  47. this._initButton();
  48. },
  49. updateState: function(){
  50. // summary:
  51. // Over-ride for button state control for disabled to work.
  52. this.button.set("disabled", this.get("disabled"));
  53. },
  54. _preview: function(){
  55. // summary:
  56. // Function to trigger previewing of the editor document
  57. // tags:
  58. // private
  59. try{
  60. var content = this.editor.get("value");
  61. var head = "\t\t<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>\n";
  62. var i;
  63. // Apply the stylesheets, then apply the styles.
  64. if(this.stylesheets){
  65. for(i = 0; i < this.stylesheets.length; i++){
  66. head += "\t\t<link rel='stylesheet' type='text/css' href='" + this.stylesheets[i] + "'>\n";
  67. }
  68. }
  69. if(this.styles){
  70. head += ("\t\t<style>" + this.styles + "</style>\n");
  71. }
  72. content = "<html>\n\t<head>\n" + head + "\t</head>\n\t<body>\n" + content + "\n\t</body>\n</html>";
  73. var win = window.open("javascript: ''", this._nlsResources["preview"], "status=1,menubar=0,location=0,toolbar=0");
  74. win.document.open();
  75. win.document.write(content);
  76. win.document.close();
  77. }catch(e){
  78. console.warn(e);
  79. }
  80. }
  81. });
  82. // Register this plugin.
  83. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  84. if(o.plugin){ return; }
  85. var name = o.args.name.toLowerCase();
  86. if(name === "preview"){
  87. o.plugin = new dojox.editor.plugins.Preview({
  88. styles: ("styles" in o.args)?o.args.styles:"",
  89. stylesheets: ("stylesheets" in o.args)? o.args.stylesheets:null
  90. });
  91. }
  92. });
  93. return dojox.editor.plugins.Preview;
  94. });