From 11f5cb0fb370f87a1825d5bf9442bdec8a8625ac Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Thu, 12 Dec 2013 22:32:03 +0100 Subject: [PATCH] [#1377] Add this.options and this.el javascript example --- ckanext/example_theme/v17_popover/__init__.py | 0 .../fanstatic/example_theme_popover.js | 35 ++++++++++ ckanext/example_theme/v17_popover/plugin.py | 1 + .../templates/snippets/package_item.html | 16 +++++ doc/theming/best-practices.rst | 12 ++++ doc/theming/javascript.rst | 67 +++++++++++++++++++ setup.py | 1 + 7 files changed, 132 insertions(+) create mode 100644 ckanext/example_theme/v17_popover/__init__.py create mode 100644 ckanext/example_theme/v17_popover/fanstatic/example_theme_popover.js create mode 120000 ckanext/example_theme/v17_popover/plugin.py create mode 100644 ckanext/example_theme/v17_popover/templates/snippets/package_item.html diff --git a/ckanext/example_theme/v17_popover/__init__.py b/ckanext/example_theme/v17_popover/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/ckanext/example_theme/v17_popover/fanstatic/example_theme_popover.js b/ckanext/example_theme/v17_popover/fanstatic/example_theme_popover.js new file mode 100644 index 00000000000..a8312e99cc4 --- /dev/null +++ b/ckanext/example_theme/v17_popover/fanstatic/example_theme_popover.js @@ -0,0 +1,35 @@ +/* example_theme_popover + * + * This JavaScript module adds a Bootstrap popover with some extra info about a + * dataset to the HTML element that the module is applied to. Users can click + * on the HTML element to show the popover. + * + * title - the title of the dataset + * license - the title of the dataset's copyright license + * num_resources - the number of resources that the dataset has. + * + */ +ckan.module('example_theme_popover', function (jQuery, _) { + return { + initialize: function () { + + // Access some options passed to this JavaScript module by the calling + // template. + var num_resources = this.options.num_resources; + var license = this.options.license; + + // Format a simple string with the number of resources and the license, + // e.g. "3 resources, Open Data Commons Attribution License". + var content = 'NUM resources, LICENSE' + .replace('NUM', this.options.num_resources) + .replace('LICENSE', this.options.license) + + // Add a Bootstrap popover to the HTML element (this.el) that this + // JavaScript module was initialized on. + this.el.popover({title: this.options.title, + content: content, + placement: 'left'}); + } + }; +}); + diff --git a/ckanext/example_theme/v17_popover/plugin.py b/ckanext/example_theme/v17_popover/plugin.py new file mode 120000 index 00000000000..1bef9f0d987 --- /dev/null +++ b/ckanext/example_theme/v17_popover/plugin.py @@ -0,0 +1 @@ +../v16_initialize_a_javascript_module/plugin.py \ No newline at end of file diff --git a/ckanext/example_theme/v17_popover/templates/snippets/package_item.html b/ckanext/example_theme/v17_popover/templates/snippets/package_item.html new file mode 100644 index 00000000000..c1c7ce1c4ee --- /dev/null +++ b/ckanext/example_theme/v17_popover/templates/snippets/package_item.html @@ -0,0 +1,16 @@ +{% ckan_extends %} + +{% block package_item_content %} + {{ super() }} + {% resource 'example_theme/example_theme_popover.js' %} + + {# Apply out JavaScript module to an HTML +{% endblock %} diff --git a/doc/theming/best-practices.rst b/doc/theming/best-practices.rst index bea48709dde..1ea6ddf436e 100644 --- a/doc/theming/best-practices.rst +++ b/doc/theming/best-practices.rst @@ -72,6 +72,18 @@ example: ``fanstatic/example_theme_popover.js``: .. literalinclude:: /../ckanext/example_theme/v16_initialize_a_javascript_module/fanstatic/example_theme_popover.js +.. _javascript module docstrings best practice: + +------------------------------------------- +|javascript| modules should have docstrings +------------------------------------------- + +A |javascript| module should have a docstring at the top of the file, briefly +documentating what the module does and what options it takes. For example: + +.. literalinclude:: /../ckanext/example_theme/v17_popover/fanstatic/example_theme_popover.js + :language: javascript + -------------------------------------------- Use ``{% snippet %}``, not ``{% include %}`` diff --git a/doc/theming/javascript.rst b/doc/theming/javascript.rst index db54e56c01e..cd420f1e72b 100644 --- a/doc/theming/javascript.rst +++ b/doc/theming/javascript.rst @@ -30,6 +30,7 @@ To get CKAN to call some custom JavaScript code, we need to: function? I've seen ``jQuery`` and ``$`` and ``i18n`` and ``_``. .. literalinclude:: /../ckanext/example_theme/v16_initialize_a_javascript_module/fanstatic/example_theme_popover.js + :language: javascript .. note:: @@ -56,6 +57,7 @@ To get CKAN to call some custom JavaScript code, we need to: contents: .. literalinclude:: /../ckanext/example_theme/v16_initialize_a_javascript_module/templates/snippets/package_item.html + :language: django .. todo:: Link to something about HTML data-* attributes. @@ -82,6 +84,71 @@ To get CKAN to call some custom JavaScript code, we need to: own ``