I am getting error: (index):113 Uncaught ReferenceError: doSearch is not defined at HTMLDocument. ((index):113:17) at j (jquery-2.1.4.min.js:2:26925) at Object.fireWith [as resolveWith] (jquery-2.1.4.min.js:2:27738) at Function.ready (jquery-2.1.4.min.js:2:29532) at HTMLDocument.I (jquery-2.1.4.min.js:2:29723) you want to see any code?
Assistant
Based on the error message, it seems you're encountering a ReferenceError because the doSearch function is not defined but is being called in your code. This appears to be happening during the jQuery document ready event handler.
While the provided code context doesn't show where doSearch is defined, here are a few potential solutions:
First, check if you have properly included all required JavaScript files in your HTML file. The error suggests that the file containing the doSearch function definition may not be loaded.
Make sure your JavaScript files are loaded in the correct order. The file containing the doSearch function should be loaded before any code that tries to use it.
If you're using the search functionality from MusicBlocks, you should ensure that the activity.js file is properly loaded, as it contains the search-related functionality.
To help debug this further, I would need to see:
The relevant section of your HTML file where you include JavaScript files
The code where you're trying to use the function
User
Assistant
Based on the code you've shared, I can see the issue. The problem is with the order of operations and function availability. Here's what's happening:
In your index.html, you're calling doSearch() in the jQuery document.ready handler:
javascript
$(document).ready(function() {
doSearch();
});
However, doSearch() is a method defined inside the Activity class in activity.js, and it's not available globally. It's scoped to the Activity class instance.
User
the code I shared is inside constructor()
Assistant
Ah, I see. Since the code is inside the constructor, we need to ensure that the methods are properly bound and accessible. Here's how you can fix this:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
/*
* Handles resizing for MB.
* Detects width/height changes and closes any menus before actual resize.
* Repositions containers/palette/home buttons
*/
this._onResize = (force) => {
if (!force) {
if (this.saveLocally !== null) {
this.saveLocally();
}
}
const $j = jQuery.noConflict();
let w = 0,
h = 0;
if (typeof platform !== 'undefined' &&
!platform.androidWebkit) {
w = window.innerWidth;
h = window.innerHeight;
} else {
w = window.outerWidth;
h = window.outerHeight;
}
this._clientWidth = document.body.clientWidth;
this._clientHeight = document.body.clientHeight;
this._innerWidth = window.innerWidth;
this._innerHeight = window.innerHeight;
this._outerWidth = window.outerWidth;
this._outerHeight = window.outerHeight;
if (docById("labelDiv").classList.contains("hasKeyboard")) {
return;
}
const smallSide = Math.min(w, h);
let mobileSize;
if (smallSide < this.cellSize * 9) {
mobileSize = false;
/*
if (w < this.cellSize * 10) {
this.turtleBlocksScale = smallSide / (this.cellSize * 11);
} else {
this.turtleBlocksScale = Math.max(smallSide / (this.cellSize * 11), 0.75);
}
*/
} else {
mobileSize = false;
/*
if (w / 1200 > h / 900) {
this.turtleBlocksScale = w / 1200;
} else {
this.turtleBlocksScale = h / 900;
}
*/
}
this.turtleBl
To fix this, you have a few options:
Remove the immediate doSearch call:
The simplest fix would be to remove the document.ready handler that calls doSearch from your index.html, since the search functionality is already properly handled by the showSearchWidget() method which calls doSearch() at the appropriate time.
Wait for Activity initialization:
If you really need to call doSearch on page load, you should wait for the Activity class to be initialized. You could modify your code to use the global activity instance:
javascript
$(document).ready(function() {
// Wait for activity to be initialized
const checkActivity = setInterval(() => {
if (window.globalActivity) {
clearInterval(checkActivity);
window.globalActivity.doSearch();
}
}, 100);
});
Move the search initialization:
Alternatively, you could move any initialization code you need into the Activity class itself, where it has proper access to all methods.
The recommended solution is option #1 - removing the document.ready handler entirely, because:
The search functionality is already properly initialized by the Activity class
The showSearchWidget() method handles the proper setup and timing of the search functionality
It avoids potential race conditions with class initialization
This should resolve the "doSearch is not defined" error you're experiencing.
First, move the doSearch method definition before it's used in the constructor. In the Activity class in activity.js, reorganize the methods like this:
javascript
class Activity {
constructor() {
// First define doSearch
this.doSearch = () => {
const $j = jQuery.noConflict();
const that = this;
$j("#search").autocomplete({
source: that.searchSuggestions,
select: (event, ui) => {
event.preventDefault();
that.searchWidget.value = ui.item.label;
that.searchWidget.idInput_custom = ui.item.value;
that.searchWidget.protoblk = ui.item.specialDict;
that.doSearch();
if (event.keyCode === 13) this.searchWidget.style.visibility = "visible";
},
focus: (event) => {
event.preventDefault();
}
});
// ... rest of doSearch implementation ...
};
// Then use it in showSearchWidget
this.showSearchWidget = () => {
// ... showSearchWidget implementation ...
setTimeout(() => {
that.searchWidget.focus();
that.doSearch();
}, 500);
};
// ... rest of constructor code ...
}
}
Then, modify your document.ready handler to use the global activity instance:
Alternatively, you could remove the document.ready handler entirely and let the search functionality be handled by the Activity class's normal initialization process.
The key points to remember are:
Methods need to be defined before they're used
When using methods within setTimeout or other callbacks, make sure to use arrow functions to preserve the this context
Use that or bind the methods properly to maintain the correct context
The best solution would be to remove the document.ready handler and let the Activity class handle the search initialization through its normal flow, as the search widget is designed to be shown and initialized on demand rather than immediately on page load.
this.searchWidget.value = null;
this.searchWidget.style.visibility = "visible";
this.searchWidget.style.left =
this.palettes.getSearchPos()[0] * this.turtleBlocksScale * 1.5 + "px";
this.searchWidget.style.top =
this.palettes.getSearchPos()[1] * this.turtleBlocksScale * 0.95 + "px";
this.searchBlockPosition = [100, 100];
this.prepSearchWidget();
const that = this;
const closeListener = (e) => {
if (
docById("search").style.visibility === "visible" &&
(e.target === docById("search") || docById("search").contains(e.target))
) {
//do nothing when clicked in the input field
} else if (
docById("ui-id-1") && docById("ui-id-1").style.display === "block" &&
(e.target === docById("ui-id-1") || docById("ui-id-1").contains(e.target))
) {
//do nothing when clicked on the menu
} else if (document.getElementsByTagName("tr")[2].contains(e.target)) {
//do nothing when clicked on the search row
} else if (e.target.id === "myCanvas") {
that.hideSearchWidget();
document.removeEventListener("mousedown", closeListener);
}
};
document.addEventListener("mousedown", closeListener);
// Give the browser time to update before selecting
// focus.
setTimeout(() => {
that.searchWidget.focus();
that.doSearch();
}, 500);
}
};
/*
* Uses JQuery to add autocompleted search suggestions
*/
this.doSearch = () => {
const $j = jQuery.noConflict();
const that = this;
$j("#search").autocomplete({
source: that.searchSuggestions,
select: (event, ui) => {
event.preventDefault();
that.searchWidget.value = ui.item.label;
that.searchWidget.idInput_custom = ui.item.value;
that.searchWidget.protoblk = ui.item.specialDict;
that.doSearch();
if (event.keyCode === 13) this.searchWidget.style.visibility = "visible";
},
focus: (event) => {
event.preventDefault();
}
});
$j("#search").autocomplete("instance")._renderItem = (ul, item) => {
return $j("<li></li>")
.data("item.autocomplete", item)
.append(
'<img src="' +
item.artwork +
'" height="20px">' +
"<a> " +
item.label +
"</a>"
)
.appendTo(ul.css({
"z-index": 9999,
"max-height": "200px",
"overflow-y": "auto"
}));
};
const searchInput = this.searchWidget.idInput_custom;
if (!searchInput || searchInput.length <= 0) return;
const protoblk = this.searchWidget.protoblk;
const paletteName = protoblk.palette.name;
const protoName = protoblk.name;
// eslint-disable-next-line no-prototype-builtins
if (this.blocks.protoBlockDict.hasOwnProperty(protoName)) {
this.palettes.dict[paletteName].makeBlockFromSearch(
[Redacted Person],
protoName,
(newBlock) => {
that.blocks.moveBlock(
newBlock,
100 + that.searchBlockPosition[0] - that.blocksContainer.x,
that.searchBlockPosition[1] - that.blocksContainer.y
);
}
);
// Move the position of the next newly created block.
this.searchBlockPosition[0] += STANDARDBLOCKHEIGHT;
this.searchBlockPosition[1] += STANDARDBLOCKHEIGHT;
} else if (this.deprecatedBlockNames.indexOf(searchInput) > -1) {
this.errorMsg(_("This block is deprecated."));
} else {
this.errorMsg(_("Block cannot be found."));
}
this.searchWidget.value = "";
this.update = true;
};