Preview.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.editor.plugins.Preview"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.editor.plugins.Preview"] = true;
  8. dojo.provide("dojox.editor.plugins.Preview");
  9. dojo.require("dijit.form.Button");
  10. dojo.require("dijit._editor._Plugin");
  11. dojo.require("dojo.i18n");
  12. dojo.requireLocalization("dojox.editor.plugins", "Preview", null, "ROOT,ar,bg,ca,cs,da,de,el,es,fi,fr,he,hr,hu,it,ja,kk,ko,nb,nl,pl,pt,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-tw");
  13. dojo.declare("dojox.editor.plugins.Preview",dijit._editor._Plugin,{
  14. // summary:
  15. // This plugin provides Preview cabability to the editor. When
  16. // clicked, the document in the editor frame will displayed in a separate
  17. // window/tab
  18. // useDefaultCommand [protected]
  19. // Over-ride indicating that the command processing is done all by this plugin.
  20. useDefaultCommand: false,
  21. // styles: [public] String
  22. // A string of CSS styles to apply to the previewed content, if any.
  23. styles: "",
  24. // stylesheets: [public] Array
  25. // An array of stylesheets to import into the preview, if any.
  26. stylesheets: null,
  27. // iconClassPrefix: [const] String
  28. // The CSS class name for the button node icon.
  29. iconClassPrefix: "dijitAdditionalEditorIcon",
  30. _initButton: function(){
  31. // summary:
  32. // Over-ride for creation of the preview button.
  33. this._nlsResources = dojo.i18n.getLocalization("dojox.editor.plugins", "Preview");
  34. this.button = new dijit.form.Button({
  35. label: this._nlsResources["preview"],
  36. showLabel: false,
  37. iconClass: this.iconClassPrefix + " " + this.iconClassPrefix + "Preview",
  38. tabIndex: "-1",
  39. onClick: dojo.hitch(this, "_preview")
  40. });
  41. },
  42. setEditor: function(editor){
  43. // summary:
  44. // Over-ride for the setting of the editor.
  45. // editor: Object
  46. // The editor to configure for this plugin to use.
  47. this.editor = editor;
  48. this._initButton();
  49. },
  50. updateState: function(){
  51. // summary:
  52. // Over-ride for button state control for disabled to work.
  53. this.button.set("disabled", this.get("disabled"));
  54. },
  55. _preview: function(){
  56. // summary:
  57. // Function to trigger previewing of the editor document
  58. // tags:
  59. // private
  60. try{
  61. var content = this.editor.get("value");
  62. var head = "\t\t<meta http-equiv='Content-Type' content='text/html; charset='UTF-8'>\n";
  63. var i;
  64. // Apply the stylesheets, then apply the styles.
  65. if(this.stylesheets){
  66. for(i = 0; i < this.stylesheets.length; i++){
  67. head += "\t\t<link rel='stylesheet' type='text/css' href='" + this.stylesheets[i] + "'>\n";
  68. }
  69. }
  70. if(this.styles){
  71. head += ("\t\t<style>" + this.styles + "</style>\n");
  72. }
  73. content = "<html>\n\t<head>\n" + head + "\t</head>\n\t<body>\n" + content + "\n\t</body>\n</html>";
  74. var win = window.open("javascript: ''", this._nlsResources["preview"], "status=1,menubar=0,location=0,toolbar=0");
  75. win.document.open();
  76. win.document.write(content);
  77. win.document.close();
  78. }catch(e){
  79. console.warn(e);
  80. }
  81. }
  82. });
  83. // Register this plugin.
  84. dojo.subscribe(dijit._scopeName + ".Editor.getPlugin",null,function(o){
  85. if(o.plugin){ return; }
  86. var name = o.args.name.toLowerCase();
  87. if(name === "preview"){
  88. o.plugin = new dojox.editor.plugins.Preview({
  89. styles: ("styles" in o.args)?o.args.styles:"",
  90. stylesheets: ("stylesheets" in o.args)? o.args.stylesheets:null
  91. });
  92. }
  93. });
  94. }