TextContentView.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.TextContentView = function(string, mimeType)
  26. {
  27. WebInspector.ContentView.call(this, string);
  28. this.element.classList.add(WebInspector.TextContentView.StyleClassName);
  29. this._textEditor = new WebInspector.TextEditor;
  30. this._textEditor.addEventListener(WebInspector.TextEditor.Event.NumberOfSearchResultsDidChange, this._numberOfSearchResultsDidChange, this);
  31. this._textEditor.addEventListener(WebInspector.TextEditor.Event.FormattingDidChange, this._textEditorFormattingDidChange, this);
  32. this.element.appendChild(this._textEditor.element);
  33. this._textEditor.readOnly = true;
  34. this._textEditor.mimeType = mimeType;
  35. this._textEditor.string = string;
  36. var toolTip = WebInspector.UIString("Pretty print");
  37. var activatedToolTip = WebInspector.UIString("Original formatting");
  38. this._prettyPrintButtonNavigationItem = new WebInspector.ActivateButtonNavigationItem("pretty-print", toolTip, activatedToolTip, "Images/NavigationItemCurleyBraces.pdf", 16, 16);
  39. this._prettyPrintButtonNavigationItem.addEventListener(WebInspector.ButtonNavigationItem.Event.Clicked, this._togglePrettyPrint, this);
  40. this._prettyPrintButtonNavigationItem.enabled = this._textEditor.canBeFormatted();
  41. };
  42. WebInspector.TextContentView.StyleClassName = "text";
  43. WebInspector.TextContentView.prototype = {
  44. constructor: WebInspector.TextContentView,
  45. // Public
  46. get textEditor()
  47. {
  48. return this._textEditor;
  49. },
  50. get navigationItems()
  51. {
  52. return [this._prettyPrintButtonNavigationItem];
  53. },
  54. revealPosition: function(position, textRangeToSelect, forceUnformatted)
  55. {
  56. this._textEditor.revealPosition(position, textRangeToSelect, forceUnformatted);
  57. },
  58. shown: function()
  59. {
  60. WebInspector.ResourceContentView.prototype.shown.call(this);
  61. this._textEditor.shown();
  62. },
  63. hidden: function()
  64. {
  65. WebInspector.ResourceContentView.prototype.hidden.call(this);
  66. this._textEditor.hidden();
  67. },
  68. closed: function()
  69. {
  70. WebInspector.ContentView.prototype.closed.call(this);
  71. this._textEditor.close();
  72. },
  73. get supportsSave()
  74. {
  75. return true;
  76. },
  77. get saveData()
  78. {
  79. var url = "web-inspector:///" + encodeURI(WebInspector.UIString("Untitled")) + ".txt";
  80. return {url: url, content: this._textEditor.string, forceSaveAs: true};
  81. },
  82. get supportsSearch()
  83. {
  84. return true;
  85. },
  86. get numberOfSearchResults()
  87. {
  88. return this._textEditor.numberOfSearchResults;
  89. },
  90. get hasPerformedSearch()
  91. {
  92. return this._textEditor.currentSearchQuery !== null;
  93. },
  94. set automaticallyRevealFirstSearchResult(reveal)
  95. {
  96. this._textEditor.automaticallyRevealFirstSearchResult = reveal;
  97. },
  98. performSearch: function(query)
  99. {
  100. this._textEditor.performSearch(query);
  101. },
  102. searchCleared: function()
  103. {
  104. this._textEditor.searchCleared();
  105. },
  106. searchQueryWithSelection: function()
  107. {
  108. return this._textEditor.searchQueryWithSelection();
  109. },
  110. revealPreviousSearchResult: function(changeFocus)
  111. {
  112. this._textEditor.revealPreviousSearchResult(changeFocus);
  113. },
  114. revealNextSearchResult: function(changeFocus)
  115. {
  116. this._textEditor.revealNextSearchResult(changeFocus);
  117. },
  118. updateLayout: function()
  119. {
  120. this._textEditor.updateLayout();
  121. },
  122. // Private
  123. _togglePrettyPrint: function(event)
  124. {
  125. var activated = !this._prettyPrintButtonNavigationItem.activated;
  126. this._textEditor.formatted = activated;
  127. },
  128. _textEditorFormattingDidChange: function(event)
  129. {
  130. this._prettyPrintButtonNavigationItem.activated = this._textEditor.formatted;
  131. },
  132. _numberOfSearchResultsDidChange: function(event)
  133. {
  134. this.dispatchEventToListeners(WebInspector.ContentView.Event.NumberOfSearchResultsDidChange);
  135. }
  136. };
  137. WebInspector.TextContentView.prototype.__proto__ = WebInspector.ContentView.prototype;