utils.service.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * AccessibleBlockly
  3. *
  4. * Copyright 2016 Google Inc.
  5. * https://developers.google.com/blockly/
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the 'License');
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an 'AS IS' BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /**
  20. * @fileoverview Angular2 utility service for multiple components. All
  21. * functions in this service should be stateless, since this is a singleton
  22. * service that is used for the entire application.
  23. *
  24. * @author madeeha@google.com (Madeeha Ghori)
  25. */
  26. var blocklyApp = {};
  27. blocklyApp.UtilsService = ng.core
  28. .Class({
  29. constructor: function() {},
  30. generateUniqueId: function() {
  31. return 'blockly-' + Blockly.genUid();
  32. },
  33. generateIds: function(elementsList) {
  34. var idMap = {};
  35. for (var i = 0; i < elementsList.length; i++){
  36. idMap[elementsList[i]] = this.generateUniqueId();
  37. }
  38. return idMap;
  39. },
  40. generateAriaLabelledByAttr: function(mainLabel, secondLabel) {
  41. return mainLabel + (secondLabel ? ' ' + secondLabel : '');
  42. },
  43. getInputTypeLabel: function(connection) {
  44. // Returns the input type name, or 'any' if any official input type
  45. // qualifies.
  46. if (connection.check_) {
  47. return connection.check_.join(', ');
  48. } else {
  49. return Blockly.Msg.ANY;
  50. }
  51. },
  52. getBlockTypeLabel: function(inputBlock) {
  53. if (inputBlock.type == Blockly.NEXT_STATEMENT) {
  54. return Blockly.Msg.BLOCK;
  55. } else {
  56. return Blockly.Msg.VALUE;
  57. }
  58. },
  59. getBlockDescription: function(block) {
  60. // We use 'BLANK' instead of the default '?' so that the string is read
  61. // out. (By default, screen readers tend to ignore punctuation.)
  62. return block.toString(undefined, 'BLANK');
  63. },
  64. isWorkspaceEmpty: function() {
  65. return !blocklyApp.workspace.topBlocks_.length;
  66. },
  67. getBlockById: function(blockId) {
  68. return this.getBlockByIdFromWorkspace(blockId, blocklyApp.workspace);
  69. },
  70. getBlockByIdFromWorkspace: function(blockId, workspace) {
  71. // This is used for non-default workspaces, such as those comprising the
  72. // toolbox.
  73. return workspace.getBlockById(blockId);
  74. }
  75. });