Skip to content

Commit

Permalink
[#2375] Move the sandbox into a seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jul 2, 2012
1 parent ef1f630 commit 97fff6b
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
39 changes: 39 additions & 0 deletions ckan/public/base/javascript/main.js
Expand Up @@ -6,6 +6,45 @@ this.ckan.trans = function (string) {
return string;
};

this.ckan.module = function (name, factory, defaults) {
factory.defaults = defaults || {};
this.modules[name] = factory;
};

this.ckan.modules = {};
this.ckan.setup = function () {
var modules = this.modules;
var _this = this;

jQuery('[data-module]').each(function () {
var data = $(this).data();
var module = modules[data.module];
var options = jQuery.extend({}, module.defaults || {});
var prefix = 'module';
var key;
var sandbox;
var prop;

if (module && typeof module === 'function') {
for (key in data) {
if (key !== prefix && key.indexOf(prefix) === 0) {
prop = key.slice(prefix.length);
prop = prop[0].toLowerCase() + prop.slice(1);

options[prop] = data[key];
}
}

sandbox = _this.sandbox(this, options);
module.call(sandbox, sandbox, sandbox.options, sandbox.trans);
}
});
};

jQuery(function () {
ckan.setup();
});

// Temporary banner to let users on IE7 know that it may not display as
// expected.
(function showIEBanner() {
Expand Down
81 changes: 81 additions & 0 deletions ckan/public/base/javascript/sandbox.js
@@ -0,0 +1,81 @@
this.ckan = this.ckan || {};

(function (ckan, $) {
// An empty jQuery object to use for event management.
ckan.events = jQuery({});

ckan.sandbox = function (element, options) {
return {
/* The jQuery element for the current module */
el: jQuery(element),

/* The options object passed into the module either via data-* attributes
* or the default settings.
*/
options: options,

/* A scoped find function restricted to the current scope. */
$: function (selector) {
return this.el(selector);
},

/* An alias for ckan.trans() */
trans: ckan.trans,

/* Publishes an event to all modules. Can be used to notify other modules
* that an area of the site has changed.
*
* topic - A topic string. These are global to all modules to choose
* them carefully.
* args* - All successive arguments are passed into callbacks.
*
* Returns the sandbox object.
*/
publish: function (topic /* arguments */) {
ckan.events.triggerHandler(topic, [].slice.call(arguments, 1));
return this;
},

/* Subscribes a module to a topic. The callback will receive any
* arguments provided by the publisher.
*
* topic - The topic to subscribe to.
* callback - A function to be called when subscribing.
*
* Returns this sandbox object.
*/
subscribe: function (topic, callback) {
if ($.isPlainObject(topic)) {
$.each(topic, $.proxy(this.subscribe, this));
return this;
}

// Call fn, stripping out the 1st argument (the event object).
function wrapper() {
return callback.apply(this, [].slice.call(arguments, 1));
}

// Add .guid property to function to allow it to be easily unbound. Note
// that $.guid is new in jQuery 1.4+, and $.event.guid was used before.
wrapper.guid = callback.guid = callback.guid || ($.guid += 1);

// Bind the handler.
ckan.events.on(topic, wrapper);
return this;
},

/* Unsubscribes a module from a topic. If no callback is provided then
* all handlers for that topic will be unsubscribed.
*
* topic - The topic to unsubscribe from.
* callback - An optional callback to unsubscribe.
*
* Returns the sandbox object.
*/
unsubscribe: function (topic, callback) {
ckan.events.off(this.el, arguments);
return this;
}
};
};
})(this.ckan, this.jQuery);
2 changes: 2 additions & 0 deletions ckan/templates/base.html
Expand Up @@ -106,6 +106,8 @@
{% endblock %}
#}
{%- block scripts %}
<script src="{% url_for_static "/base/javascript/vendor/jquery.js" %}"></script>
<script src="{% url_for_static '/base/javascript/sandbox.js' %}"></script>
<script src="{% url_for_static '/base/javascript/main.js' %}"></script>
{% endblock -%}
</body>
Expand Down

0 comments on commit 97fff6b

Please sign in to comment.