+ for (var teamName in mkws.teams) {
+ mkws.teams[teamName].queue("ready").publish();
+ }
+ }
+
+
+ function selectorForAllWidgets() {
+ if (mkws.config && mkws.config.scan_all_nodes) {
+ // This is the old version, which works by telling jQuery to
+ // find every node that has a class beginning with "mkws". In
+ // theory it should be slower than the class-based selector; but
+ // instrumentation suprisnigly shows this is consistently
+ // faster. It also has the advantage that any widgets of
+ // non-registered types are logged as warnings rather than
+ // silently ignored.
+ return '[class^="mkws"],[class*=" mkws"]';
+ } else {
+ // This is the new version, which works by looking up the
+ // specific classes of all registered widget types and their
+ // resize containers. Because all it requires jQuery to do is
+ // some hash lookups in pre-built tables, it should be very
+ // fast; but it silently ignores widgets of unregistered types.
+ var s = "";
+ for (var type in mkws.widgetType2function) {
+ if (s) s += ',';
+ s += '.mkws' + type;
+ s += ',.mkws' + type + "-Container-wide";
+ s += ',.mkws' + type + "-Container-narrow";
+ }
+ return s;
+ }
+ }
+
+
+ function makeWidgetsWithin(level, node) {
+ if (node) var widgetNodes = node.find(selectorForAllWidgets());
+ else widgetNodes = $(selectorForAllWidgets());
+ // Return false if we parse no widgets
+ if (widgetNodes.length < 1) return false;
+ widgetNodes.each(function() {
+ handleNodeWithTeam(this, function(tname, type) {
+ var myTeam = mkws.teams[tname];
+ if (!myTeam) {
+ myTeam = mkws.teams[tname] = team($, tname);
+ log("made MKWS team '" + tname + "'");
+ }
+
+ var oldHTML = this.innerHTML;
+ var myWidget = widget($, myTeam, type, this);
+ myTeam.addWidget(myWidget);
+ var newHTML = this.innerHTML;
+ if (newHTML !== oldHTML) {
+ log("widget " + tname + ":" + type + " HTML changed: reparsing");
+ makeWidgetsWithin(level+1, $(this));
+ }
+ });
+ });
+ return true;
+ }
+
+
+ // The second "rootsel" parameter is passed to jQuery and is a DOM node
+ // or a selector string you would like to constrain the search for widgets to.
+ //
+ // This function has no side effects if run again on an operating session,
+ // even if the element/selector passed causes existing widgets to be reparsed:
+ //
+ // (TODO: that last bit isn't true and we currently have to avoid reinitialising
+ // widgets, MKWS-261)
+ //
+ // * configuration is not regenerated
+ // * authentication is not performed again
+ // * autosearches are not re-run
+ mkws.init = function(message, rootsel) {
+ var greet = "MKWS initialised";
+ if (rootsel) greet += " (limited to " + rootsel + ")"
+ if (message) greet += " :: " + message;
+ mkws.log(greet);
+
+ // TODO: Let's remove this soon
+ // Backwards compatibility: set new magic class names on any
+ // elements that have the old magic IDs.
+ var ids = [ "Switch", "Lang", "Search", "Pager", "Navi",
+ "Results", "Records", "Targets", "Ranking",
+ "Termlists", "Stat", "MOTD" ];
+ for (var i = 0; i < ids.length; i++) {
+ var id = 'mkws' + ids[i];
+ var node = $('#' + id);
+ if (node.attr('id')) {
+ node.addClass(id);
+ log("added magic class to '" + node.attr('id') + "'");
+ }
+ }