Skip to content

Commit

Permalink
Widgets now subscribe to a widget/ready event. Apply change to all wi…
Browse files Browse the repository at this point in the history
…dgets, added some tests & lots of cleanup.
  • Loading branch information
Dan Hansen committed Jul 14, 2011
1 parent abe10b3 commit ca2413f
Show file tree
Hide file tree
Showing 20 changed files with 555 additions and 398 deletions.
76 changes: 40 additions & 36 deletions public/javascripts/diaspora.js
Expand Up @@ -10,57 +10,61 @@

var Diaspora = { };

Diaspora.WidgetCollection = function() {
this.initialized = false;
this.collection = { };
this.eventsContainer = $({});
};
Diaspora.EventBroker = {
extend: function(obj) {
obj.eventsContainer = $({});

obj.subscribe = Diaspora.EventBroker.subscribe;
obj.publish = Diaspora.EventBroker.publish;

obj.publish = $.proxy(function(eventId, args) {
this.eventsContainer.trigger(eventId, args);
}, obj);

Diaspora.WidgetCollection.prototype.add = function(widgetId, Widget) {
$.extend(Widget.prototype, Diaspora.BaseWidget);
obj.subscribe = $.proxy(function(eventIds, callback, context) {
var eventIds = eventIds.split(" ");

for(var eventId in eventIds) {
this.eventsContainer.bind(eventIds[eventId], $.proxy(callback, context));
}
}, obj);

this[widgetId] = this.collection[widgetId] = new Widget();
if(this.initialized) {
this.collection[widgetId].start();
return obj;
}
};

Diaspora.WidgetCollection.prototype.remove = function(widgetId) {
delete this.collection[widgetId];
};
Diaspora.widgets = {
initialize: false,
collection: {},
constructors: {},

Diaspora.WidgetCollection.prototype.init = function() {
this.initialized = true;
initialize: function() {
this.initialized = true;
Diaspora.EventBroker.extend(this);

for(var widgetId in this.collection) {
if(typeof this.collection[widgetId].start !== "undefined") {
this.collection[widgetId].start();
for(var widgetId in this.collection) {
this.collection[widgetId].publish("widget/ready");
}
};

};
},

Diaspora.WidgetCollection.prototype.subscribe = function(id, callback, context) {
var ids = id.split(" ");
for(var id in ids) {
this.eventsContainer.bind(ids[id], $.proxy(callback, context));
}
};
add: function(widgetId, Widget) {
$.extend(Widget.prototype, Diaspora.EventBroker.extend({}));

Diaspora.WidgetCollection.prototype.publish = function(id, args) {
this.eventsContainer.trigger(id, args);
};
this[widgetId] = this.collection[widgetId] = new Widget();
if(this.initialized) {
this.collection[widgetId].publish("widget/ready");
}
},

Diaspora.BaseWidget = {
require: function(widgetName) {
this[widgetName] = Diaspora.widgets[widgetName];
remove: function(widgetId) {
delete this.collection[widgetId];
}
};

Diaspora.widgets = new Diaspora.WidgetCollection();

window.Diaspora = Diaspora;
})();


$(document).ready(function() { Diaspora.widgets.init(); });
$(function() {
Diaspora.widgets.initialize();
});
59 changes: 31 additions & 28 deletions public/javascripts/widgets/alert.js
@@ -1,34 +1,37 @@
Diaspora.widgets.add("alert", function() {
this.start = function() {
$(document).bind("close.facebox", function() {
if ($("#diaspora_alert").length) {
$("#diaspora_alert").detach();
}
});
};
(function() {
var Alert = function() {
var self = this;

this.faceboxTemplate = '<div id="diaspora_alert" class="facebox_content">' +
'<div class="span-12 last">' +
'<div id="facebox_header">' +
'<h4>' +
'{{title}}' +
'</h4>' +
this.faceboxTemplate = '<div id="diaspora_alert" class="facebox_content">' +
'<div class="span-12 last">' +
'<div id="facebox_header">' +
'<h4>' +
'{{title}}' +
'</h4>' +
'</div>' +
'{{content}}' +
'</div>' +
'{{content}}' +
'</div>' +
'</div>';
'</div>';


this.subscribe("widget/ready", function() {
$(document).bind("close.facebox", function() {
$("#facebox, #diaspora_alert").remove();
});
});

this.alert = function(title, content) {
var template = $.mustache(this.faceboxTemplate, {
title: title,
content: content
});

$(template).appendTo(document.body);
this.alert = function(title, content) {
$($.mustache(self.faceboxTemplate, {
title: title,
content: content
})).appendTo(document.body);

$.facebox({
div: "#diaspora_alert"
}, "diaspora_alert");
}
};

$.facebox({
div: "#diaspora_alert"
}, 'diaspora_alert');
}
});
Diaspora.widgets.add("alert", Alert);
})();
118 changes: 68 additions & 50 deletions public/javascripts/widgets/directionDetector.js
Expand Up @@ -3,66 +3,84 @@
* the COPYRIGHT file.
*/
/* Modified version of https://gitorious.org/statusnet/mainline/blobs/master/plugins/DirectionDetector/jquery.DirectionDetector.js */
(function() {
var DirectionDetector = function() {
var self = this;
this.binds = [];
this.cleaner = new RegExp("@[^ ]+|^RT[: ]{1}| RT | RT: |[♺♻:]+", "g");

Diaspora.widgets.add("directionDetector", function() {

this.start = function() {
Diaspora.widgets.directionDetector.updateBinds();
this.subscribe("widget/ready", function() {
self.updateBinds();

Diaspora.widgets.subscribe("stream/scrolled", function() {
Diaspora.widgets.directionDetector.updateBinds();
Diaspora.widgets.subscribe("stream/scrolled", function() {
self.updateBinds();
});
});
};

this.isRTL = function(str) {
if(typeof str != typeof "" || str.length<1)
this.isRTL = function(str) {
if(typeof str !== "string" || str.length < 1) {
return false;
}

var charCode = str.charCodeAt(0);
if(charCode >= 1536 && charCode <= 1791) // Sarabic, Persian, ...
return true;

else if(charCode >= 65136 && charCode <= 65279) // Arabic present 1
return true;

else if(charCode >= 64336 && charCode <= 65023) // Arabic present 2
return true;

else if(charCode>=1424 && charCode<=1535) // Hebrew
return true;

else if(charCode>=64256 && charCode<=64335) // Hebrew present
return true;

else if(charCode>=1792 && charCode<=1871) // Syriac
return true;

else if(charCode>=1920 && charCode<=1983) // Thaana
return true;

else if(charCode>=1984 && charCode<=2047) // NKo
return true;

else if(charCode>=11568 && charCode<=11647) // Tifinagh
return true;

return false;
var cc = str.charCodeAt(0);
if(cc>=1536 && cc<=1791) // arabic, persian, ...
return true;
if(cc>=65136 && cc<=65279) // arabic peresent 2
return true;
if(cc>=64336 && cc<=65023) // arabic peresent 1
return true;
if(cc>=1424 && cc<=1535) // hebrew
return true;
if(cc>=64256 && cc<=64335) // hebrew peresent
return true;
if(cc>=1792 && cc<=1871) // Syriac
return true;
if(cc>=1920 && cc<=1983) // Thaana
return true;
if(cc>=1984 && cc<=2047) // NKo
return true;
if(cc>=11568 && cc<=11647) // Tifinagh
return true;
return false;
};

this.cleaner = new RegExp('@[^ ]+|^RT[: ]{1}| RT | RT: |[♺♻:]+', 'g');
this.updateBinds = function() {
$.each(self.binds, function(index, bind) {
bind.unbind("keyup", self.updateDirection);
});

this.binds = [];
self.binds = [];

this.updateBinds = function() {
$.each(Diaspora.widgets.directionDetector.binds, function(i, v) {v.unbind('keyup', Diaspora.widgets.directionDetector.updateDirection);});
Diaspora.widgets.directionDetector.binds = [];
$("textarea, input[type='text'], input[type='search']").each(self.bind);
};

$("textarea").each(Diaspora.widgets.directionDetector.bind);
$("input[type='text']").each(Diaspora.widgets.directionDetector.bind);
$("input[type='search']").each(Diaspora.widgets.directionDetector.bind);
};
this.bind = function() {
self.binds.push(
$(this).bind("keyup", self.updateDirection)
);
};

this.bind = function() {
$(this).bind('keyup', Diaspora.widgets.directionDetector.updateDirection);
Diaspora.widgets.directionDetector.binds.push($(this));
this.updateDirection = function() {
var textArea = $(this),
cleaned = textArea.val().replace(self.cleaner, "").replace(/^[ ]+/, "");

if(self.isRTL(cleaned)) {
textArea.css("direction", "rtl");
}
else {
textArea.css("direction", "ltr");
}
};
};

this.updateDirection = function() {
tArea = $(this);
var cleaned = tArea.val().replace(Diaspora.widgets.directionDetector.cleaner, '').replace(/^[ ]+/, '');
if(Diaspora.widgets.directionDetector.isRTL(cleaned))
tArea.css('direction', 'rtl');
else
tArea.css('direction', 'ltr');
};
});
Diaspora.widgets.add("directionDetector", DirectionDetector);
})();

0 comments on commit ca2413f

Please sign in to comment.