Alexander Nazarov (nfokz) wrote in changelog,
Alexander Nazarov
nfokz
changelog

[livejournal] r22298: LJSUP-12631: Implement widget initializa...

Committer: anazarov
LJSUP-12631: Implement widget initialization framework.
U   trunk/htdocs/js/basic.js
U   trunk/htdocs/js/livejournal.js
Modified: trunk/htdocs/js/basic.js
===================================================================
--- trunk/htdocs/js/basic.js	2012-06-21 07:29:16 UTC (rev 22297)
+++ trunk/htdocs/js/basic.js	2012-06-21 09:00:43 UTC (rev 22298)
@@ -710,6 +710,110 @@
 	}
 };
 
+(function () {
+	var widgets = {},
+		unique = 1,
+		baseClass = 'lj-widget',
+		selector = '.' + baseClass;
+
+	LJ.UI._widgets = widgets;
+
+	/**
+	 * Init widget on node
+	 *
+	 * @param {jQuery} node Node
+	 */
+	LJ.UI.initWidget = function (node, force) {
+		var widget = node.data('widget'),
+			bootstrap = node.data('bootstrap') || null;
+
+		if (node.attr('data-widget-id')) {
+			/* Widget already has unique id */
+			return;
+		}
+
+		if (force) {
+			if (typeof jQuery.fn[widget] === 'function') {
+				jQuery.fn[widget].apply(node);
+			} else {
+				LJ.console.warn('Widget ' + widget + ' was not loaded');
+				return;
+			}
+		}
+
+		widgets[++unique] = {
+			ready: !!force,
+			entryPoint: bootstrap,
+			name: widget,
+			node: node
+		};
+
+		node
+			.attr('data-widget-id', unique)
+			.addClass(baseClass)
+			.addClass(baseClass + '-' + unique);
+	};
+
+	/**
+	 * Cleanup widget by node
+	 *
+	 * @param {jQuery} node Node
+	 */
+	LJ.UI.removeWidget = function (node) {
+		var id = node.data('widget-id');
+
+		if (!node.is(baseClass)) {
+			LJ.console.warn('Widget was not found on node');
+			return;
+		}
+
+		if (!id in widgets) {
+			LJ.console.warn('Widget ' + id + ' was removed already or never created');
+			return;
+		}
+
+		delete widgets[id];
+	};
+
+	/**
+	 * Init widgets with specific entry point or all remaining 
+	 *
+	 * @param {String} entryPoint Entry point name
+	 */
+	LJ.UI.bootstrap = function (entryPoint) {
+		var widget, unique, fn;
+
+		jQuery(selector).each(function () {
+			LJ.UI.initWidget(jQuery(this));
+		});
+
+		for (unique in widgets) {
+			if (!widgets.hasOwnProperty(unique)) {
+				continue;
+			}
+
+			widget = widgets[unique];
+
+			if (widget.ready) {
+				continue;
+			}
+
+			if (!entryPoint || widget.entryPoint === entryPoint) {
+				fn = jQuery.fn[widget.name];
+
+				if (typeof fn === 'function') {
+					fn.apply(widget.node);
+					widget.ready = true;
+				} else {
+					LJ.console.warn('Widget ' + widget.name + ' was not loaded');
+					continue;
+				}
+			} 
+		}
+	};
+}());
+
+
 LJ.ml = function(key, dict, def) {
 	var str = '', tmpl;
 	dict = dict || {};

Modified: trunk/htdocs/js/livejournal.js
===================================================================
--- trunk/htdocs/js/livejournal.js	2012-06-21 07:29:16 UTC (rev 22297)
+++ trunk/htdocs/js/livejournal.js	2012-06-21 09:00:43 UTC (rev 22298)
@@ -41,6 +41,8 @@
 		LJ.Api.init({ auth_token: Site.auth_token });
 	}
 
+	LJ.UI.bootstrap();
+
 	//register system hooks
 	LiveJournal.register_hook('update_wallet_balance', LiveJournal.updateWalletBalance);
 	LiveJournal.register_hook('xdr/message', LiveJournal.processXdr);

Tags: anazarov, js, livejournal, nfokz
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments