ImageView.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2007, 2008 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. *
  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. * @extends {WebInspector.ResourceView}
  30. * @constructor
  31. */
  32. WebInspector.ImageView = function(resource)
  33. {
  34. WebInspector.ResourceView.call(this, resource);
  35. this.element.addStyleClass("image");
  36. }
  37. WebInspector.ImageView.prototype = {
  38. hasContent: function()
  39. {
  40. return true;
  41. },
  42. wasShown: function()
  43. {
  44. this._createContentIfNeeded();
  45. },
  46. _createContentIfNeeded: function()
  47. {
  48. if (this._container)
  49. return;
  50. var imageContainer = document.createElement("div");
  51. imageContainer.className = "image";
  52. this.element.appendChild(imageContainer);
  53. var imagePreviewElement = document.createElement("img");
  54. imagePreviewElement.addStyleClass("resource-image-view");
  55. imageContainer.appendChild(imagePreviewElement);
  56. imagePreviewElement.addEventListener("contextmenu", this._contextMenu.bind(this), true);
  57. this._container = document.createElement("div");
  58. this._container.className = "info";
  59. this.element.appendChild(this._container);
  60. var imageNameElement = document.createElement("h1");
  61. imageNameElement.className = "title";
  62. imageNameElement.textContent = this.resource.displayName;
  63. this._container.appendChild(imageNameElement);
  64. var infoListElement = document.createElement("dl");
  65. infoListElement.className = "infoList";
  66. this.resource.populateImageSource(imagePreviewElement);
  67. function onImageLoad()
  68. {
  69. var content = this.resource.content;
  70. if (content)
  71. var resourceSize = this._base64ToSize(content);
  72. else
  73. var resourceSize = this.resource.resourceSize;
  74. var imageProperties = [
  75. { name: WebInspector.UIString("Dimensions"), value: WebInspector.UIString("%d × %d", imagePreviewElement.naturalWidth, imagePreviewElement.naturalHeight) },
  76. { name: WebInspector.UIString("File size"), value: Number.bytesToString(resourceSize) },
  77. { name: WebInspector.UIString("MIME type"), value: this.resource.mimeType }
  78. ];
  79. infoListElement.removeChildren();
  80. for (var i = 0; i < imageProperties.length; ++i) {
  81. var dt = document.createElement("dt");
  82. dt.textContent = imageProperties[i].name;
  83. infoListElement.appendChild(dt);
  84. var dd = document.createElement("dd");
  85. dd.textContent = imageProperties[i].value;
  86. infoListElement.appendChild(dd);
  87. }
  88. var dt = document.createElement("dt");
  89. dt.textContent = WebInspector.UIString("URL");
  90. infoListElement.appendChild(dt);
  91. var dd = document.createElement("dd");
  92. var externalResource = true;
  93. dd.appendChild(WebInspector.linkifyURLAsNode(this.resource.url, undefined, undefined, externalResource));
  94. infoListElement.appendChild(dd);
  95. this._container.appendChild(infoListElement);
  96. }
  97. imagePreviewElement.addEventListener("load", onImageLoad.bind(this), false);
  98. },
  99. _base64ToSize: function(content)
  100. {
  101. if (!content.length)
  102. return 0;
  103. var size = (content.length || 0) * 3 / 4;
  104. if (content.length > 0 && content[content.length - 1] === "=")
  105. size--;
  106. if (content.length > 1 && content[content.length - 2] === "=")
  107. size--;
  108. return size;
  109. },
  110. _contextMenu: function(event)
  111. {
  112. var contextMenu = new WebInspector.ContextMenu(event);
  113. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Copy image URL" : "Copy Image URL"), this._copyImageURL.bind(this));
  114. contextMenu.appendItem(WebInspector.UIString(WebInspector.useLowerCaseMenuTitles() ? "Open image in new tab" : "Open Image in New Tab"), this._openInNewTab.bind(this));
  115. contextMenu.show();
  116. },
  117. _copyImageURL: function()
  118. {
  119. InspectorFrontendHost.copyText(this.resource.url);
  120. },
  121. _openInNewTab: function()
  122. {
  123. InspectorFrontendHost.openInNewTab(this.resource.url);
  124. },
  125. __proto__: WebInspector.ResourceView.prototype
  126. }