diff --git a/Gruntfile.js b/Gruntfile.js index 4d289a828..f75794e40 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -145,6 +145,7 @@ module.exports = function(grunt) { "packages/node_modules/@node-red/editor-client/src/js/ui/common/tabs.js", "packages/node_modules/@node-red/editor-client/src/js/ui/common/stack.js", "packages/node_modules/@node-red/editor-client/src/js/ui/common/typedInput.js", + "packages/node_modules/@node-red/editor-client/src/js/ui/common/toggleButton.js", "packages/node_modules/@node-red/editor-client/src/js/ui/actions.js", "packages/node_modules/@node-red/editor-client/src/js/ui/deploy.js", "packages/node_modules/@node-red/editor-client/src/js/ui/diff.js", diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/common/toggleButton.js b/packages/node_modules/@node-red/editor-client/src/js/ui/common/toggleButton.js new file mode 100644 index 000000000..8e8b13326 --- /dev/null +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/common/toggleButton.js @@ -0,0 +1,105 @@ +/** + * Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ +(function($) { + +/** + * options: + * - invertState : boolean - if "true" the button will show "enabled" when the + * checkbox is not selected and vice versa. + * - enabledIcon : string - the icon for "enabled" state, default "fa-check-square-o" + * - enabledLabel : string - the label for "enabled" state, default "Enabled" ("editor:workspace.enabled") + * - disabledIcon : string - the icon for "disabled" state, default "fa-square-o" + * - disabledLabel : string - the label for "disabled" state, default "Disabled" ("editor:workspace.disabled") + * - class: string - additional classes to apply to the button - eg "red-ui-button-small" + * methods: + * - + */ + $.widget( "nodered.toggleButton", { + _create: function() { + var that = this; + + var invertState = false; + if (this.options.hasOwnProperty("invertState")) { + invertState = this.options.invertState; + } + var enabledIcon = this.options.enabledIcon || "fa-check-square-o"; + var disabledIcon = this.options.disabledIcon || "fa-square-o"; + var enabledLabel = this.options.enabledLabel || RED._("editor:workspace.enabled"); + var disabledLabel = this.options.disabledLabel || RED._("editor:workspace.disabled"); + + this.element.addClass("red-ui-toggleButton"); + this.element.css("display","none"); + this.element.on("focus", function() { + that.button.focus(); + }); + this.button = $(''); + if (this.options.class) { + this.button.addClass(this.options.class) + } + this.element.after(this.button); + this.buttonIcon = this.button.find("i"); + this.buttonLabel = this.button.find("span"); + + // Quick hack to find the maximum width of the button + this.button.addClass("selected"); + this.buttonIcon.addClass(enabledIcon); + this.buttonLabel.text(enabledLabel); + var width = this.button.width(); + this.button.removeClass("selected"); + this.buttonIcon.removeClass(enabledIcon); + that.buttonIcon.addClass(disabledIcon); + that.buttonLabel.text(disabledLabel); + width = Math.max(width,this.button.width()); + this.buttonIcon.removeClass(disabledIcon); + + // Fix the width of the button so it doesn't jump around when toggled + if (width > 0) { + this.button.width(Math.ceil(width)); + } + + this.button.on("click",function(e) { + if (that.buttonIcon.hasClass(disabledIcon)) { + that.button.addClass("selected"); + that.buttonIcon.addClass(enabledIcon); + that.buttonIcon.removeClass(disabledIcon); + that.element.prop("checked",!invertState); + that.buttonLabel.text(enabledLabel); + } else { + that.buttonIcon.addClass(disabledIcon); + that.button.removeClass("selected"); + that.buttonIcon.removeClass(enabledIcon); + that.element.prop("checked",invertState); + that.buttonLabel.text(disabledLabel); + } + }) + + this.element.on("change", function(e) { + if ($(this).prop("checked") !== invertState) { + that.button.addClass("selected"); + that.buttonIcon.addClass(enabledIcon); + that.buttonIcon.removeClass(disabledIcon); + that.buttonLabel.text(enabledLabel); + } else { + that.button.removeClass("selected"); + that.buttonIcon.addClass(disabledIcon); + that.buttonIcon.removeClass(enabledIcon); + that.buttonLabel.text(disabledLabel); + } + }) + this.element.trigger("change"); + } + }); +})(jQuery); diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js b/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js index 35b62cba5..6ccd01e5b 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js @@ -158,9 +158,8 @@ RED.workspaces = (function() { '').appendTo(dialogForm); $('