ObjectPropertiesSection.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*
  2. * Copyright (C) 2008 Apple Inc. All Rights Reserved.
  3. * Copyright (C) 2009 Joseph Pecoraro
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. * @constructor
  28. * @extends {WebInspector.PropertiesSection}
  29. * @param {WebInspector.RemoteObject} object
  30. * @param {?string|Element=} title
  31. * @param {string=} subtitle
  32. * @param {?string=} emptyPlaceholder
  33. * @param {boolean=} ignoreHasOwnProperty
  34. * @param {Array.<WebInspector.RemoteObjectProperty>=} extraProperties
  35. * @param {function(new:TreeElement, WebInspector.RemoteObjectProperty)=} treeElementConstructor
  36. */
  37. WebInspector.ObjectPropertiesSection = function(object, title, subtitle, emptyPlaceholder, ignoreHasOwnProperty, extraProperties, treeElementConstructor)
  38. {
  39. this.emptyPlaceholder = (emptyPlaceholder || WebInspector.UIString("No Properties"));
  40. this.object = object;
  41. this.ignoreHasOwnProperty = ignoreHasOwnProperty;
  42. this.extraProperties = extraProperties;
  43. this.treeElementConstructor = treeElementConstructor || WebInspector.ObjectPropertyTreeElement;
  44. this.editable = true;
  45. this.skipProto = false;
  46. WebInspector.PropertiesSection.call(this, title || "", subtitle);
  47. }
  48. WebInspector.ObjectPropertiesSection._arrayLoadThreshold = 100;
  49. WebInspector.ObjectPropertiesSection.prototype = {
  50. enableContextMenu: function()
  51. {
  52. this.element.addEventListener("contextmenu", this._contextMenuEventFired.bind(this), false);
  53. },
  54. _contextMenuEventFired: function(event)
  55. {
  56. var contextMenu = new WebInspector.ContextMenu(event);
  57. contextMenu.appendApplicableItems(this.object);
  58. contextMenu.show();
  59. },
  60. onpopulate: function()
  61. {
  62. this.update();
  63. },
  64. update: function()
  65. {
  66. if (this.object.arrayLength() > WebInspector.ObjectPropertiesSection._arrayLoadThreshold) {
  67. this.propertiesTreeOutline.removeChildren();
  68. WebInspector.ArrayGroupingTreeElement._populateArray(this.propertiesTreeOutline, this.object, 0, this.object.arrayLength() - 1);
  69. return;
  70. }
  71. /**
  72. * @param {Array.<WebInspector.RemoteObjectProperty>} properties
  73. * @param {Array.<WebInspector.RemoteObjectProperty>=} internalProperties
  74. */
  75. function callback(properties, internalProperties)
  76. {
  77. if (!properties)
  78. return;
  79. this.updateProperties(properties, internalProperties);
  80. }
  81. if (this.ignoreHasOwnProperty)
  82. this.object.getAllProperties(callback.bind(this));
  83. else
  84. this.object.getOwnProperties(callback.bind(this));
  85. },
  86. updateProperties: function(properties, internalProperties, rootTreeElementConstructor, rootPropertyComparer)
  87. {
  88. if (!rootTreeElementConstructor)
  89. rootTreeElementConstructor = this.treeElementConstructor;
  90. if (!rootPropertyComparer)
  91. rootPropertyComparer = WebInspector.ObjectPropertiesSection.CompareProperties;
  92. if (this.extraProperties) {
  93. for (var i = 0; i < this.extraProperties.length; ++i)
  94. properties.push(this.extraProperties[i]);
  95. }
  96. this.propertiesTreeOutline.removeChildren();
  97. WebInspector.ObjectPropertyTreeElement.populateWithProperties(this.propertiesTreeOutline,
  98. properties, internalProperties,
  99. rootTreeElementConstructor, rootPropertyComparer,
  100. this.skipProto, this.object);
  101. this.propertiesForTest = properties;
  102. if (!this.propertiesTreeOutline.children.length) {
  103. var title = document.createElement("div");
  104. title.className = "info";
  105. title.textContent = this.emptyPlaceholder;
  106. var infoElement = new TreeElement(title, null, false);
  107. this.propertiesTreeOutline.appendChild(infoElement);
  108. }
  109. },
  110. __proto__: WebInspector.PropertiesSection.prototype
  111. }
  112. WebInspector.ObjectPropertiesSection.CompareProperties = function(propertyA, propertyB)
  113. {
  114. var a = propertyA.name;
  115. var b = propertyB.name;
  116. if (a === "__proto__")
  117. return 1;
  118. if (b === "__proto__")
  119. return -1;
  120. // if used elsewhere make sure to
  121. // - convert a and b to strings (not needed here, properties are all strings)
  122. // - check if a == b (not needed here, no two properties can be the same)
  123. var diff = 0;
  124. var chunk = /^\d+|^\D+/;
  125. var chunka, chunkb, anum, bnum;
  126. while (diff === 0) {
  127. if (!a && b)
  128. return -1;
  129. if (!b && a)
  130. return 1;
  131. chunka = a.match(chunk)[0];
  132. chunkb = b.match(chunk)[0];
  133. anum = !isNaN(chunka);
  134. bnum = !isNaN(chunkb);
  135. if (anum && !bnum)
  136. return -1;
  137. if (bnum && !anum)
  138. return 1;
  139. if (anum && bnum) {
  140. diff = chunka - chunkb;
  141. if (diff === 0 && chunka.length !== chunkb.length) {
  142. if (!+chunka && !+chunkb) // chunks are strings of all 0s (special case)
  143. return chunka.length - chunkb.length;
  144. else
  145. return chunkb.length - chunka.length;
  146. }
  147. } else if (chunka !== chunkb)
  148. return (chunka < chunkb) ? -1 : 1;
  149. a = a.substring(chunka.length);
  150. b = b.substring(chunkb.length);
  151. }
  152. return diff;
  153. }
  154. /**
  155. * @constructor
  156. * @extends {TreeElement}
  157. * @param {WebInspector.RemoteObjectProperty} property
  158. */
  159. WebInspector.ObjectPropertyTreeElement = function(property)
  160. {
  161. this.property = property;
  162. // Pass an empty title, the title gets made later in onattach.
  163. TreeElement.call(this, "", null, false);
  164. this.toggleOnClick = true;
  165. this.selectable = false;
  166. }
  167. WebInspector.ObjectPropertyTreeElement.prototype = {
  168. onpopulate: function()
  169. {
  170. return WebInspector.ObjectPropertyTreeElement.populate(this, this.property.value);
  171. },
  172. ondblclick: function(event)
  173. {
  174. if (this.property.writable)
  175. this.startEditing(event);
  176. },
  177. onattach: function()
  178. {
  179. this.update();
  180. },
  181. update: function()
  182. {
  183. this.nameElement = document.createElement("span");
  184. this.nameElement.className = "name";
  185. this.nameElement.textContent = this.property.name;
  186. if (!this.property.enumerable)
  187. this.nameElement.addStyleClass("dimmed");
  188. var separatorElement = document.createElement("span");
  189. separatorElement.className = "separator";
  190. separatorElement.textContent = ": ";
  191. this.valueElement = document.createElement("span");
  192. this.valueElement.className = "value";
  193. var description = this.property.value.description;
  194. // Render \n as a nice unicode cr symbol.
  195. if (this.property.wasThrown)
  196. this.valueElement.textContent = "[Exception: " + description + "]";
  197. else if (this.property.value.type === "string" && typeof description === "string") {
  198. this.valueElement.textContent = "\"" + description.replace(/\n/g, "\u21B5") + "\"";
  199. this.valueElement._originalTextContent = "\"" + description + "\"";
  200. } else if (this.property.value.type === "function" && typeof description === "string") {
  201. this.valueElement.textContent = /.*/.exec(description)[0].replace(/ +$/g, "");
  202. this.valueElement._originalTextContent = description;
  203. } else if (this.property.value.type !== "object" || this.property.value.subtype !== "node")
  204. this.valueElement.textContent = description;
  205. if (this.property.wasThrown)
  206. this.valueElement.addStyleClass("error");
  207. if (this.property.value.subtype)
  208. this.valueElement.addStyleClass("console-formatted-" + this.property.value.subtype);
  209. else if (this.property.value.type)
  210. this.valueElement.addStyleClass("console-formatted-" + this.property.value.type);
  211. this.valueElement.addEventListener("contextmenu", this._contextMenuFired.bind(this, this.property.value), false);
  212. if (this.property.value.type === "object" && this.property.value.subtype === "node" && this.property.value.description) {
  213. WebInspector.DOMPresentationUtils.createSpansForNodeTitle(this.valueElement, this.property.value.description);
  214. this.valueElement.addEventListener("mousemove", this._mouseMove.bind(this, this.property.value), false);
  215. this.valueElement.addEventListener("mouseout", this._mouseOut.bind(this, this.property.value), false);
  216. } else
  217. this.valueElement.title = description || "";
  218. this.listItemElement.removeChildren();
  219. this.listItemElement.appendChild(this.nameElement);
  220. this.listItemElement.appendChild(separatorElement);
  221. this.listItemElement.appendChild(this.valueElement);
  222. this.hasChildren = this.property.value.hasChildren && !this.property.wasThrown;
  223. },
  224. _contextMenuFired: function(value, event)
  225. {
  226. var contextMenu = new WebInspector.ContextMenu(event);
  227. this.populateContextMenu(contextMenu);
  228. contextMenu.appendApplicableItems(value);
  229. contextMenu.show();
  230. },
  231. /**
  232. * @param {WebInspector.ContextMenu} contextMenu
  233. */
  234. populateContextMenu: function(contextMenu)
  235. {
  236. },
  237. _mouseMove: function(event)
  238. {
  239. this.property.value.highlightAsDOMNode();
  240. },
  241. _mouseOut: function(event)
  242. {
  243. this.property.value.hideDOMNodeHighlight();
  244. },
  245. updateSiblings: function()
  246. {
  247. if (this.parent.root)
  248. this.treeOutline.section.update();
  249. else
  250. this.parent.shouldRefreshChildren = true;
  251. },
  252. renderPromptAsBlock: function()
  253. {
  254. return false;
  255. },
  256. /**
  257. * @param {Event=} event
  258. */
  259. elementAndValueToEdit: function(event)
  260. {
  261. return [this.valueElement, (typeof this.valueElement._originalTextContent === "string") ? this.valueElement._originalTextContent : undefined];
  262. },
  263. startEditing: function(event)
  264. {
  265. var elementAndValueToEdit = this.elementAndValueToEdit(event);
  266. var elementToEdit = elementAndValueToEdit[0];
  267. var valueToEdit = elementAndValueToEdit[1];
  268. if (WebInspector.isBeingEdited(elementToEdit) || !this.treeOutline.section.editable || this._readOnly)
  269. return;
  270. // Edit original source.
  271. if (typeof valueToEdit !== "undefined")
  272. elementToEdit.textContent = valueToEdit;
  273. var context = { expanded: this.expanded, elementToEdit: elementToEdit, previousContent: elementToEdit.textContent };
  274. // Lie about our children to prevent expanding on double click and to collapse subproperties.
  275. this.hasChildren = false;
  276. this.listItemElement.addStyleClass("editing-sub-part");
  277. this._prompt = new WebInspector.ObjectPropertyPrompt(this.editingCommitted.bind(this, null, elementToEdit.textContent, context.previousContent, context), this.editingCancelled.bind(this, null, context), this.renderPromptAsBlock());
  278. function blurListener()
  279. {
  280. this.editingCommitted(null, elementToEdit.textContent, context.previousContent, context);
  281. }
  282. var proxyElement = this._prompt.attachAndStartEditing(elementToEdit, blurListener.bind(this));
  283. window.getSelection().setBaseAndExtent(elementToEdit, 0, elementToEdit, 1);
  284. proxyElement.addEventListener("keydown", this._promptKeyDown.bind(this, context), false);
  285. },
  286. /**
  287. * @return {boolean}
  288. */
  289. isEditing: function()
  290. {
  291. return !!this._prompt;
  292. },
  293. editingEnded: function(context)
  294. {
  295. this._prompt.detach();
  296. delete this._prompt;
  297. this.listItemElement.scrollLeft = 0;
  298. this.listItemElement.removeStyleClass("editing-sub-part");
  299. if (context.expanded)
  300. this.expand();
  301. },
  302. editingCancelled: function(element, context)
  303. {
  304. this.editingEnded(context);
  305. this.update();
  306. },
  307. editingCommitted: function(element, userInput, previousContent, context)
  308. {
  309. if (userInput === previousContent)
  310. return this.editingCancelled(element, context); // nothing changed, so cancel
  311. this.editingEnded(context);
  312. this.applyExpression(userInput, true);
  313. },
  314. _promptKeyDown: function(context, event)
  315. {
  316. if (isEnterKey(event)) {
  317. event.consume(true);
  318. return this.editingCommitted(null, context.elementToEdit.textContent, context.previousContent, context);
  319. }
  320. if (event.keyIdentifier === "U+001B") { // Esc
  321. event.consume();
  322. return this.editingCancelled(null, context);
  323. }
  324. },
  325. applyExpression: function(expression, updateInterface)
  326. {
  327. expression = expression.trim();
  328. var expressionLength = expression.length;
  329. function callback(error)
  330. {
  331. if (!updateInterface)
  332. return;
  333. if (error)
  334. this.update();
  335. if (!expressionLength) {
  336. // The property was deleted, so remove this tree element.
  337. this.parent.removeChild(this);
  338. } else {
  339. // Call updateSiblings since their value might be based on the value that just changed.
  340. this.updateSiblings();
  341. }
  342. };
  343. this.property.parentObject.setPropertyValue(this.property.name, expression.trim(), callback.bind(this));
  344. },
  345. propertyPath: function()
  346. {
  347. if ("_cachedPropertyPath" in this)
  348. return this._cachedPropertyPath;
  349. var current = this;
  350. var result;
  351. do {
  352. if (current.property) {
  353. if (result)
  354. result = current.property.name + "." + result;
  355. else
  356. result = current.property.name;
  357. }
  358. current = current.parent;
  359. } while (current && !current.root);
  360. this._cachedPropertyPath = result;
  361. return result;
  362. },
  363. __proto__: TreeElement.prototype
  364. }
  365. /**
  366. * @param {TreeElement} treeElement
  367. * @param {WebInspector.RemoteObject} value
  368. */
  369. WebInspector.ObjectPropertyTreeElement.populate = function(treeElement, value) {
  370. if (treeElement.children.length && !treeElement.shouldRefreshChildren)
  371. return;
  372. if (value.arrayLength() > WebInspector.ObjectPropertiesSection._arrayLoadThreshold) {
  373. treeElement.removeChildren();
  374. WebInspector.ArrayGroupingTreeElement._populateArray(treeElement, value, 0, value.arrayLength() - 1);
  375. return;
  376. }
  377. /**
  378. * @param {Array.<WebInspector.RemoteObjectProperty>} properties
  379. * @param {Array.<WebInspector.RemoteObjectProperty>=} internalProperties
  380. */
  381. function callback(properties, internalProperties)
  382. {
  383. treeElement.removeChildren();
  384. if (!properties)
  385. return;
  386. if (!internalProperties)
  387. internalProperties = [];
  388. WebInspector.ObjectPropertyTreeElement.populateWithProperties(treeElement, properties, internalProperties,
  389. treeElement.treeOutline.section.treeElementConstructor, WebInspector.ObjectPropertiesSection.CompareProperties,
  390. treeElement.treeOutline.section.skipProto, value);
  391. }
  392. value.getOwnProperties(callback);
  393. }
  394. /**
  395. * @param {!TreeElement|!TreeOutline} treeElement
  396. * @param {Array.<!WebInspector.RemoteObjectProperty>} properties
  397. * @param {?Array.<!WebInspector.RemoteObjectProperty>} internalProperties
  398. * @param {function(new:TreeElement, WebInspector.RemoteObjectProperty)} treeElementConstructor
  399. * @param {function (WebInspector.RemoteObjectProperty, WebInspector.RemoteObjectProperty): number} comparator
  400. * @param {boolean} skipProto
  401. * @param {?WebInspector.RemoteObject} value
  402. */
  403. WebInspector.ObjectPropertyTreeElement.populateWithProperties = function(treeElement, properties, internalProperties, treeElementConstructor, comparator, skipProto, value) {
  404. properties.sort(comparator);
  405. for (var i = 0; i < properties.length; ++i) {
  406. if (skipProto && properties[i].name === "__proto__")
  407. continue;
  408. properties[i].parentObject = value;
  409. treeElement.appendChild(new treeElementConstructor(properties[i]));
  410. }
  411. if (value && value.type === "function") {
  412. // Whether function has TargetFunction internal property.
  413. // This is a simple way to tell that the function is actually a bound function (we are not told).
  414. // Bound function never has inner scope and doesn't need corresponding UI node.
  415. var hasTargetFunction = false;
  416. if (internalProperties) {
  417. for (var i = 0; i < internalProperties.length; i++) {
  418. if (internalProperties[i].name == "[[TargetFunction]]") {
  419. hasTargetFunction = true;
  420. break;
  421. }
  422. }
  423. }
  424. if (!hasTargetFunction)
  425. treeElement.appendChild(new WebInspector.FunctionScopeMainTreeElement(value));
  426. }
  427. if (internalProperties) {
  428. for (var i = 0; i < internalProperties.length; i++) {
  429. internalProperties[i].parentObject = value;
  430. treeElement.appendChild(new treeElementConstructor(internalProperties[i]));
  431. }
  432. }
  433. }
  434. /**
  435. * @constructor
  436. * @extends {TreeElement}
  437. * @param {WebInspector.RemoteObject} remoteObject
  438. */
  439. WebInspector.FunctionScopeMainTreeElement = function(remoteObject)
  440. {
  441. TreeElement.call(this, "<function scope>", null, false);
  442. this.toggleOnClick = true;
  443. this.selectable = false;
  444. this._remoteObject = remoteObject;
  445. this.hasChildren = true;
  446. }
  447. WebInspector.FunctionScopeMainTreeElement.prototype = {
  448. onpopulate: function()
  449. {
  450. if (this.children.length && !this.shouldRefreshChildren)
  451. return;
  452. function didGetDetails(error, response)
  453. {
  454. if (error) {
  455. console.error(error);
  456. return;
  457. }
  458. this.removeChildren();
  459. var scopeChain = response.scopeChain;
  460. if (!scopeChain)
  461. return;
  462. for (var i = 0; i < scopeChain.length; ++i) {
  463. var scope = scopeChain[i];
  464. var title = null;
  465. var isTrueObject;
  466. switch (scope.type) {
  467. case "local":
  468. // Not really expecting this scope type here.
  469. title = WebInspector.UIString("Local");
  470. isTrueObject = false;
  471. break;
  472. case "closure":
  473. title = WebInspector.UIString("Closure");
  474. isTrueObject = false;
  475. break;
  476. case "catch":
  477. title = WebInspector.UIString("Catch");
  478. isTrueObject = false;
  479. break;
  480. case "with":
  481. title = WebInspector.UIString("With Block");
  482. isTrueObject = true;
  483. break;
  484. case "global":
  485. title = WebInspector.UIString("Global");
  486. isTrueObject = true;
  487. break;
  488. default:
  489. console.error("Unknown scope type: " + scope.type);
  490. continue;
  491. }
  492. var scopeRef;
  493. if (isTrueObject)
  494. scopeRef = undefined;
  495. else
  496. scopeRef = new WebInspector.ScopeRef(i, undefined, this._remoteObject.objectId);
  497. var remoteObject = WebInspector.RemoteObject.fromScopePayload(scope.object, scopeRef);
  498. if (isTrueObject) {
  499. var property = WebInspector.RemoteObjectProperty.fromScopeValue(title, remoteObject);
  500. property.parentObject = null;
  501. this.appendChild(new this.treeOutline.section.treeElementConstructor(property));
  502. } else {
  503. var scopeTreeElement = new WebInspector.ScopeTreeElement(title, null, remoteObject);
  504. this.appendChild(scopeTreeElement);
  505. }
  506. }
  507. }
  508. DebuggerAgent.getFunctionDetails(this._remoteObject.objectId, didGetDetails.bind(this));
  509. },
  510. __proto__: TreeElement.prototype
  511. }
  512. /**
  513. * @constructor
  514. * @extends {TreeElement}
  515. * @param {WebInspector.RemoteObject} remoteObject
  516. */
  517. WebInspector.ScopeTreeElement = function(title, subtitle, remoteObject)
  518. {
  519. // TODO: use subtitle parameter.
  520. TreeElement.call(this, title, null, false);
  521. this.toggleOnClick = true;
  522. this.selectable = false;
  523. this._remoteObject = remoteObject;
  524. this.hasChildren = true;
  525. }
  526. WebInspector.ScopeTreeElement.prototype = {
  527. onpopulate: function()
  528. {
  529. return WebInspector.ObjectPropertyTreeElement.populate(this, this._remoteObject);
  530. },
  531. __proto__: TreeElement.prototype
  532. }
  533. /**
  534. * @constructor
  535. * @extends {TreeElement}
  536. * @param {WebInspector.RemoteObject} object
  537. * @param {number} fromIndex
  538. * @param {number} toIndex
  539. * @param {number} propertyCount
  540. */
  541. WebInspector.ArrayGroupingTreeElement = function(object, fromIndex, toIndex, propertyCount)
  542. {
  543. TreeElement.call(this, String.sprintf("[%d \u2026 %d]", fromIndex, toIndex), undefined, true);
  544. this._fromIndex = fromIndex;
  545. this._toIndex = toIndex;
  546. this._object = object;
  547. this._readOnly = true;
  548. this._propertyCount = propertyCount;
  549. this._populated = false;
  550. }
  551. WebInspector.ArrayGroupingTreeElement._bucketThreshold = 100;
  552. WebInspector.ArrayGroupingTreeElement._sparseIterationThreshold = 250000;
  553. /**
  554. * @param {TreeElement|TreeOutline} treeElement
  555. * @param {WebInspector.RemoteObject} object
  556. * @param {number} fromIndex
  557. * @param {number} toIndex
  558. */
  559. WebInspector.ArrayGroupingTreeElement._populateArray = function(treeElement, object, fromIndex, toIndex)
  560. {
  561. WebInspector.ArrayGroupingTreeElement._populateRanges(treeElement, object, fromIndex, toIndex, true);
  562. }
  563. /**
  564. * @param {TreeElement|TreeOutline} treeElement
  565. * @param {WebInspector.RemoteObject} object
  566. * @param {number} fromIndex
  567. * @param {number} toIndex
  568. * @param {boolean} topLevel
  569. */
  570. WebInspector.ArrayGroupingTreeElement._populateRanges = function(treeElement, object, fromIndex, toIndex, topLevel)
  571. {
  572. object.callFunctionJSON(packRanges, [{value: fromIndex}, {value: toIndex}, {value: WebInspector.ArrayGroupingTreeElement._bucketThreshold}, {value: WebInspector.ArrayGroupingTreeElement._sparseIterationThreshold}], callback.bind(this));
  573. /**
  574. * @this {Object}
  575. * @param {number=} fromIndex // must declare optional
  576. * @param {number=} toIndex // must declare optional
  577. * @param {number=} bucketThreshold // must declare optional
  578. * @param {number=} sparseIterationThreshold // must declare optional
  579. */
  580. function packRanges(fromIndex, toIndex, bucketThreshold, sparseIterationThreshold)
  581. {
  582. var ownPropertyNames = null;
  583. function doLoop(iterationCallback)
  584. {
  585. if (toIndex - fromIndex < sparseIterationThreshold) {
  586. for (var i = fromIndex; i <= toIndex; ++i) {
  587. if (i in this)
  588. iterationCallback(i);
  589. }
  590. } else {
  591. ownPropertyNames = ownPropertyNames || Object.getOwnPropertyNames(this);
  592. for (var i = 0; i < ownPropertyNames.length; ++i) {
  593. var name = ownPropertyNames[i];
  594. var index = name >>> 0;
  595. if (String(index) === name && fromIndex <= index && index <= toIndex)
  596. iterationCallback(index);
  597. }
  598. }
  599. }
  600. var count = 0;
  601. function countIterationCallback()
  602. {
  603. ++count;
  604. }
  605. doLoop.call(this, countIterationCallback);
  606. var bucketSize = count;
  607. if (count <= bucketThreshold)
  608. bucketSize = count;
  609. else
  610. bucketSize = Math.pow(bucketThreshold, Math.ceil(Math.log(count) / Math.log(bucketThreshold)) - 1);
  611. var ranges = [];
  612. count = 0;
  613. var groupStart = -1;
  614. var groupEnd = 0;
  615. function loopIterationCallback(i)
  616. {
  617. if (groupStart === -1)
  618. groupStart = i;
  619. groupEnd = i;
  620. if (++count === bucketSize) {
  621. ranges.push([groupStart, groupEnd, count]);
  622. count = 0;
  623. groupStart = -1;
  624. }
  625. }
  626. doLoop.call(this, loopIterationCallback);
  627. if (count > 0)
  628. ranges.push([groupStart, groupEnd, count]);
  629. return ranges;
  630. }
  631. function callback(ranges)
  632. {
  633. if (ranges.length == 1)
  634. WebInspector.ArrayGroupingTreeElement._populateAsFragment(treeElement, object, ranges[0][0], ranges[0][1]);
  635. else {
  636. for (var i = 0; i < ranges.length; ++i) {
  637. var fromIndex = ranges[i][0];
  638. var toIndex = ranges[i][1];
  639. var count = ranges[i][2];
  640. if (fromIndex == toIndex)
  641. WebInspector.ArrayGroupingTreeElement._populateAsFragment(treeElement, object, fromIndex, toIndex);
  642. else
  643. treeElement.appendChild(new WebInspector.ArrayGroupingTreeElement(object, fromIndex, toIndex, count));
  644. }
  645. }
  646. if (topLevel)
  647. WebInspector.ArrayGroupingTreeElement._populateNonIndexProperties(treeElement, object);
  648. }
  649. }
  650. /**
  651. * @param {TreeElement|TreeOutline} treeElement
  652. * @param {WebInspector.RemoteObject} object
  653. * @param {number} fromIndex
  654. * @param {number} toIndex
  655. */
  656. WebInspector.ArrayGroupingTreeElement._populateAsFragment = function(treeElement, object, fromIndex, toIndex)
  657. {
  658. object.callFunction(buildArrayFragment, [{value: fromIndex}, {value: toIndex}, {value: WebInspector.ArrayGroupingTreeElement._sparseIterationThreshold}], processArrayFragment.bind(this));
  659. /**
  660. * @this {Object}
  661. * @param {number=} fromIndex // must declare optional
  662. * @param {number=} toIndex // must declare optional
  663. * @param {number=} sparseIterationThreshold // must declare optional
  664. */
  665. function buildArrayFragment(fromIndex, toIndex, sparseIterationThreshold)
  666. {
  667. var result = Object.create(null);
  668. if (toIndex - fromIndex < sparseIterationThreshold) {
  669. for (var i = fromIndex; i <= toIndex; ++i) {
  670. if (i in this)
  671. result[i] = this[i];
  672. }
  673. } else {
  674. var ownPropertyNames = Object.getOwnPropertyNames(this);
  675. for (var i = 0; i < ownPropertyNames.length; ++i) {
  676. var name = ownPropertyNames[i];
  677. var index = name >>> 0;
  678. if (String(index) === name && fromIndex <= index && index <= toIndex)
  679. result[index] = this[index];
  680. }
  681. }
  682. return result;
  683. }
  684. /** @this {WebInspector.ArrayGroupingTreeElement} */
  685. function processArrayFragment(arrayFragment)
  686. {
  687. arrayFragment.getAllProperties(processProperties.bind(this));
  688. }
  689. /** @this {WebInspector.ArrayGroupingTreeElement} */
  690. function processProperties(properties, internalProperties)
  691. {
  692. if (!properties)
  693. return;
  694. properties.sort(WebInspector.ObjectPropertiesSection.CompareProperties);
  695. for (var i = 0; i < properties.length; ++i) {
  696. properties[i].parentObject = this._object;
  697. var childTreeElement = new treeElement.treeOutline.section.treeElementConstructor(properties[i]);
  698. childTreeElement._readOnly = true;
  699. treeElement.appendChild(childTreeElement);
  700. }
  701. }
  702. }
  703. /**
  704. * @param {TreeElement|TreeOutline} treeElement
  705. * @param {WebInspector.RemoteObject} object
  706. */
  707. WebInspector.ArrayGroupingTreeElement._populateNonIndexProperties = function(treeElement, object)
  708. {
  709. object.callFunction(buildObjectFragment, undefined, processObjectFragment.bind(this));
  710. /** @this {Object} */
  711. function buildObjectFragment()
  712. {
  713. var result = Object.create(this.__proto__);
  714. var names = Object.getOwnPropertyNames(this);
  715. for (var i = 0; i < names.length; ++i) {
  716. var name = names[i];
  717. // Array index check according to the ES5-15.4.
  718. if (String(name >>> 0) === name && name >>> 0 !== 0xffffffff)
  719. continue;
  720. var descriptor = Object.getOwnPropertyDescriptor(this, name);
  721. if (descriptor)
  722. Object.defineProperty(result, name, descriptor);
  723. }
  724. return result;
  725. }
  726. function processObjectFragment(arrayFragment)
  727. {
  728. arrayFragment.getOwnProperties(processProperties.bind(this));
  729. }
  730. /** @this {WebInspector.ArrayGroupingTreeElement} */
  731. function processProperties(properties, internalProperties)
  732. {
  733. if (!properties)
  734. return;
  735. properties.sort(WebInspector.ObjectPropertiesSection.CompareProperties);
  736. for (var i = 0; i < properties.length; ++i) {
  737. properties[i].parentObject = this._object;
  738. var childTreeElement = new treeElement.treeOutline.section.treeElementConstructor(properties[i]);
  739. childTreeElement._readOnly = true;
  740. treeElement.appendChild(childTreeElement);
  741. }
  742. }
  743. }
  744. WebInspector.ArrayGroupingTreeElement.prototype = {
  745. onpopulate: function()
  746. {
  747. if (this._populated)
  748. return;
  749. this._populated = true;
  750. if (this._propertyCount >= WebInspector.ArrayGroupingTreeElement._bucketThreshold) {
  751. WebInspector.ArrayGroupingTreeElement._populateRanges(this, this._object, this._fromIndex, this._toIndex, false);
  752. return;
  753. }
  754. WebInspector.ArrayGroupingTreeElement._populateAsFragment(this, this._object, this._fromIndex, this._toIndex);
  755. },
  756. onattach: function()
  757. {
  758. this.listItemElement.addStyleClass("name");
  759. },
  760. __proto__: TreeElement.prototype
  761. }
  762. /**
  763. * @constructor
  764. * @extends {WebInspector.TextPrompt}
  765. * @param {boolean=} renderAsBlock
  766. */
  767. WebInspector.ObjectPropertyPrompt = function(commitHandler, cancelHandler, renderAsBlock)
  768. {
  769. WebInspector.TextPrompt.call(this, WebInspector.runtimeModel.completionsForTextPrompt.bind(WebInspector.runtimeModel));
  770. this.setSuggestBoxEnabled("generic-suggest");
  771. if (renderAsBlock)
  772. this.renderAsBlock();
  773. }
  774. WebInspector.ObjectPropertyPrompt.prototype = {
  775. __proto__: WebInspector.TextPrompt.prototype
  776. }