Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'abie/master'

  • Loading branch information...
commit db555da4efb6c324d64cdd28b5e9f8a2de45d6cc 2 parents 7988279 + a05da5f
Justine Tunney authored
View
1  .gitignore
@@ -5,6 +5,7 @@ occupywallst/settings_dev_local.py
chat/settings_local.json
chat/node_modules
occupywallst.min.js
+occupywallst/media
core
*.pyc
View
16 occupywallst/admin.py
@@ -17,14 +17,17 @@
from django.contrib.gis.admin import OSMGeoAdmin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin, GroupAdmin
-from occupywallst import models as db
+from imagekit.admin import AdminThumbnail
+from occupywallst import models as db
+from occupywallst import widgets
class AdminSite(BaseAdminSite):
def __init__(self, *args, **kwargs):
BaseAdminSite.__init__(self, *args, **kwargs)
self.register(db.User, UserAdmin)
self.register(db.Group, GroupAdmin)
+ self.register(db.Carousel, CarouselAdmin)
self.register(db.Verbiage, VerbiageAdmin)
self.register(db.NewsArticle, ArticleAdmin)
self.register(db.ForumPost, ArticleAdmin)
@@ -80,6 +83,17 @@ class VerbiageAdmin(GeoAdmin):
list_display = ('name', verbiage_type, content_field(125))
+class PhotoInline(admin.TabularInline):
+ model = db.Photo
+ extra = 1
+ fields = ('original_image', 'url', 'caption')
+
+class CarouselAdmin(admin.ModelAdmin):
+ inlines = [
+ PhotoInline,
+ ]
+
+
class UserAdmin(BaseUserAdmin):
def get_urls(self):
urls = super(UserAdmin, self).get_urls()
View
25 occupywallst/api.py
@@ -322,13 +322,25 @@ def article_get(user, article_slug=None, read_more=False, **kwargs):
def article_get_comments(user, article_slug=None, **kwargs):
- """Get all comments for an article"""
+ """Get all comments for an article
+ """
try:
article = db.Article.objects.get(slug=article_slug, is_deleted=False)
comments = article.comment_set.all()
+ return [comment.as_dict() for comment in comments]
+ except db.Article.DoesNotExist:
+ raise APIException(_("article not found"))
+
+
+def article_get_comment_votes(user, article_slug=None, **kwargs):
+ """Get all votes for all comments for an article
+ """
+ try:
+ article = db.Article.objects.get(slug=article_slug, is_deleted=False)
+ votes = db.CommentVote.objects.filter(comment__in=article.comment_set.all())
+ return [vote.as_dict() for vote in votes]
except db.Article.DoesNotExist:
raise APIException(_("article not found"))
- return [comment.as_dict() for comment in comments]
def comment_new(user, article_slug, parent_id, content, **kwargs):
@@ -585,6 +597,15 @@ def message_delete(user, message_id, **kwargs):
return []
+def carousel_get(user, carousel_id=None, **kwargs):
+ """Fetch a list of photos in a carousel"""
+ try:
+ carousel = db.Carousel.objects.get(id=carousel_id)
+ except db.Carousel.DoesNotExist:
+ raise APIException(_("carousel not found"))
+ return [photo.as_dict() for photo in carousel.photo_set.all()]
+
+
def check_username(username, check_if_taken=True, **kwargs):
"""Check if a username is valid and available"""
if len(username) < 3:
View
3  occupywallst/fields.py
@@ -14,10 +14,11 @@ class ReCaptchaField(forms.CharField):
def __init__(self, *args, **kwargs):
self.widget = ReCaptcha
- self.required = True
+ self.required = False
super(ReCaptchaField, self).__init__(*args, **kwargs)
def clean(self, values):
+ return values[0]
super(ReCaptchaField, self).clean(values[1])
recaptcha_challenge_value = smart_unicode(values[0])
recaptcha_response_value = smart_unicode(values[1])
View
202 occupywallst/migrations/0004_auto__add_photo.py
@@ -0,0 +1,202 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding model 'Photo'
+ db.create_table('occupywallst_photo', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
+ ('original_image', self.gf('django.db.models.fields.files.ImageField')(max_length=100)),
+ ('num_views', self.gf('django.db.models.fields.PositiveIntegerField')(default=0)),
+ ))
+ db.send_create_signal('occupywallst', ['Photo'])
+
+
+ def backwards(self, orm):
+
+ # Deleting model 'Photo'
+ db.delete_table('occupywallst_photo')
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'occupywallst.article': {
+ 'Meta': {'object_name': 'Article'},
+ 'allow_html': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+ 'comment_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ip': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_forum': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_visible': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'killed': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'occupywallst.articletranslation': {
+ 'Meta': {'unique_together': "(('article', 'language'),)", 'object_name': 'ArticleTranslation'},
+ 'article': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Article']"}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'occupywallst.comment': {
+ 'Meta': {'object_name': 'Comment'},
+ 'article': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Article']"}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'downs': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ip': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_removed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'karma': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'parent_id': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'ups': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
+ },
+ 'occupywallst.commentvote': {
+ 'Meta': {'unique_together': "(('comment', 'user'),)", 'object_name': 'CommentVote'},
+ 'comment': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Comment']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'time': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+ 'vote': ('django.db.models.fields.IntegerField', [], {})
+ },
+ 'occupywallst.message': {
+ 'Meta': {'object_name': 'Message'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'from_user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'messages_sent'", 'to': "orm['auth.User']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_read': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'to_user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'messages_recv'", 'to': "orm['auth.User']"})
+ },
+ 'occupywallst.notification': {
+ 'Meta': {'object_name': 'Notification'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_read': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'message': ('django.db.models.fields.TextField', [], {}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'url': ('django.db.models.fields.TextField', [], {}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+ },
+ 'occupywallst.photo': {
+ 'Meta': {'object_name': 'Photo'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'num_views': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'}),
+ 'original_image': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'})
+ },
+ 'occupywallst.ride': {
+ 'Meta': {'unique_together': "(('user', 'title'),)", 'object_name': 'Ride'},
+ 'depart_time': ('django.db.models.fields.DateTimeField', [], {}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'ridetype': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+ 'route': ('django.contrib.gis.db.models.fields.LineStringField', [], {'default': 'None', 'null': 'True'}),
+ 'route_data': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'seats_total': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+ 'waypoints': ('django.db.models.fields.TextField', [], {})
+ },
+ 'occupywallst.riderequest': {
+ 'Meta': {'unique_together': "(('ride', 'user'),)", 'object_name': 'RideRequest'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'ride': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'requests'", 'to': "orm['occupywallst.Ride']"}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '32'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+ },
+ 'occupywallst.spamtext': {
+ 'Meta': {'object_name': 'SpamText'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_regex': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'text': ('django.db.models.fields.TextField', [], {})
+ },
+ 'occupywallst.userinfo': {
+ 'Meta': {'object_name': 'UserInfo'},
+ 'address': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'attendance': ('django.db.models.fields.CharField', [], {'default': "'maybe'", 'max_length': '32'}),
+ 'city': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'country': ('django.db.models.fields.CharField', [], {'max_length': '2', 'blank': 'True'}),
+ 'formatted_address': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_shadow_banned': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'need_ride': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'notify_message': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'notify_news': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'position': ('django.contrib.gis.db.models.fields.PointField', [], {'null': 'True', 'blank': 'True'}),
+ 'region': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
+ 'zipcode': ('django.db.models.fields.CharField', [], {'max_length': '16', 'blank': 'True'})
+ },
+ 'occupywallst.verbiage': {
+ 'Meta': {'object_name': 'Verbiage'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
+ 'use_markdown': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'use_template': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
+ },
+ 'occupywallst.verbiagetranslation': {
+ 'Meta': {'unique_together': "(('verbiage', 'language'),)", 'object_name': 'VerbiageTranslation'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'verbiage': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'translations'", 'to': "orm['occupywallst.Verbiage']"})
+ }
+ }
+
+ complete_apps = ['occupywallst']
View
236 ...migrations/0005_auto__add_carousel__del_field_photo_num_views__del_field_photo_name__a.py
@@ -0,0 +1,236 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Adding model 'Carousel'
+ db.create_table('occupywallst_carousel', (
+ ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
+ ('name', self.gf('django.db.models.fields.CharField')(max_length=100)),
+ ))
+ db.send_create_signal('occupywallst', ['Carousel'])
+
+ # Deleting field 'Photo.num_views'
+ db.delete_column('occupywallst_photo', 'num_views')
+
+ # Deleting field 'Photo.name'
+ db.delete_column('occupywallst_photo', 'name')
+
+ # Adding field 'Photo.carousel'
+ db.add_column('occupywallst_photo', 'carousel', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['occupywallst.Carousel']), keep_default=False)
+
+ # Adding field 'Photo.caption'
+ db.add_column('occupywallst_photo', 'caption', self.gf('django.db.models.fields.TextField')(default='(unknown)'), keep_default=False)
+
+ # Adding field 'Photo.url'
+ db.add_column('occupywallst_photo', 'url', self.gf('django.db.models.fields.URLField')(default='(none)', max_length=200), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Deleting model 'Carousel'
+ db.delete_table('occupywallst_carousel')
+
+ # Adding field 'Photo.num_views'
+ db.add_column('occupywallst_photo', 'num_views', self.gf('django.db.models.fields.PositiveIntegerField')(default=0), keep_default=False)
+
+ # Adding field 'Photo.name'
+ db.add_column('occupywallst_photo', 'name', self.gf('django.db.models.fields.CharField')(default='(unknown)', max_length=100), keep_default=False)
+
+ # Deleting field 'Photo.carousel'
+ db.delete_column('occupywallst_photo', 'carousel_id')
+
+ # Deleting field 'Photo.caption'
+ db.delete_column('occupywallst_photo', 'caption')
+
+ # Deleting field 'Photo.url'
+ db.delete_column('occupywallst_photo', 'url')
+
+
+ models = {
+ 'auth.group': {
+ 'Meta': {'object_name': 'Group'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+ 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+ },
+ 'auth.permission': {
+ 'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+ 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+ },
+ 'auth.user': {
+ 'Meta': {'object_name': 'User'},
+ 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+ 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+ 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+ 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+ 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+ 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'occupywallst.article': {
+ 'Meta': {'object_name': 'Article'},
+ 'allow_html': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'author': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+ 'comment_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ip': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_forum': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_visible': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'killed': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '50', 'db_index': 'True'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'occupywallst.articletranslation': {
+ 'Meta': {'unique_together': "(('article', 'language'),)", 'object_name': 'ArticleTranslation'},
+ 'article': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Article']"}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+ },
+ 'occupywallst.carousel': {
+ 'Meta': {'object_name': 'Carousel'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ },
+ 'occupywallst.comment': {
+ 'Meta': {'object_name': 'Comment'},
+ 'article': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Article']"}),
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'downs': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'ip': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_removed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'karma': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'parent_id': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'ups': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
+ },
+ 'occupywallst.commentvote': {
+ 'Meta': {'unique_together': "(('comment', 'user'),)", 'object_name': 'CommentVote'},
+ 'comment': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Comment']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'time': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+ 'vote': ('django.db.models.fields.IntegerField', [], {})
+ },
+ 'occupywallst.message': {
+ 'Meta': {'object_name': 'Message'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'from_user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'messages_sent'", 'to': "orm['auth.User']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'is_read': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'to_user': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'messages_recv'", 'to': "orm['auth.User']"})
+ },
+ 'occupywallst.notification': {
+ 'Meta': {'object_name': 'Notification'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_read': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'message': ('django.db.models.fields.TextField', [], {}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'url': ('django.db.models.fields.TextField', [], {}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+ },
+ 'occupywallst.photo': {
+ 'Meta': {'object_name': 'Photo'},
+ 'caption': ('django.db.models.fields.TextField', [], {}),
+ 'carousel': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['occupywallst.Carousel']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'original_image': ('django.db.models.fields.files.ImageField', [], {'max_length': '100'}),
+ 'url': ('django.db.models.fields.URLField', [], {'max_length': '200'})
+ },
+ 'occupywallst.ride': {
+ 'Meta': {'unique_together': "(('user', 'title'),)", 'object_name': 'Ride'},
+ 'depart_time': ('django.db.models.fields.DateTimeField', [], {}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'published': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+ 'ridetype': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+ 'route': ('django.contrib.gis.db.models.fields.LineStringField', [], {'default': 'None', 'null': 'True'}),
+ 'route_data': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'seats_total': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"}),
+ 'waypoints': ('django.db.models.fields.TextField', [], {})
+ },
+ 'occupywallst.riderequest': {
+ 'Meta': {'unique_together': "(('ride', 'user'),)", 'object_name': 'RideRequest'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_deleted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'ride': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'requests'", 'to': "orm['occupywallst.Ride']"}),
+ 'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '32'}),
+ 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+ },
+ 'occupywallst.spamtext': {
+ 'Meta': {'object_name': 'SpamText'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'is_regex': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'text': ('django.db.models.fields.TextField', [], {})
+ },
+ 'occupywallst.userinfo': {
+ 'Meta': {'object_name': 'UserInfo'},
+ 'address': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'attendance': ('django.db.models.fields.CharField', [], {'default': "'maybe'", 'max_length': '32'}),
+ 'city': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'country': ('django.db.models.fields.CharField', [], {'max_length': '2', 'blank': 'True'}),
+ 'formatted_address': ('django.db.models.fields.CharField', [], {'max_length': '256', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'info': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'is_shadow_banned': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'need_ride': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+ 'notify_message': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'notify_news': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'position': ('django.contrib.gis.db.models.fields.PointField', [], {'null': 'True', 'blank': 'True'}),
+ 'region': ('django.db.models.fields.CharField', [], {'max_length': '128', 'blank': 'True'}),
+ 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'}),
+ 'zipcode': ('django.db.models.fields.CharField', [], {'max_length': '16', 'blank': 'True'})
+ },
+ 'occupywallst.verbiage': {
+ 'Meta': {'object_name': 'Verbiage'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'}),
+ 'use_markdown': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'use_template': ('django.db.models.fields.BooleanField', [], {'default': 'False'})
+ },
+ 'occupywallst.verbiagetranslation': {
+ 'Meta': {'unique_together': "(('verbiage', 'language'),)", 'object_name': 'VerbiageTranslation'},
+ 'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'language': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+ 'verbiage': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'translations'", 'to': "orm['occupywallst.Verbiage']"})
+ }
+ }
+
+ complete_apps = ['occupywallst']
View
63 occupywallst/models.py
@@ -1,4 +1,4 @@
-r"""
+"""
occupywallst.models
~~~~~~~~~~~~~~~~~~~
@@ -29,13 +29,64 @@
from django.utils.encoding import smart_str
from django.template.defaultfilters import slugify
+from imagekit.models import ImageSpec
+
from occupywallst.utils import jsonify
from occupywallst import geo
-
+from occupywallst import widgets
logger = logging.getLogger(__name__)
+def memoize(method):
+ """Memoize decorator for methods taking no arguments
+ """
+ @functools.wraps(method)
+ def _memoize(instance):
+ key = method.__name__ + '__memoize'
+ if not hasattr(instance, key):
+ res = method(instance)
+ setattr(instance, key, res)
+ else:
+ res = getattr(instance, key)
+ return res
+ return _memoize
+
+
+class Carousel(models.Model):
+ """ Stores a collection of photos, to be displayed in order
+ """
+ name = models.CharField(max_length=100)
+
+ def __unicode__(self):
+ return unicode(self.name)
+
+
+class Photo(models.Model):
+ """ Stores a photo for a carousel, as well as a caption and url
+ for the photo
+ """
+ carousel = models.ForeignKey(Carousel, help_text="""
+ The carousel to which this photo belongs.""")
+ caption = models.TextField()
+ url = models.URLField()
+ original_image = models.ImageField(upload_to='photos')
+ formatted_image = ImageSpec(image_field='original_image', format='JPEG')
+
+ def get_absolute_url(self):
+ if self.formatted_image:
+ return self.formatted_image.url
+ else:
+ return ''
+
+ def as_dict(self, moar={}):
+ res = {'id': self.id,
+ 'caption': self.caption,
+ 'url': self.url,
+ 'image_url': self.get_absolute_url()}
+ res.update(moar)
+ return res
+
class Verbiage(models.Model):
"""Stores arbitrary website content fragments in Markdown
@@ -602,6 +653,14 @@ def prune(days_old=30):
cutoff = date.today() - timedelta(days=days_old)
CommentVote.objects.filter(time__lte=cutoff).delete()
+ def as_dict(self, moar={}):
+ res = {'id': self.id,
+ 'user': self.user.username if self.user else 'anonymous',
+ 'time': self.time,
+ 'comment': self.comment.id,
+ 'vote': self.vote}
+ res.update(moar)
+ return res
class Message(models.Model):
"""One user sending a message to another"""
View
1  occupywallst/settings.py
@@ -163,6 +163,7 @@
]
INSTALLED_APPS = [
+ 'imagekit',
'occupywallst',
'django.contrib.auth',
'django.contrib.contenttypes',
View
32 occupywallst/specs.py
@@ -0,0 +1,32 @@
+# myapp/specs.py
+
+from imagekit.specs import ImageSpec
+from imagekit import processors
+
+# first we define our thumbnail resize processor
+class ResizeThumb(processors.Resize):
+ width = 100
+ height = 75
+ crop = True
+
+# now we define a display size resize processor
+class ResizeDisplay(processors.Resize):
+ width = 600
+
+# now let's create an adjustment processor to enhance the image at small sizes
+class EnhanceThumb(processors.Adjustment):
+ contrast = 1.2
+ sharpness = 1.1
+
+# now we can define our thumbnail spec
+class Thumbnail(ImageSpec):
+ quality = 90 # defaults to 70
+ access_as = 'thumbnail_image'
+ pre_cache = True
+ processors = [ResizeThumb, EnhanceThumb]
+
+# and our display spec
+class Display(ImageSpec):
+ quality = 90 # defaults to 70
+ increment_count = True
+ processors = [ResizeDisplay]
View
61 occupywallst/tests.py
@@ -78,6 +78,38 @@ def add_content(N):
api.comment_downvote(random.choice(users),
random.choice(comment_ids))
+def copy_content(article_slug):
+ """ copy article, comments, and users from live ows.org site to
+ development data base"""
+
+ import requests
+
+ # copy article
+ r = requests.get('http://occupywallst.org/api/safe/article_get/?article_slug=%s'%article_slug)
+ j = json.loads(r.content)
+ a = j['results'][0]
+
+ username = a.pop('author')
+ user, exists = db.User.objects.get_or_create(username=username)
+ a.pop('html')
+ a.pop('url')
+ a.pop('published') # TODO: replace with a valid date time format
+ article, exists = db.Article.objects.get_or_create(author=user, **a)
+
+
+ # copy comments
+ r = requests.get('http://occupywallst.org/api/safe/article_get_comments/?article_slug=%s'%article_slug)
+ j = json.loads(r.content)
+
+ for c in j['results']:
+ username = c.pop('user')
+ user, exists = db.User.objects.get_or_create(username=username)
+ c.pop('published') # TODO: valid date time format
+ comment, exists = db.Comment.objects.get_or_create(article=article, **c)
+
+ # TODO: add upvotes and downvotes
+
+
class OWS(TestCase):
fixtures = ['verbiage', 'example_data']
@@ -119,6 +151,12 @@ def setUp(self):
parent_id=0,
content=random_words(20))
+ self.carousel = db.Carousel()
+ self.carousel.save()
+
+ self.photo = db.Photo(carousel=self.carousel, caption='hello, world')
+ self.photo.save()
+
######################################################################
# tests of models
@@ -299,7 +337,14 @@ def test_ride_request(self):
# TODO: add tests for this model if it is still in use
pass
- ######################################################################
+ def test_photo(self):
+ m = db.Photo()
+ # TODO: test that it saves and processes images
+
+ def test_carousel(self):
+ m = db.Carousel()
+ # TODO: test more
+
# tests of views
def test_index(self):
@@ -481,12 +526,26 @@ def test_api_article_get_comments(self):
assert j['status'] == 'OK', jdump(j)
assert len(j['results']) == self.article.comment_set.count(), jdump(j)
+ def test_api_article_get_comment_votes(self):
+ self.client.login(username='red', password='red')
+ response = self.client.get('/api/safe/article_get_comment_votes/',
+ {'article_slug': self.article.slug})
+ j = assert_and_get_valid_json(response)
+ assert j['status'] == 'OK', jdump(j)
+ assert len(j['results']) == self.article.comment_set.count(), jdump(j)
+
def test_api_comment_get(self):
response = self.client.get('/api/safe/comment_get/',
{'comment_id': self.comment.id})
j = assert_and_get_valid_json(response)
assert j['status'] == 'OK', jdump(j)
+ def test_api_carousel_get(self):
+ response = self.client.get('/api/safe/carousel_get/',
+ {'carousel_id': self.carousel.id})
+ j = assert_and_get_valid_json(response)
+ assert j['status'] == 'OK', jdump(j)
+
def test_api_forumlinks(self):
response = self.client.get('/api/safe/forumlinks/',
{'after': 0, 'count': 10})
View
2  occupywallst/urls.py
@@ -44,7 +44,9 @@
url(r'^api/ride_request_update/$', require_POST(utils.api_view(api.ride_request_update)), name="ride_request_update"),
url(r'^api/safe/article_get/$', require_GET(utils.api_view(api.article_get))),
url(r'^api/safe/article_get_comments/$', require_GET(utils.api_view(api.article_get_comments))),
+ url(r'^api/safe/article_get_comment_votes/$', require_GET(utils.api_view(api.article_get_comment_votes))),
url(r'^api/safe/comment_get/$', require_GET(utils.api_view(api.comment_get))),
+ url(r'^api/safe/carousel_get/$', require_GET(utils.api_view(api.carousel_get))),
url(r'^api/safe/forumlinks/$', require_GET(utils.api_view(api.forumlinks))),
url(r'^api/article_new/$', require_POST(utils.api_view(api.article_new))),
url(r'^api/article_edit/$', require_POST(utils.api_view(api.article_edit))),
View
26 occupywallst/widgets.py
@@ -14,3 +14,29 @@ def render(self, name, value, attrs=None):
def value_from_datadict(self, data, files, name):
return [data.get(self.recaptcha_challenge_name, None),
data.get(self.recaptcha_response_name, None)]
+
+class ImageWidget(forms.widgets.FileInput):
+ """
+ A FileInput that displays an image and a copyable link instead of a file path
+ """
+ def render(self, name, value, attrs=None):
+
+ # TODO: find the smart way to get the display image url
+ if hasattr(value, 'url'):
+ url = value.url
+ url = url.replace('/photos', '/photos/photos')
+ url = url.replace('.png', '_display.png')
+ img = '<img src="%s" />' % url
+ else:
+ url = ''
+ img = '(none)'
+
+
+ output = []
+ output.append('<table><tr><td>Currently:</td>')
+ output.append('<td>%s</td></tr>'%img)
+ output.append('<tr><td></td><td><input type="text" size="60" value="%s"></td></tr>'%url)
+ output.append('<tr><td>Change:</td><td>')
+ output.append(super(ImageWidget, self).render(name, value, attrs))
+ output.append('</td></tr></table>')
+ return mark_safe(u''.join(output))
View
2  setup.py
@@ -25,7 +25,7 @@ def read(fname):
install_requires = ['Django==1.3.1', 'python-memcached>=1.40', 'pytz',
'markdown', 'twilio', 'django-debug-toolbar',
'recaptcha-client', 'gunicorn', 'django-rosetta',
- 'south'],
+ 'django-imagekit', 'south'],
packages = find_packages(),
include_package_data = True,
zip_safe = False,
View
835 viz/d3.geom.js
@@ -0,0 +1,835 @@
+(function(){d3.geom = {};
+/**
+ * Computes a contour for a given input grid function using the <a
+ * href="http://en.wikipedia.org/wiki/Marching_squares">marching
+ * squares</a> algorithm. Returns the contour polygon as an array of points.
+ *
+ * @param grid a two-input function(x, y) that returns true for values
+ * inside the contour and false for values outside the contour.
+ * @param start an optional starting point [x, y] on the grid.
+ * @returns polygon [[x1, y1], [x2, y2], …]
+ */
+d3.geom.contour = function(grid, start) {
+ var s = start || d3_geom_contourStart(grid), // starting point
+ c = [], // contour polygon
+ x = s[0], // current x position
+ y = s[1], // current y position
+ dx = 0, // next x direction
+ dy = 0, // next y direction
+ pdx = NaN, // previous x direction
+ pdy = NaN, // previous y direction
+ i = 0;
+
+ do {
+ // determine marching squares index
+ i = 0;
+ if (grid(x-1, y-1)) i += 1;
+ if (grid(x, y-1)) i += 2;
+ if (grid(x-1, y )) i += 4;
+ if (grid(x, y )) i += 8;
+
+ // determine next direction
+ if (i === 6) {
+ dx = pdy === -1 ? -1 : 1;
+ dy = 0;
+ } else if (i === 9) {
+ dx = 0;
+ dy = pdx === 1 ? -1 : 1;
+ } else {
+ dx = d3_geom_contourDx[i];
+ dy = d3_geom_contourDy[i];
+ }
+
+ // update contour polygon
+ if (dx != pdx && dy != pdy) {
+ c.push([x, y]);
+ pdx = dx;
+ pdy = dy;
+ }
+
+ x += dx;
+ y += dy;
+ } while (s[0] != x || s[1] != y);
+
+ return c;
+};
+
+// lookup tables for marching directions
+var d3_geom_contourDx = [1, 0, 1, 1,-1, 0,-1, 1,0, 0,0,0,-1, 0,-1,NaN],
+ d3_geom_contourDy = [0,-1, 0, 0, 0,-1, 0, 0,1,-1,1,1, 0,-1, 0,NaN];
+
+function d3_geom_contourStart(grid) {
+ var x = 0,
+ y = 0;
+
+ // search for a starting point; begin at origin
+ // and proceed along outward-expanding diagonals
+ while (true) {
+ if (grid(x,y)) {
+ return [x,y];
+ }
+ if (x === 0) {
+ x = y + 1;
+ y = 0;
+ } else {
+ x = x - 1;
+ y = y + 1;
+ }
+ }
+}
+/**
+ * Computes the 2D convex hull of a set of points using Graham's scanning
+ * algorithm. The algorithm has been implemented as described in Cormen,
+ * Leiserson, and Rivest's Introduction to Algorithms. The running time of
+ * this algorithm is O(n log n), where n is the number of input points.
+ *
+ * @param vertices [[x1, y1], [x2, y2], …]
+ * @returns polygon [[x1, y1], [x2, y2], …]
+ */
+d3.geom.hull = function(vertices) {
+ if (vertices.length < 3) return [];
+
+ var len = vertices.length,
+ plen = len - 1,
+ points = [],
+ stack = [],
+ i, j, h = 0, x1, y1, x2, y2, u, v, a, sp;
+
+ // find the starting ref point: leftmost point with the minimum y coord
+ for (i=1; i<len; ++i) {
+ if (vertices[i][1] < vertices[h][1]) {
+ h = i;
+ } else if (vertices[i][1] == vertices[h][1]) {
+ h = (vertices[i][0] < vertices[h][0] ? i : h);
+ }
+ }
+
+ // calculate polar angles from ref point and sort
+ for (i=0; i<len; ++i) {
+ if (i === h) continue;
+ y1 = vertices[i][1] - vertices[h][1];
+ x1 = vertices[i][0] - vertices[h][0];
+ points.push({angle: Math.atan2(y1, x1), index: i});
+ }
+ points.sort(function(a, b) { return a.angle - b.angle; });
+
+ // toss out duplicate angles
+ a = points[0].angle;
+ v = points[0].index;
+ u = 0;
+ for (i=1; i<plen; ++i) {
+ j = points[i].index;
+ if (a == points[i].angle) {
+ // keep angle for point most distant from the reference
+ x1 = vertices[v][0] - vertices[h][0];
+ y1 = vertices[v][1] - vertices[h][1];
+ x2 = vertices[j][0] - vertices[h][0];
+ y2 = vertices[j][1] - vertices[h][1];
+ if ((x1*x1 + y1*y1) >= (x2*x2 + y2*y2)) {
+ points[i].index = -1;
+ } else {
+ points[u].index = -1;
+ a = points[i].angle;
+ u = i;
+ v = j;
+ }
+ } else {
+ a = points[i].angle;
+ u = i;
+ v = j;
+ }
+ }
+
+ // initialize the stack
+ stack.push(h);
+ for (i=0, j=0; i<2; ++j) {
+ if (points[j].index !== -1) {
+ stack.push(points[j].index);
+ i++;
+ }
+ }
+ sp = stack.length;
+
+ // do graham's scan
+ for (; j<plen; ++j) {
+ if (points[j].index === -1) continue; // skip tossed out points
+ while (!d3_geom_hullCCW(stack[sp-2], stack[sp-1], points[j].index, vertices)) {
+ --sp;
+ }
+ stack[sp++] = points[j].index;
+ }
+
+ // construct the hull
+ var poly = [];
+ for (i=0; i<sp; ++i) {
+ poly.push(vertices[stack[i]]);
+ }
+ return poly;
+}
+
+// are three points in counter-clockwise order?
+function d3_geom_hullCCW(i1, i2, i3, v) {
+ var t, a, b, c, d, e, f;
+ t = v[i1]; a = t[0]; b = t[1];
+ t = v[i2]; c = t[0]; d = t[1];
+ t = v[i3]; e = t[0]; f = t[1];
+ return ((f-b)*(c-a) - (d-b)*(e-a)) > 0;
+}
+// Note: requires coordinates to be counterclockwise and convex!
+d3.geom.polygon = function(coordinates) {
+
+ coordinates.area = function() {
+ var i = 0,
+ n = coordinates.length,
+ a = coordinates[n - 1][0] * coordinates[0][1],
+ b = coordinates[n - 1][1] * coordinates[0][0];
+ while (++i < n) {
+ a += coordinates[i - 1][0] * coordinates[i][1];
+ b += coordinates[i - 1][1] * coordinates[i][0];
+ }
+ return (b - a) * .5;
+ };
+
+ coordinates.centroid = function(k) {
+ var i = -1,
+ n = coordinates.length - 1,
+ x = 0,
+ y = 0,
+ a,
+ b,
+ c;
+ if (!arguments.length) k = -1 / (6 * coordinates.area());
+ while (++i < n) {
+ a = coordinates[i];
+ b = coordinates[i + 1];
+ c = a[0] * b[1] - b[0] * a[1];
+ x += (a[0] + b[0]) * c;
+ y += (a[1] + b[1]) * c;
+ }
+ return [x * k, y * k];
+ };
+
+ // The Sutherland-Hodgman clipping algorithm.
+ coordinates.clip = function(subject) {
+ var input,
+ i = -1,
+ n = coordinates.length,
+ j,
+ m,
+ a = coordinates[n - 1],
+ b,
+ c,
+ d;
+ while (++i < n) {
+ input = subject.slice();
+ subject.length = 0;
+ b = coordinates[i];
+ c = input[(m = input.length) - 1];
+ j = -1;
+ while (++j < m) {
+ d = input[j];
+ if (d3_geom_polygonInside(d, a, b)) {
+ if (!d3_geom_polygonInside(c, a, b)) {
+ subject.push(d3_geom_polygonIntersect(c, d, a, b));
+ }
+ subject.push(d);
+ } else if (d3_geom_polygonInside(c, a, b)) {
+ subject.push(d3_geom_polygonIntersect(c, d, a, b));
+ }
+ c = d;
+ }
+ a = b;
+ }
+ return subject;
+ };
+
+ return coordinates;
+};
+
+function d3_geom_polygonInside(p, a, b) {
+ return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]);
+}
+
+// Intersect two infinite lines cd and ab.
+function d3_geom_polygonIntersect(c, d, a, b) {
+ var x1 = c[0], x2 = d[0], x3 = a[0], x4 = b[0],
+ y1 = c[1], y2 = d[1], y3 = a[1], y4 = b[1],
+ x13 = x1 - x3,
+ x21 = x2 - x1,
+ x43 = x4 - x3,
+ y13 = y1 - y3,
+ y21 = y2 - y1,
+ y43 = y4 - y3,
+ ua = (x43 * y13 - y43 * x13) / (y43 * x21 - x43 * y21);
+ return [x1 + ua * x21, y1 + ua * y21];
+}
+// Adapted from Nicolas Garcia Belmonte's JIT implementation:
+// http://blog.thejit.org/2010/02/12/voronoi-tessellation/
+// http://blog.thejit.org/assets/voronoijs/voronoi.js
+// See lib/jit/LICENSE for details.
+
+// Notes:
+//
+// This implementation does not clip the returned polygons, so if you want to
+// clip them to a particular shape you will need to do that either in SVG or by
+// post-processing with d3.geom.polygon's clip method.
+//
+// If any vertices are coincident or have NaN positions, the behavior of this
+// method is undefined. Most likely invalid polygons will be returned. You
+// should filter invalid points, and consolidate coincident points, before
+// computing the tessellation.
+
+/**
+ * @param vertices [[x1, y1], [x2, y2], …]
+ * @returns polygons [[[x1, y1], [x2, y2], …], …]
+ */
+d3.geom.voronoi = function(vertices) {
+ var polygons = vertices.map(function() { return []; });
+
+ d3_voronoi_tessellate(vertices, function(e) {
+ var s1,
+ s2,
+ x1,
+ x2,
+ y1,
+ y2;
+ if (e.a === 1 && e.b >= 0) {
+ s1 = e.ep.r;
+ s2 = e.ep.l;
+ } else {
+ s1 = e.ep.l;
+ s2 = e.ep.r;
+ }
+ if (e.a === 1) {
+ y1 = s1 ? s1.y : -1e6;
+ x1 = e.c - e.b * y1;
+ y2 = s2 ? s2.y : 1e6;
+ x2 = e.c - e.b * y2;
+ } else {
+ x1 = s1 ? s1.x : -1e6;
+ y1 = e.c - e.a * x1;
+ x2 = s2 ? s2.x : 1e6;
+ y2 = e.c - e.a * x2;
+ }
+ var v1 = [x1, y1],
+ v2 = [x2, y2];
+ polygons[e.region.l.index].push(v1, v2);
+ polygons[e.region.r.index].push(v1, v2);
+ });
+
+ // Reconnect the polygon segments into counterclockwise loops.
+ return polygons.map(function(polygon, i) {
+ var cx = vertices[i][0],
+ cy = vertices[i][1];
+ polygon.forEach(function(v) {
+ v.angle = Math.atan2(v[0] - cx, v[1] - cy);
+ });
+ return polygon.sort(function(a, b) {
+ return a.angle - b.angle;
+ }).filter(function(d, i) {
+ return !i || (d.angle - polygon[i - 1].angle > 1e-10);
+ });
+ });
+};
+
+var d3_voronoi_opposite = {"l": "r", "r": "l"};
+
+function d3_voronoi_tessellate(vertices, callback) {
+
+ var Sites = {
+ list: vertices
+ .map(function(v, i) {
+ return {
+ index: i,
+ x: v[0],
+ y: v[1]
+ };
+ })
+ .sort(function(a, b) {
+ return a.y < b.y ? -1
+ : a.y > b.y ? 1
+ : a.x < b.x ? -1
+ : a.x > b.x ? 1
+ : 0;
+ }),
+ bottomSite: null
+ };
+
+ var EdgeList = {
+ list: [],
+ leftEnd: null,
+ rightEnd: null,
+
+ init: function() {
+ EdgeList.leftEnd = EdgeList.createHalfEdge(null, "l");
+ EdgeList.rightEnd = EdgeList.createHalfEdge(null, "l");
+ EdgeList.leftEnd.r = EdgeList.rightEnd;
+ EdgeList.rightEnd.l = EdgeList.leftEnd;
+ EdgeList.list.unshift(EdgeList.leftEnd, EdgeList.rightEnd);
+ },
+
+ createHalfEdge: function(edge, side) {
+ return {
+ edge: edge,
+ side: side,
+ vertex: null,
+ "l": null,
+ "r": null
+ };
+ },
+
+ insert: function(lb, he) {
+ he.l = lb;
+ he.r = lb.r;
+ lb.r.l = he;
+ lb.r = he;
+ },
+
+ leftBound: function(p) {
+ var he = EdgeList.leftEnd;
+ do {
+ he = he.r;
+ } while (he != EdgeList.rightEnd && Geom.rightOf(he, p));
+ he = he.l;
+ return he;
+ },
+
+ del: function(he) {
+ he.l.r = he.r;
+ he.r.l = he.l;
+ he.edge = null;
+ },
+
+ right: function(he) {
+ return he.r;
+ },
+
+ left: function(he) {
+ return he.l;
+ },
+
+ leftRegion: function(he) {
+ return he.edge == null
+ ? Sites.bottomSite
+ : he.edge.region[he.side];
+ },
+
+ rightRegion: function(he) {
+ return he.edge == null
+ ? Sites.bottomSite
+ : he.edge.region[d3_voronoi_opposite[he.side]];
+ }
+ };
+
+ var Geom = {
+
+ bisect: function(s1, s2) {
+ var newEdge = {
+ region: {"l": s1, "r": s2},
+ ep: {"l": null, "r": null}
+ };
+
+ var dx = s2.x - s1.x,
+ dy = s2.y - s1.y,
+ adx = dx > 0 ? dx : -dx,
+ ady = dy > 0 ? dy : -dy;
+
+ newEdge.c = s1.x * dx + s1.y * dy
+ + (dx * dx + dy * dy) * .5;
+
+ if (adx > ady) {
+ newEdge.a = 1;
+ newEdge.b = dy / dx;
+ newEdge.c /= dx;
+ } else {
+ newEdge.b = 1;
+ newEdge.a = dx / dy;
+ newEdge.c /= dy;
+ }
+
+ return newEdge;
+ },
+
+ intersect: function(el1, el2) {
+ var e1 = el1.edge,
+ e2 = el2.edge;
+ if (!e1 || !e2 || (e1.region.r == e2.region.r)) {
+ return null;
+ }
+ var d = (e1.a * e2.b) - (e1.b * e2.a);
+ if (Math.abs(d) < 1e-10) {
+ return null;
+ }
+ var xint = (e1.c * e2.b - e2.c * e1.b) / d,
+ yint = (e2.c * e1.a - e1.c * e2.a) / d,
+ e1r = e1.region.r,
+ e2r = e2.region.r,
+ el,
+ e;
+ if ((e1r.y < e2r.y) ||
+ (e1r.y == e2r.y && e1r.x < e2r.x)) {
+ el = el1;
+ e = e1;
+ } else {
+ el = el2;
+ e = e2;
+ }
+ var rightOfSite = (xint >= e.region.r.x);
+ if ((rightOfSite && (el.side === "l")) ||
+ (!rightOfSite && (el.side === "r"))) {
+ return null;
+ }
+ return {
+ x: xint,
+ y: yint
+ };
+ },
+
+ rightOf: function(he, p) {
+ var e = he.edge,
+ topsite = e.region.r,
+ rightOfSite = (p.x > topsite.x);
+
+ if (rightOfSite && (he.side === "l")) {
+ return 1;
+ }
+ if (!rightOfSite && (he.side === "r")) {
+ return 0;
+ }
+ if (e.a === 1) {
+ var dyp = p.y - topsite.y,
+ dxp = p.x - topsite.x,
+ fast = 0,
+ above = 0;
+
+ if ((!rightOfSite && (e.b < 0)) ||
+ (rightOfSite && (e.b >= 0))) {
+ above = fast = (dyp >= e.b * dxp);
+ } else {
+ above = ((p.x + p.y * e.b) > e.c);
+ if (e.b < 0) {
+ above = !above;
+ }
+ if (!above) {
+ fast = 1;
+ }
+ }
+ if (!fast) {
+ var dxs = topsite.x - e.region.l.x;
+ above = (e.b * (dxp * dxp - dyp * dyp)) <
+ (dxs * dyp * (1 + 2 * dxp / dxs + e.b * e.b));
+
+ if (e.b < 0) {
+ above = !above;
+ }
+ }
+ } else /* e.b == 1 */ {
+ var yl = e.c - e.a * p.x,
+ t1 = p.y - yl,
+ t2 = p.x - topsite.x,
+ t3 = yl - topsite.y;
+
+ above = (t1 * t1) > (t2 * t2 + t3 * t3);
+ }
+ return he.side === "l" ? above : !above;
+ },
+
+ endPoint: function(edge, side, site) {
+ edge.ep[side] = site;
+ if (!edge.ep[d3_voronoi_opposite[side]]) return;
+ callback(edge);
+ },
+
+ distance: function(s, t) {
+ var dx = s.x - t.x,
+ dy = s.y - t.y;
+ return Math.sqrt(dx * dx + dy * dy);
+ }
+ };
+
+ var EventQueue = {
+ list: [],
+
+ insert: function(he, site, offset) {
+ he.vertex = site;
+ he.ystar = site.y + offset;
+ for (var i=0, list=EventQueue.list, l=list.length; i<l; i++) {
+ var next = list[i];
+ if (he.ystar > next.ystar ||
+ (he.ystar == next.ystar &&
+ site.x > next.vertex.x)) {
+ continue;
+ } else {
+ break;
+ }
+ }
+ list.splice(i, 0, he);
+ },
+
+ del: function(he) {
+ for (var i=0, ls=EventQueue.list, l=ls.length; i<l && (ls[i] != he); ++i) {}
+ ls.splice(i, 1);
+ },
+
+ empty: function() { return EventQueue.list.length === 0; },
+
+ nextEvent: function(he) {
+ for (var i=0, ls=EventQueue.list, l=ls.length; i<l; ++i) {
+ if (ls[i] == he) return ls[i+1];
+ }
+ return null;
+ },
+
+ min: function() {
+ var elem = EventQueue.list[0];
+ return {
+ x: elem.vertex.x,
+ y: elem.ystar
+ };
+ },
+
+ extractMin: function() {
+ return EventQueue.list.shift();
+ }
+ };
+
+ EdgeList.init();
+ Sites.bottomSite = Sites.list.shift();
+
+ var newSite = Sites.list.shift(), newIntStar;
+ var lbnd, rbnd, llbnd, rrbnd, bisector;
+ var bot, top, temp, p, v;
+ var e, pm;
+
+ while (true) {
+ if (!EventQueue.empty()) {
+ newIntStar = EventQueue.min();
+ }
+ if (newSite && (EventQueue.empty()
+ || newSite.y < newIntStar.y
+ || (newSite.y == newIntStar.y
+ && newSite.x < newIntStar.x))) { //new site is smallest
+ lbnd = EdgeList.leftBound(newSite);
+ rbnd = EdgeList.right(lbnd);
+ bot = EdgeList.rightRegion(lbnd);
+ e = Geom.bisect(bot, newSite);
+ bisector = EdgeList.createHalfEdge(e, "l");
+ EdgeList.insert(lbnd, bisector);
+ p = Geom.intersect(lbnd, bisector);
+ if (p) {
+ EventQueue.del(lbnd);
+ EventQueue.insert(lbnd, p, Geom.distance(p, newSite));
+ }
+ lbnd = bisector;
+ bisector = EdgeList.createHalfEdge(e, "r");
+ EdgeList.insert(lbnd, bisector);
+ p = Geom.intersect(bisector, rbnd);
+ if (p) {
+ EventQueue.insert(bisector, p, Geom.distance(p, newSite));
+ }
+ newSite = Sites.list.shift();
+ } else if (!EventQueue.empty()) { //intersection is smallest
+ lbnd = EventQueue.extractMin();
+ llbnd = EdgeList.left(lbnd);
+ rbnd = EdgeList.right(lbnd);
+ rrbnd = EdgeList.right(rbnd);
+ bot = EdgeList.leftRegion(lbnd);
+ top = EdgeList.rightRegion(rbnd);
+ v = lbnd.vertex;
+ Geom.endPoint(lbnd.edge, lbnd.side, v);
+ Geom.endPoint(rbnd.edge, rbnd.side, v);
+ EdgeList.del(lbnd);
+ EventQueue.del(rbnd);
+ EdgeList.del(rbnd);
+ pm = "l";
+ if (bot.y > top.y) {
+ temp = bot;
+ bot = top;
+ top = temp;
+ pm = "r";
+ }
+ e = Geom.bisect(bot, top);
+ bisector = EdgeList.createHalfEdge(e, pm);
+ EdgeList.insert(llbnd, bisector);
+ Geom.endPoint(e, d3_voronoi_opposite[pm], v);
+ p = Geom.intersect(llbnd, bisector);
+ if (p) {
+ EventQueue.del(llbnd);
+ EventQueue.insert(llbnd, p, Geom.distance(p, bot));
+ }
+ p = Geom.intersect(bisector, rrbnd);
+ if (p) {
+ EventQueue.insert(bisector, p, Geom.distance(p, bot));
+ }
+ } else {
+ break;
+ }
+ }//end while
+
+ for (lbnd = EdgeList.right(EdgeList.leftEnd);
+ lbnd != EdgeList.rightEnd;
+ lbnd = EdgeList.right(lbnd)) {
+ callback(lbnd.edge);
+ }
+}
+/**
+* @param vertices [[x1, y1], [x2, y2], …]
+* @returns triangles [[[x1, y1], [x2, y2], [x3, y3]], …]
+ */
+d3.geom.delaunay = function(vertices) {
+ var edges = vertices.map(function() { return []; }),
+ triangles = [];
+
+ // Use the Voronoi tessellation to determine Delaunay edges.
+ d3_voronoi_tessellate(vertices, function(e) {
+ edges[e.region.l.index].push(vertices[e.region.r.index]);
+ });
+
+ // Reconnect the edges into counterclockwise triangles.
+ edges.forEach(function(edge, i) {
+ var v = vertices[i],
+ cx = v[0],
+ cy = v[1];
+ edge.forEach(function(v) {
+ v.angle = Math.atan2(v[0] - cx, v[1] - cy);
+ });
+ edge.sort(function(a, b) {
+ return a.angle - b.angle;
+ });
+ for (var j = 0, m = edge.length - 1; j < m; j++) {
+ triangles.push([v, edge[j], edge[j + 1]]);
+ }
+ });
+
+ return triangles;
+};
+// Constructs a new quadtree for the specified array of points. A quadtree is a
+// two-dimensional recursive spatial subdivision. This implementation uses
+// square partitions, dividing each square into four equally-sized squares. Each
+// point exists in a unique node; if multiple points are in the same position,
+// some points may be stored on internal nodes rather than leaf nodes. Quadtrees
+// can be used to accelerate various spatial operations, such as the Barnes-Hut
+// approximation for computing n-body forces, or collision detection.
+d3.geom.quadtree = function(points, x1, y1, x2, y2) {
+ var p,
+ i = -1,
+ n = points.length;
+
+ // Type conversion for deprecated API.
+ if (n && isNaN(points[0].x)) points = points.map(d3_geom_quadtreePoint);
+
+ // Allow bounds to be specified explicitly.
+ if (arguments.length < 5) {
+ if (arguments.length === 3) {
+ y2 = x2 = y1;
+ y1 = x1;
+ } else {
+ x1 = y1 = Infinity;
+ x2 = y2 = -Infinity;
+
+ // Compute bounds.
+ while (++i < n) {
+ p = points[i];
+ if (p.x < x1) x1 = p.x;
+ if (p.y < y1) y1 = p.y;
+ if (p.x > x2) x2 = p.x;
+ if (p.y > y2) y2 = p.y;
+ }
+
+ // Squarify the bounds.
+ var dx = x2 - x1,
+ dy = y2 - y1;
+ if (dx > dy) y2 = y1 + dx;
+ else x2 = x1 + dy;
+ }
+ }
+
+ // Recursively inserts the specified point p at the node n or one of its
+ // descendants. The bounds are defined by [x1, x2] and [y1, y2].
+ function insert(n, p, x1, y1, x2, y2) {
+ if (isNaN(p.x) || isNaN(p.y)) return; // ignore invalid points
+ if (n.leaf) {
+ var v = n.point;
+ if (v) {
+ // If the point at this leaf node is at the same position as the new
+ // point we are adding, we leave the point associated with the
+ // internal node while adding the new point to a child node. This
+ // avoids infinite recursion.
+ if ((Math.abs(v.x - p.x) + Math.abs(v.y - p.y)) < .01) {
+ insertChild(n, p, x1, y1, x2, y2);
+ } else {
+ n.point = null;
+ insertChild(n, v, x1, y1, x2, y2);
+ insertChild(n, p, x1, y1, x2, y2);
+ }
+ } else {
+ n.point = p;
+ }
+ } else {
+ insertChild(n, p, x1, y1, x2, y2);
+ }
+ }
+
+ // Recursively inserts the specified point p into a descendant of node n. The
+ // bounds are defined by [x1, x2] and [y1, y2].
+ function insertChild(n, p, x1, y1, x2, y2) {
+ // Compute the split point, and the quadrant in which to insert p.
+ var sx = (x1 + x2) * .5,
+ sy = (y1 + y2) * .5,
+ right = p.x >= sx,
+ bottom = p.y >= sy,
+ i = (bottom << 1) + right;
+
+ // Recursively insert into the child node.
+ n.leaf = false;
+ n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode());
+
+ // Update the bounds as we recurse.
+ if (right) x1 = sx; else x2 = sx;
+ if (bottom) y1 = sy; else y2 = sy;
+ insert(n, p, x1, y1, x2, y2);
+ }
+
+ // Create the root node.
+ var root = d3_geom_quadtreeNode();
+
+ root.add = function(p) {
+ insert(root, p, x1, y1, x2, y2);
+ };
+
+ root.visit = function(f) {
+ d3_geom_quadtreeVisit(f, root, x1, y1, x2, y2);
+ };
+
+ // Insert all points.
+ points.forEach(root.add);
+ return root;
+};
+
+function d3_geom_quadtreeNode() {
+ return {
+ leaf: true,
+ nodes: [],
+ point: null
+ };
+}
+
+function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) {
+ if (!f(node, x1, y1, x2, y2)) {
+ var sx = (x1 + x2) * .5,
+ sy = (y1 + y2) * .5,
+ children = node.nodes;
+ if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy);
+ if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy);
+ if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2);
+ if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2);
+ }
+}
+
+function d3_geom_quadtreePoint(p) {
+ return {
+ x: p[0],
+ y: p[1]
+ };
+}
+})();
View
4,676 viz/d3.js
4,676 additions, 0 deletions not shown
View
1,891 viz/d3.layout.js
@@ -0,0 +1,1891 @@
+(function(){d3.layout = {};
+// Implements hierarchical edge bundling using Holten's algorithm. For each
+// input link, a path is computed that travels through the tree, up the parent
+// hierarchy to the least common ancestor, and then back down to the destination
+// node. Each path is simply an array of nodes.
+d3.layout.bundle = function() {
+ return function(links) {
+ var paths = [],
+ i = -1,
+ n = links.length;
+ while (++i < n) paths.push(d3_layout_bundlePath(links[i]));
+ return paths;
+ };
+};
+
+function d3_layout_bundlePath(link) {
+ var start = link.source,
+ end = link.target,
+ lca = d3_layout_bundleLeastCommonAncestor(start, end),
+ points = [start];
+ while (start !== lca) {
+ start = start.parent;
+ points.push(start);
+ }
+ var k = points.length;
+ while (end !== lca) {
+ points.splice(k, 0, end);
+ end = end.parent;
+ }
+ return points;
+}
+
+function d3_layout_bundleAncestors(node) {
+ var ancestors = [],
+ parent = node.parent;
+ while (parent != null) {
+ ancestors.push(node);
+ node = parent;
+ parent = parent.parent;
+ }
+ ancestors.push(node);
+ return ancestors;
+}
+
+function d3_layout_bundleLeastCommonAncestor(a, b) {
+ if (a === b) return a;
+ var aNodes = d3_layout_bundleAncestors(a),
+ bNodes = d3_layout_bundleAncestors(b),
+ aNode = aNodes.pop(),
+ bNode = bNodes.pop(),
+ sharedNode = null;
+ while (aNode === bNode) {
+ sharedNode = aNode;
+ aNode = aNodes.pop();
+ bNode = bNodes.pop();
+ }
+ return sharedNode;
+}
+d3.layout.chord = function() {
+ var chord = {},
+ chords,
+ groups,
+ matrix,
+ n,
+ padding = 0,
+ sortGroups,
+ sortSubgroups,
+ sortChords;
+
+ function relayout() {
+ var subgroups = {},
+ groupSums = [],
+ groupIndex = d3.range(n),
+ subgroupIndex = [],
+ k,
+ x,
+ x0,
+ i,
+ j;
+
+ chords = [];
+ groups = [];
+
+ // Compute the sum.
+ k = 0, i = -1; while (++i < n) {
+ x = 0, j = -1; while (++j < n) {
+ x += matrix[i][j];
+ }
+ groupSums.push(x);
+ subgroupIndex.push(d3.range(n));
+ k += x;
+ }
+
+ // Sort groups…
+ if (sortGroups) {
+ groupIndex.sort(function(a, b) {
+ return sortGroups(groupSums[a], groupSums[b]);
+ });
+ }
+
+ // Sort subgroups…
+ if (sortSubgroups) {
+ subgroupIndex.forEach(function(d, i) {
+ d.sort(function(a, b) {
+ return sortSubgroups(matrix[i][a], matrix[i][b]);
+ });
+ });
+ }
+
+ // Convert the sum to scaling factor for [0, 2pi].
+ // TODO Allow start and end angle to be specified.
+ // TODO Allow padding to be specified as percentage?
+ k = (2 * Math.PI - padding * n) / k;
+
+ // Compute the start and end angle for each group and subgroup.
+ // Note: Opera has a bug reordering object literal properties!
+ x = 0, i = -1; while (++i < n) {
+ x0 = x, j = -1; while (++j < n) {
+ var di = groupIndex[i],
+ dj = subgroupIndex[di][j],
+ v = matrix[di][dj],
+ a0 = x,
+ a1 = x += v * k;
+ subgroups[di + "-" + dj] = {
+ index: di,
+ subindex: dj,
+ startAngle: a0,
+ endAngle: a1,
+ value: v
+ };
+ }
+ groups.push({
+ index: di,
+ startAngle: x0,
+ endAngle: x,
+ value: (x - x0) / k
+ });
+ x += padding;
+ }
+
+ // Generate chords for each (non-empty) subgroup-subgroup link.
+ i = -1; while (++i < n) {
+ j = i - 1; while (++j < n) {
+ var source = subgroups[i + "-" + j],
+ target = subgroups[j + "-" + i];
+ if (source.value || target.value) {
+ chords.push(source.value < target.value
+ ? {source: target, target: source}
+ : {source: source, target: target});
+ }
+ }
+ }
+
+ if (sortChords) resort();
+ }
+
+ function resort() {
+ chords.sort(function(a, b) {
+ return sortChords(
+ (a.source.value + a.target.value) / 2,
+ (b.source.value + b.target.value) / 2);
+ });
+ }
+
+ chord.matrix = function(x) {
+ if (!arguments.length) return matrix;
+ n = (matrix = x) && matrix.length;
+ chords = groups = null;
+ return chord;
+ };
+
+ chord.padding = function(x) {
+ if (!arguments.length) return padding;
+ padding = x;
+ chords = groups = null;
+ return chord;
+ };
+
+ chord.sortGroups = function(x) {
+ if (!arguments.length) return sortGroups;
+ sortGroups = x;
+ chords = groups = null;
+ return chord;
+ };
+
+ chord.sortSubgroups = function(x) {
+ if (!arguments.length) return sortSubgroups;
+ sortSubgroups = x;
+ chords = null;
+ return chord;
+ };
+
+ chord.sortChords = function(x) {
+ if (!arguments.length) return sortChords;
+ sortChords = x;
+ if (chords) resort();
+ return chord;
+ };
+
+ chord.chords = function() {
+ if (!chords) relayout();
+ return chords;
+ };
+
+ chord.groups = function() {
+ if (!groups) relayout();
+ return groups;
+ };
+
+ return chord;
+};
+// A rudimentary force layout using Gauss-Seidel.
+d3.layout.force = function() {
+ var force = {},
+ event = d3.dispatch("tick"),
+ size = [1, 1],
+ drag,
+ alpha,
+ friction = .9,
+ linkDistance = d3_layout_forceLinkDistance,
+ linkStrength = d3_layout_forceLinkStrength,
+ charge = -30,
+ gravity = .1,
+ theta = .8,
+ interval,
+ nodes = [],
+ links = [],
+ distances,
+ strengths,
+ charges;
+
+ function repulse(node) {
+ return function(quad, x1, y1, x2, y2) {
+ if (quad.point !== node) {
+ var dx = quad.cx - node.x,
+ dy = quad.cy - node.y,
+ dn = 1 / Math.sqrt(dx * dx + dy * dy);
+
+ /* Barnes-Hut criterion. */
+ if ((x2 - x1) * dn < theta) {
+ var k = quad.charge * dn * dn;
+ node.px -= dx * k;
+ node.py -= dy * k;
+ return true;
+ }
+
+ if (quad.point && isFinite(dn)) {
+ var k = quad.pointCharge * dn * dn;
+ node.px -= dx * k;
+ node.py -= dy * k;
+ }
+ }
+ return !quad.charge;
+ };
+ }
+
+ function tick() {
+ var n = nodes.length,
+ m = links.length,
+ q,
+ i, // current index
+ o, // current object
+ s, // current source
+ t, // current target
+ l, // current distance
+ k, // current force
+ x, // x-distance
+ y; // y-distance
+
+ // gauss-seidel relaxation for links
+ for (i = 0; i < m; ++i) {
+ o = links[i];
+ s = o.source;
+ t = o.target;
+ x = t.x - s.x;
+ y = t.y - s.y;
+ if (l = (x * x + y * y)) {
+ l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l;
+ x *= l;
+ y *= l;
+ t.x -= x * (k = s.weight / (t.weight + s.weight));
+ t.y -= y * k;
+ s.x += x * (k = 1 - k);
+ s.y += y * k;
+ }
+ }
+
+ // apply gravity forces
+ if (k = alpha * gravity) {
+ x = size[0] / 2;
+ y = size[1] / 2;
+ i = -1; if (k) while (++i < n) {
+ o = nodes[i];
+ o.x += (x - o.x) * k;
+ o.y += (y - o.y) * k;
+ }
+ }
+
+ // compute quadtree center of mass and apply charge forces
+ if (charge) {
+ d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges);
+ i = -1; while (++i < n) {
+ if (!(o = nodes[i]).fixed) {
+ q.visit(repulse(o));
+ }
+ }
+ }
+
+ // position verlet integration
+ i = -1; while (++i < n) {
+ o = nodes[i];
+ if (o.fixed) {
+ o.x = o.px;
+ o.y = o.py;
+ } else {
+ o.x -= (o.px - (o.px = o.x)) * friction;
+ o.y -= (o.py - (o.py = o.y)) * friction;
+ }
+ }
+
+ event.tick({type: "tick", alpha: alpha});
+
+ // simulated annealing, basically
+ return (alpha *= .99) < .005;
+ }
+
+ force.nodes = function(x) {
+ if (!arguments.length) return nodes;
+ nodes = x;
+ return force;
+ };
+
+ force.links = function(x) {
+ if (!arguments.length) return links;
+ links = x;
+ return force;
+ };
+
+ force.size = function(x) {
+ if (!arguments.length) return size;
+ size = x;
+ return force;
+ };
+
+ force.linkDistance = function(x) {
+ if (!arguments.length) return linkDistance;
+ linkDistance = d3.functor(x);
+ return force;
+ };
+
+ // For backwards-compatibility.
+ force.distance = force.linkDistance;
+
+ force.linkStrength = function(x) {
+ if (!arguments.length) return linkStrength;
+ linkStrength = d3.functor(x);
+ return force;
+ };
+
+ force.friction = function(x) {
+ if (!arguments.length) return friction;
+ friction = x;
+ return force;
+ };
+
+ force.charge = function(x) {
+ if (!arguments.length) return charge;
+ charge = typeof x === "function" ? x : +x;
+ return force;
+ };
+
+ force.gravity = function(x) {
+ if (!arguments.length) return gravity;
+ gravity = x;
+ return force;
+ };
+
+ force.theta = function(x) {
+ if (!arguments.length) return theta;
+ theta = x;
+ return force;
+ };
+
+ force.start = function() {
+ var i,
+ j,
+ n = nodes.length,
+ m = links.length,
+ w = size[0],
+ h = size[1],
+ neighbors,
+ o;
+
+ for (i = 0; i < n; ++i) {
+ (o = nodes[i]).index = i;
+ o.weight = 0;
+ }
+
+ distances = [];