javascript.txt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 `JSHint`_ code linter to check your code for bugs and style errors.
  18. JSHint will be run when you run the JavaScript tests. We also recommended
  19. installing a JSHint 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. .. code-block:: console
  43. $ pip install closure
  44. $ python django/contrib/admin/bin/compress.py
  45. Behind the scenes, ``compress.py`` is a front-end for Google's
  46. `Closure Compiler`_ which is written in Java. The Closure Compiler library is
  47. not bundled with Django, but you can install it using pip as done above. The
  48. Closure Compiler library requires `Java`_ 7 or higher.
  49. Please don't forget to run ``compress.py`` and include the ``diff`` of the
  50. minified scripts when submitting patches for Django's JavaScript.
  51. .. _javascript-tests:
  52. JavaScript tests
  53. ================
  54. Django's JavaScript tests can be run in a browser or from the command line.
  55. The tests are located in a top level ``js_tests`` directory.
  56. Writing tests
  57. -------------
  58. Django's JavaScript tests use `QUnit`_. Here is an example test module:
  59. .. code-block:: javascript
  60. module('magicTricks', {
  61. beforeEach: function() {
  62. var $ = django.jQuery;
  63. $('#qunit-fixture').append('<button class="button"></button>');
  64. }
  65. });
  66. test('removeOnClick removes button on click', function(assert) {
  67. var $ = django.jQuery;
  68. removeOnClick('.button');
  69. assert.equal($('.button').length === 1);
  70. $('.button').click();
  71. assert.equal($('.button').length === 0);
  72. });
  73. test('copyOnClick adds button on click', function(assert) {
  74. var $ = django.jQuery;
  75. copyOnClick('.button');
  76. assert.equal($('.button').length === 1);
  77. $('.button').click();
  78. assert.equal($('.button').length === 2);
  79. });
  80. Please consult the QUnit documentation for information on the types of
  81. `assertions supported by QUnit <https://api.qunitjs.com/category/assert/>`_.
  82. Running tests
  83. -------------
  84. The JavaScript tests may be run from a web browser or from the command line.
  85. Testing from a web browser
  86. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. To run the tests from a web browser, open up ``js_tests/tests.html`` in your
  88. browser.
  89. To measure code coverage when running the tests, you need to view that file
  90. over HTTP. To view code coverage:
  91. * Execute ``python -m http.server`` (or ``python -m SimpleHTTPServer`` on
  92. Python 2) from the root directory (not from inside ``js_tests``).
  93. * Open http://localhost:8000/js_tests/tests.html in your web browser.
  94. Testing from the command line
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. To run the tests from the command line, you need to have `Node.js`_ installed.
  97. After installing `Node.js`, install the JavaScript test dependencies by running
  98. the following from the root of your Django checkout:
  99. .. code-block:: console
  100. $ npm install
  101. Then run the tests with:
  102. .. code-block:: console
  103. $ npm test
  104. .. _Closure Compiler: https://developers.google.com/closure/compiler/
  105. .. _EditorConfig: http://editorconfig.org/
  106. .. _Java: https://www.java.com
  107. .. _jshint: http://jshint.com/
  108. .. _node.js: https://nodejs.org/
  109. .. _qunit: https://qunitjs.com/