ardublockly_desktop.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * @license Licensed under the Apache License, Version 2.0 (the "License"):
  3. * http://www.apache.org/licenses/LICENSE-2.0
  4. *
  5. * @fileoverview Front end code relevant only to the Desktop version of
  6. * Ardublockly.
  7. */
  8. 'use strict';
  9. /** Create a namespace for the application. */
  10. var Ardublockly = Ardublockly || {};
  11. /**
  12. * Checks if the current JavaScript is loaded in the rendered process of
  13. * Electron. Works even if the node integration is turned off.
  14. * @return {!boolean} True if Ardublockly running in Electron application
  15. */
  16. Ardublockly.isRunningElectron = function() {
  17. return navigator.userAgent.toLowerCase().indexOf('ardublockly') > -1;
  18. };
  19. (function loadJsInElectron(){
  20. if (Ardublockly.isRunningElectron()) {
  21. var projectLocator = require('electron').remote.require('./projectlocator.js');
  22. var projectRoot = projectLocator.getProjectRootPath();
  23. window.$ = window.jQuery = require(projectRoot +
  24. '/ardublockly/js_libs/jquery-2.1.3.min.js');
  25. window.Hammer = require(projectRoot + '/ardublockly/js_libs/hammer.min.js');
  26. window.JsDiff = require(projectRoot + '/ardublockly/js_libs/diff.js');
  27. }
  28. })();
  29. /** Sets all the elements using the container class to have a width of 100%. */
  30. Ardublockly.containerFullWidth = function() {
  31. var containers = $('.container');
  32. for (var i = 0; i < containers.length; i++) {
  33. containers[i].style.width = '100%';
  34. }
  35. };
  36. /** Hides the side menu button. */
  37. Ardublockly.hideSideMenuButton = function() {
  38. var sideMenuButton = document.getElementById('button-collapse');
  39. sideMenuButton.style.setProperty ('display', 'none', 'important');
  40. };
  41. Ardublockly.htmlPrompt = function(message, defaultValue, callback) {
  42. $('#gen_prompt_message').text('');
  43. $('#gen_prompt_message').append(message);
  44. $('#gen_prompt_input').val(defaultValue);
  45. // Bind callback events to buttons
  46. $('#gen_prompt_ok_link').bind('click', function() {
  47. callback($('#gen_prompt_input').val());
  48. });
  49. $('#gen_prompt_cancel_link').bind('click', function() {
  50. callback(null);
  51. });
  52. $('#gen_prompt').openModal();
  53. window.location.hash = '';
  54. };
  55. /** Initialize Ardublockly code required for Electron on page load. */
  56. window.addEventListener('load', function load(event) {
  57. window.removeEventListener('load', load, false);
  58. if (Ardublockly.isRunningElectron()) {
  59. // Edit the page layout for better appearance on desktop
  60. Ardublockly.containerFullWidth();
  61. Ardublockly.hideSideMenuButton();
  62. // Prevent browser zoom changes like pinch-to-zoom
  63. var webFrame = require('electron').webFrame;
  64. webFrame.setZoomLevelLimits(1, 1);
  65. // Electron does not offer a prompt, so replace Blocks version with modal
  66. // Original signature: function(message, opt_defaultInput, opt_callback)
  67. Blockly.prompt = Ardublockly.htmlPrompt;
  68. }
  69. });