javascript.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ==========
  2. JavaScript
  3. ==========
  4. While most of Django core is Python, the ``admin`` and ``gis`` contrib apps
  5. contain JavaScript code.
  6. Please follow these coding standards when writing JavaScript code for inclusion
  7. in Django.
  8. Code style
  9. ==========
  10. * Please conform to the indentation style dictated in the ``.editorconfig``
  11. file. We recommend using a text editor with `EditorConfig`_ support to avoid
  12. indentation and whitespace issues. Most of the JavaScript files use 4 spaces
  13. for indentation, but there are some exceptions.
  14. * When naming variables, use ``camelCase`` instead of ``underscore_case``.
  15. Different JavaScript files sometimes use a different code style. Please try to
  16. conform to the code style of each file.
  17. * Use the `ESLint`_ code linter to check your code for bugs and style errors.
  18. ESLint will be run when you run the JavaScript tests. We also recommended
  19. installing a ESLint plugin in your text editor.
  20. * Where possible, write code that will work even if the page structure is later
  21. changed with JavaScript. For instance, when binding a click handler, use
  22. ``$('body').on('click', selector, func)`` instead of
  23. ``$(selector).click(func)``. This makes it easier for projects to extend
  24. Django's default behavior with JavaScript.
  25. .. _javascript-patches:
  26. JavaScript patches
  27. ==================
  28. Django's admin system leverages the jQuery framework to increase the
  29. capabilities of the admin interface. In conjunction, there is an emphasis on
  30. admin JavaScript performance and minimizing overall admin media file size.
  31. Serving compressed or "minified" versions of JavaScript files is considered
  32. best practice in this regard.
  33. To that end, patches for JavaScript files should include both the original
  34. code for future development (e.g. ``foo.js``), and a compressed version for
  35. production use (e.g. ``foo.min.js``). Any links to the file in the codebase
  36. should point to the compressed version.
  37. Compressing JavaScript
  38. ----------------------
  39. To simplify the process of providing optimized JavaScript code, Django
  40. includes a handy Python script which should be used to create a "minified"
  41. version. To run it:
  42. .. console::
  43. $ python django/contrib/admin/bin/compress.py
  44. Behind the scenes, ``compress.py`` is a front-end for Google's
  45. `Closure Compiler`_ which is written in Java. The Closure Compiler library is
  46. not bundled with Django, but will be installed automatically by ``npm``. The
  47. Closure Compiler library requires `Java`_ 7 or higher.
  48. Please don't forget to run ``compress.py`` and include the ``diff`` of the
  49. minified scripts when submitting patches for Django's JavaScript.
  50. .. _javascript-tests:
  51. JavaScript tests
  52. ================
  53. Django's JavaScript tests can be run in a browser or from the command line.
  54. The tests are located in a top level ``js_tests`` directory.
  55. Writing tests
  56. -------------
  57. Django's JavaScript tests use `QUnit`_. Here is an example test module:
  58. .. code-block:: javascript
  59. QUnit.module('magicTricks', {
  60. beforeEach: function() {
  61. var $ = django.jQuery;
  62. $('#qunit-fixture').append('<button class="button"></button>');
  63. }
  64. });
  65. QUnit.test('removeOnClick removes button on click', function(assert) {
  66. var $ = django.jQuery;
  67. removeOnClick('.button');
  68. assert.equal($('.button').length, 1);
  69. $('.button').click();
  70. assert.equal($('.button').length, 0);
  71. });
  72. QUnit.test('copyOnClick adds button on click', function(assert) {
  73. var $ = django.jQuery;
  74. copyOnClick('.button');
  75. assert.equal($('.button').length, 1);
  76. $('.button').click();
  77. assert.equal($('.button').length, 2);
  78. });
  79. Please consult the QUnit documentation for information on the types of
  80. `assertions supported by QUnit <https://api.qunitjs.com/assert/>`_.
  81. Running tests
  82. -------------
  83. The JavaScript tests may be run from a web browser or from the command line.
  84. Testing from a web browser
  85. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  86. To run the tests from a web browser, open up ``js_tests/tests.html`` in your
  87. browser.
  88. To measure code coverage when running the tests, you need to view that file
  89. over HTTP. To view code coverage:
  90. * Execute ``python -m http.server`` from the root directory (not from inside
  91. ``js_tests``).
  92. * Open http://localhost:8000/js_tests/tests.html in your web browser.
  93. Testing from the command line
  94. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. To run the tests from the command line, you need to have `Node.js`_ installed.
  96. After installing ``Node.js``, install the JavaScript test dependencies by
  97. running the following from the root of your Django checkout:
  98. .. console::
  99. $ npm install
  100. Then run the tests with:
  101. .. console::
  102. $ npm test
  103. .. _Closure Compiler: https://developers.google.com/closure/compiler/
  104. .. _EditorConfig: https://editorconfig.org/
  105. .. _Java: https://www.java.com
  106. .. _eslint: https://eslint.org/
  107. .. _node.js: https://nodejs.org/
  108. .. _qunit: https://qunitjs.com/