ConsolePanel.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2009 Joseph Pecoraro
  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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.Panel}
  31. */
  32. WebInspector.ConsolePanel = function()
  33. {
  34. WebInspector.Panel.call(this, "console");
  35. WebInspector.consoleView.addEventListener(WebInspector.ConsoleView.Events.EntryAdded, this._consoleMessageAdded, this);
  36. WebInspector.consoleView.addEventListener(WebInspector.ConsoleView.Events.ConsoleCleared, this._consoleCleared, this);
  37. this._view = WebInspector.consoleView;
  38. }
  39. WebInspector.ConsolePanel.prototype = {
  40. statusBarItems: function()
  41. {
  42. return this._view.statusBarItems();
  43. },
  44. wasShown: function()
  45. {
  46. WebInspector.Panel.prototype.wasShown.call(this);
  47. if (WebInspector.drawer.visible) {
  48. WebInspector.drawer.hide(WebInspector.Drawer.AnimationType.Immediately);
  49. this._drawerWasVisible = true;
  50. }
  51. this._view.show(this.element);
  52. },
  53. willHide: function()
  54. {
  55. if (this._drawerWasVisible) {
  56. WebInspector.drawer.show(this._view, WebInspector.Drawer.AnimationType.Immediately);
  57. delete this._drawerWasVisible;
  58. }
  59. WebInspector.Panel.prototype.willHide.call(this);
  60. },
  61. searchCanceled: function()
  62. {
  63. this._clearCurrentSearchResultHighlight();
  64. delete this._searchResults;
  65. delete this._searchRegex;
  66. },
  67. /**
  68. * @param {string} query
  69. */
  70. performSearch: function(query)
  71. {
  72. WebInspector.searchController.updateSearchMatchesCount(0, this);
  73. this.searchCanceled();
  74. this._searchRegex = createPlainTextSearchRegex(query, "gi");
  75. this._searchResults = [];
  76. var messages = WebInspector.console.messages;
  77. for (var i = 0; i < messages.length; i++) {
  78. if (messages[i].matchesRegex(this._searchRegex)) {
  79. this._searchResults.push(messages[i]);
  80. this._searchRegex.lastIndex = 0;
  81. }
  82. }
  83. WebInspector.searchController.updateSearchMatchesCount(this._searchResults.length, this);
  84. this._currentSearchResultIndex = -1;
  85. if (this._searchResults.length)
  86. this._jumpToSearchResult(0);
  87. },
  88. jumpToNextSearchResult: function()
  89. {
  90. if (!this._searchResults || !this._searchResults.length)
  91. return;
  92. this._jumpToSearchResult((this._currentSearchResultIndex + 1) % this._searchResults.length);
  93. },
  94. jumpToPreviousSearchResult: function()
  95. {
  96. if (!this._searchResults || !this._searchResults.length)
  97. return;
  98. var index = this._currentSearchResultIndex - 1;
  99. if (index === -1)
  100. index = this._searchResults.length - 1;
  101. this._jumpToSearchResult(index);
  102. },
  103. _clearCurrentSearchResultHighlight: function()
  104. {
  105. if (!this._searchResults)
  106. return;
  107. var highlightedMessage = this._searchResults[this._currentSearchResultIndex];
  108. if (highlightedMessage)
  109. highlightedMessage.clearHighlight();
  110. this._currentSearchResultIndex = -1;
  111. },
  112. _jumpToSearchResult: function(index)
  113. {
  114. this._clearCurrentSearchResultHighlight();
  115. this._currentSearchResultIndex = index;
  116. WebInspector.searchController.updateCurrentMatchIndex(this._currentSearchResultIndex, this);
  117. this._searchResults[index].highlightSearchResults(this._searchRegex);
  118. },
  119. _consoleMessageAdded: function(event)
  120. {
  121. if (!this._searchRegex || !this.isShowing())
  122. return;
  123. var message = event.data;
  124. this._searchRegex.lastIndex = 0;
  125. if (message.matchesRegex(this._searchRegex)) {
  126. this._searchResults.push(message);
  127. WebInspector.searchController.updateSearchMatchesCount(this._searchResults.length, this);
  128. }
  129. },
  130. _consoleCleared: function()
  131. {
  132. if (!this._searchResults)
  133. return;
  134. this._clearCurrentSearchResultHighlight();
  135. this._searchResults.length = 0;
  136. if (this.isShowing())
  137. WebInspector.searchController.updateSearchMatchesCount(0, this);
  138. },
  139. __proto__: WebInspector.Panel.prototype
  140. }