-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Move alert/confirm/prompt to a new file, dialog.js #5457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
89273c5
Migrate prompt/alert/confirm to dedicated module
gonfunko d2516be
Update core/blockly.js to pass through calls to prompt/alert/confirm …
gonfunko 6e4893c
Update calls to Blockly.prompt/alert/confirm to dialog.prompt/alert/c…
gonfunko f2322e7
Fix typo and errant redeclaration of Blockly.prompt
gonfunko 22902c4
Clarify JSDoc on customizing Blockly.dialog.alert/confirm/prompt
gonfunko 5fce34e
Merge branch 'goog_module' into dialogs
gonfunko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2021 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @fileoverview Wrapper functions around JS functions for showing | ||
| * alert/confirmation dialogs. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| goog.module('Blockly.dialog'); | ||
| goog.module.declareLegacyNamespace(); | ||
|
|
||
| let alertImplementation = function(message, opt_callback) { | ||
| window.alert(message); | ||
| if (opt_callback) { | ||
| opt_callback(); | ||
| } | ||
| }; | ||
|
|
||
| let confirmImplementation = function(message, callback) { | ||
| callback(window.confirm(message)); | ||
| }; | ||
|
|
||
| let promptImplementation = function(message, defaultValue, callback) { | ||
| callback(window.prompt(message, defaultValue)); | ||
| }; | ||
|
|
||
| /** | ||
| * Wrapper to window.alert() that app developers may override via setAlert to | ||
| * provide alternatives to the modal browser window. | ||
| * @param {string} message The message to display to the user. | ||
| * @param {function()=} opt_callback The callback when the alert is dismissed. | ||
| */ | ||
| const alert = function(message, opt_callback) { | ||
| alertImplementation(message, opt_callback); | ||
| }; | ||
| exports.alert = alert; | ||
|
|
||
| /** | ||
| * Sets the function to be run when Blockly.dialog.alert() is called. | ||
| * @param {!function(string, function()=)} alertFunction The function to be run. | ||
| * @see Blockly.dialog.alert | ||
| */ | ||
| const setAlert = function(alertFunction) { | ||
| alertImplementation = alertFunction; | ||
| }; | ||
| exports.setAlert = setAlert; | ||
|
|
||
| /** | ||
| * Wrapper to window.confirm() that app developers may override via setConfirm | ||
| * to provide alternatives to the modal browser window. | ||
| * @param {string} message The message to display to the user. | ||
| * @param {!function(boolean)} callback The callback for handling user response. | ||
| */ | ||
| const confirm = function(message, callback) { | ||
| confirmImplementation(message, callback); | ||
| }; | ||
| exports.confirm = confirm; | ||
|
|
||
| /** | ||
| * Sets the function to be run when Blockly.dialog.confirm() is called. | ||
| * @param {!function(string, !function(boolean))} confirmFunction The function | ||
| * to be run. | ||
| * @see Blockly.dialog.confirm | ||
| */ | ||
| const setConfirm = function(confirmFunction) { | ||
| confirmImplementation = confirmFunction; | ||
| }; | ||
| exports.setConfirm = setConfirm; | ||
|
|
||
| /** | ||
| * Wrapper to window.prompt() that app developers may override via setPrompt to | ||
| * provide alternatives to the modal browser window. Built-in browser prompts | ||
| * are often used for better text input experience on mobile device. We strongly | ||
| * recommend testing mobile when overriding this. | ||
| * @param {string} message The message to display to the user. | ||
| * @param {string} defaultValue The value to initialize the prompt with. | ||
| * @param {!function(?string)} callback The callback for handling user response. | ||
| */ | ||
| const prompt = function(message, defaultValue, callback) { | ||
| promptImplementation(message, defaultValue, callback); | ||
| }; | ||
| exports.prompt = prompt; | ||
|
|
||
| /** | ||
| * Sets the function to be run when Blockly.dialog.prompt() is called. | ||
| * @param {!function(string, string, !function(?string))} promptFunction The | ||
| * function to be run. | ||
| * @see Blockly.dialog.prompt | ||
| */ | ||
| const setPrompt = function(promptFunction) { | ||
| promptImplementation = promptFunction; | ||
| }; | ||
| exports.setPrompt = setPrompt; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why create a wrapper here, instead of just doing
exports.alert = alertImplementation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because that exports the initial value of alertImplementation, so even if alertImplementation gets modified via the setter, the initial/default implementation will continue to be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that makes sense.
The comment is no longer correct (for this and the others) because to override you would call
setAlertrather than overridingalert.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the JSDoc to clarify, thanks!