UISourceCodeFrame.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2012 Google 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 are
  6. * met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
  20. * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.SourceFrame}
  31. * @param {WebInspector.UISourceCode} uiSourceCode
  32. */
  33. WebInspector.UISourceCodeFrame = function(uiSourceCode)
  34. {
  35. this._uiSourceCode = uiSourceCode;
  36. WebInspector.SourceFrame.call(this, this._uiSourceCode);
  37. this.textEditor.setCompletionDictionary(new WebInspector.SampleCompletionDictionary());
  38. this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.FormattedChanged, this._onFormattedChanged, this);
  39. this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyChanged, this._onWorkingCopyChanged, this);
  40. this._uiSourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._onWorkingCopyCommitted, this);
  41. this._updateStyle();
  42. }
  43. WebInspector.UISourceCodeFrame.prototype = {
  44. wasShown: function()
  45. {
  46. WebInspector.SourceFrame.prototype.wasShown.call(this);
  47. this._boundWindowFocused = this._windowFocused.bind(this);
  48. window.addEventListener("focus", this._boundWindowFocused, false);
  49. this._checkContentUpdated();
  50. },
  51. willHide: function()
  52. {
  53. WebInspector.SourceFrame.prototype.willHide.call(this);
  54. window.removeEventListener("focus", this._boundWindowFocused, false);
  55. delete this._boundWindowFocused;
  56. this._uiSourceCode.removeWorkingCopyGetter();
  57. },
  58. /**
  59. * @return {boolean}
  60. */
  61. canEditSource: function()
  62. {
  63. return this._uiSourceCode.isEditable();
  64. },
  65. _windowFocused: function(event)
  66. {
  67. this._checkContentUpdated();
  68. },
  69. _checkContentUpdated: function()
  70. {
  71. if (!this.loaded || !this.isShowing())
  72. return;
  73. this._uiSourceCode.checkContentUpdated();
  74. },
  75. /**
  76. * @param {string} text
  77. */
  78. commitEditing: function(text)
  79. {
  80. if (!this._uiSourceCode.isDirty())
  81. return;
  82. this._muteSourceCodeEvents = true;
  83. this._uiSourceCode.commitWorkingCopy(this._didEditContent.bind(this));
  84. delete this._muteSourceCodeEvents;
  85. },
  86. onTextChanged: function(oldRange, newRange)
  87. {
  88. WebInspector.SourceFrame.prototype.onTextChanged.call(this, oldRange, newRange);
  89. if (this._isSettingContent)
  90. return;
  91. this._muteSourceCodeEvents = true;
  92. if (this._textEditor.isClean())
  93. this._uiSourceCode.resetWorkingCopy();
  94. else
  95. this._uiSourceCode.setWorkingCopyGetter(this._textEditor.text.bind(this._textEditor));
  96. delete this._muteSourceCodeEvents;
  97. },
  98. _didEditContent: function(error)
  99. {
  100. if (error) {
  101. WebInspector.log(error, WebInspector.ConsoleMessage.MessageLevel.Error, true);
  102. return;
  103. }
  104. },
  105. /**
  106. * @param {WebInspector.Event} event
  107. */
  108. _onFormattedChanged: function(event)
  109. {
  110. var content = /** @type {string} */ (event.data.content);
  111. this._textEditor.setReadOnly(this._uiSourceCode.formatted());
  112. this._innerSetContent(content);
  113. },
  114. /**
  115. * @param {WebInspector.Event} event
  116. */
  117. _onWorkingCopyChanged: function(event)
  118. {
  119. if (this._muteSourceCodeEvents)
  120. return;
  121. this._innerSetContent(this._uiSourceCode.workingCopy());
  122. this.onUISourceCodeContentChanged();
  123. },
  124. /**
  125. * @param {WebInspector.Event} event
  126. */
  127. _onWorkingCopyCommitted: function(event)
  128. {
  129. if (!this._muteSourceCodeEvents) {
  130. this._innerSetContent(this._uiSourceCode.workingCopy());
  131. this.onUISourceCodeContentChanged();
  132. }
  133. this._textEditor.markClean();
  134. this._updateStyle();
  135. },
  136. _updateStyle: function()
  137. {
  138. this.element.enableStyleClass("source-frame-unsaved-committed-changes", this._uiSourceCode.hasUnsavedCommittedChanges());
  139. },
  140. onUISourceCodeContentChanged: function()
  141. {
  142. },
  143. /**
  144. * @param {string} content
  145. */
  146. _innerSetContent: function(content)
  147. {
  148. this._isSettingContent = true;
  149. this.setContent(content, false, this._uiSourceCode.mimeType());
  150. delete this._isSettingContent;
  151. },
  152. populateTextAreaContextMenu: function(contextMenu, lineNumber)
  153. {
  154. WebInspector.SourceFrame.prototype.populateTextAreaContextMenu.call(this, contextMenu, lineNumber);
  155. contextMenu.appendApplicableItems(this._uiSourceCode);
  156. contextMenu.appendSeparator();
  157. },
  158. dispose: function()
  159. {
  160. this.detach();
  161. },
  162. __proto__: WebInspector.SourceFrame.prototype
  163. }