blob: 003f2359cab339646632a981a2519578b850e33e [file]
#summary Proposal about Javascript refactoring
#labels Importance-Useful,Phase-Implementation,Contents-Draft
*Warning*: this is a proposal, and references to code not yet pushed in the main repository. Current scripts and tools can be found on the [http://bitbucket.org/dhans/statistic-module-for-melange/ statistics GSoC branch], the following are links to provisional refactored code in [http://bitbucket.org/dhans/statistic-module-for-melange/src/tip/app/soc/content/js/melange.js melange.js] and [http://bitbucket.org/dhans/statistic-module-for-melange/src/tip/app/soc/content/js/melange.graph.js melange.graph.js]
=Introduction=
With more Javascript code being written and deployed, global namespace pollution is going to be a risk. So, we have two options: handle Java-like packages and dependencies between modules with projects like [http://code.google.com/p/jingo/ Jingo] or keep Python to handle all this stuff, just giving it the potential to handle dependencies.
=Tools=
==jsLint==
To run jsLint and check melange.js and melange.graph.js files, you need java installed in your local computer, just run
{{{
./jsLint.sh
}}}
in {{{ scripts/ }}} directory.
The check is based to a slightly modified source of [http://www.jslint.com/rhino/index.html Jslint rhino version].
Following the defaults:
{{{
bitwise:false,
eqeqeq:true,
immed:true,
laxbreak:false,
newcap:false,
nomen:false,
onevar:false,
plusplus:false,
regexp:false,
browser:true,
undef:true,
white:true,
indent:2,
predef:['window','jQuery']
}}}
In practice:
* check for use of == and != (always use === and !==)
* check if immediate function calling are surrounded by parenthesis
* disallow uncorrect breaking of lines
* assumes a browser
* prevent undefined variables
* check whitespaces before an anonymous function calling
* check if there are 2 whitespaces per indentation
* ignores {{{ window }} and {{{ jQuery }}} being global variables
* do not tolerate use of {{{ eval }}} statement
* do not tolerate unfiltered {{{ for...in }}} statements
If you want to check your code copying and pasting in the [http://www.jslint.com/ online version of jsLint] (you will receive more useful infos as output), set the following [http://www.jslint.com/#options jsLint options]:
* Strict white space
* Assume a browser
* Disallow undefined variables
* Disallow {{{ == }}} and {{{ != }}}
* Require parens around immediate invocation
* Strict white space indentation: set it to *2*
==jsUnit==
Provisional tests for [http://bitbucket.org/dhans/statistic-module-for-melange/src/tip/app/soc/content/js/melange.js melange.js] and [http://bitbucket.org/dhans/statistic-module-for-melange/src/tip/app/soc/content/js/melange.graph.js melange.graph.js] can be found in {{{ tests/javascript/melange.tests.html }}}, [http://bitbucket.org/dhans/statistic-module-for-melange/src/tip/tests/javascript/melange.tests.html source code for tests in the repository can be found here].
To run jsUnit, you need to point your browser to this file:
{{{ ./thirdparty/jsunit/testRunner.html }}}
In the input line that will be shown, insert the path (URL style) of
{{{ ./tests/javascript/melange.tests.html }}}
starting from your home directory.
Then you just need to click "Run", and it will run the test against the code. Fill free to set "Test level" to "debug (highest)" to see more details.
==jsCoverage==
==JSDocToolkit==
==Shrinksafe==
=Guidelines=
In any way, we need:
1. To separate Javascript code in *modules*, and use Javascript language features to _namespace correctly_ those modules.
2. To keep as much Javascript code as possible *outside* Django templates.
3. To put HTML strings *outside* Javascript code, eventually developing a little function to load those templates and then, for templating, something that emulates {{{ sprintf() }}} or begin to use [http://plugins.jquery.com/project/jquerytemplate jQuery Template plugin].
4. Use {{{ jQuery.noConflict() }}} (after loading any library/plugin) and giving jQuery a different namespace like {{{ $j }}} to avoid conflicts if we ever want to integrate other libraries ({{{ $ }}} namespace is very well overused)
The proposal is to create a {{{ melange }}} namespace and begin adding sub-namespace to it for every feature we need. We might want to add {{{ models }}}, {{{ logic }}} and {{{ templates }}} subdirectories to the {{{ content/js }}} as well, to keep an homogeneous structure with the python code.