From 870ad3d9f78b5959f88c7e9e52ada6b875ce9135 Mon Sep 17 00:00:00 2001
From: Dennis Collinson <dennis.collective@gmail.com>
Date: Mon, 23 Jan 2012 18:08:01 -0800
Subject: [PATCH] DC DG; some fixes, mentioning works.

---
 .../javascripts/app/views/publisher_view.js   |   3 +-
 public/javascripts/mentions.js                |   3 +
 public/javascripts/publisher.js               |  40 +--
 spec/javascripts/publisher-spec.js            | 255 ------------------
 spec/javascripts/support/jasmine.yml          |   4 +-
 5 files changed, 20 insertions(+), 285 deletions(-)

diff --git a/public/javascripts/app/views/publisher_view.js b/public/javascripts/app/views/publisher_view.js
index b33f4ea2e5..7f8e9d9168 100644
--- a/public/javascripts/app/views/publisher_view.js
+++ b/public/javascripts/app/views/publisher_view.js
@@ -54,8 +54,7 @@ app.views.Publisher = Backbone.View.extend({
     // close publishing area (CSS)
     this.close();
 
-    // clear mentions (TO BE REMOVED!!)
-    Publisher.autocompletion.mentionList.clear()
+    Publisher.clear()
 
     return this;
   },
diff --git a/public/javascripts/mentions.js b/public/javascripts/mentions.js
index f9f9d5c025..6436055bbe 100644
--- a/public/javascripts/mentions.js
+++ b/public/javascripts/mentions.js
@@ -1,7 +1,10 @@
 var Mentions = {
   initialize: function(mentionsInput) {
     mentionsInput.mentionsInput(Mentions.options);
+    Mentions.fetchContacts();
+  },
 
+  fetchContacts : function(){
     $.getJSON($(".selected_contacts_link").attr("href"), function(data) {
       Mentions.contacts = data;
     });
diff --git a/public/javascripts/publisher.js b/public/javascripts/publisher.js
index 576e9a72eb..0f7d3c6419 100644
--- a/public/javascripts/publisher.js
+++ b/public/javascripts/publisher.js
@@ -5,39 +5,22 @@
 
 //TODO: make this a widget
 var Publisher = {
-
   bookmarklet : false,
 
-  cachedForm : false,
   form: function(){
-    if(!Publisher.cachedForm){
-      Publisher.cachedForm = $('#publisher');
-    }
-    return Publisher.cachedForm;
+    return Publisher.cachedForm = Publisher.cachedForm || $('#publisher');
   },
 
-  cachedInput : false,
   input: function(){
-    if(!Publisher.cachedInput){
-      Publisher.cachedInput = Publisher.form().find('#status_message_fake_text');
-    }
-    return Publisher.cachedInput;
+    return Publisher.cachedInput = Publisher.cachedInput || Publisher.form().find('#status_message_fake_text');
   },
 
-  cachedHiddenInput : false,
   hiddenInput: function(){
-    if(!Publisher.cachedHiddenInput){
-      Publisher.cachedHiddenInput = Publisher.form().find('#status_message_text');
-    }
-    return Publisher.cachedHiddenInput;
+    return Publisher.cachedHiddenInput= Publisher.cachedHiddenInput || Publisher.form().find('#status_message_text');
   },
 
-  cachedSubmit : false,
   submit: function(){
-    if(!Publisher.cachedSubmit){
-      Publisher.cachedSubmit = Publisher.form().find('#status_message_submit');
-    }
-    return Publisher.cachedSubmit;
+    return Publisher.cachedSubmit = Publisher.cachedSubmit || Publisher.form().find('#status_message_submit');
   },
 
   determineSubmitAvailability: function(){
@@ -54,7 +37,8 @@ var Publisher = {
 
   clear: function(){
     $("#photodropzone").find('li').remove();
-    Publisher.input().removeClass("with_attachments")
+    Publisher.input()
+      .removeClass("with_attachments")
       .css('paddingBottom', '')
       .mentionsInput("reset");
   },
@@ -165,16 +149,21 @@ var Publisher = {
       Publisher.toggleAspectIds(li);
     });
   },
+
   beforeSubmit: function(){
     if($("#publisher .content_creation form #aspect_ids_").length == 0){
       alert(Diaspora.I18n.t('publisher.at_least_one_aspect'));
       return false;
     }
+  },
 
+  keyUp : function(){
+    Publisher.determineSubmitAvailability()
     Publisher.input().mentionsInput("val", function(value) {
       Publisher.hiddenInput().val(value);
     });
   },
+
   onSubmit: function(data, json, xhr){
     $("#photodropzone").find('li').remove();
     Publisher.input().removeClass("with_attachments").css('paddingBottom', '');
@@ -217,8 +206,8 @@ var Publisher = {
     //Stream.setUpImageLinks();
     Stream.setUpAudioLinks();
   },
+
   bindAjax: function(){
-    //Publisher.form().bind('submit', Publisher.beforeSubmit);
     Publisher.form().bind('ajax:loading', Publisher.onSubmit);
     Publisher.form().bind('ajax:failure', Publisher.onFailure);
     Publisher.form().bind('ajax:success', Publisher.onSuccess);
@@ -277,10 +266,7 @@ var Publisher = {
     }
 
     Publisher.input().autoResize({'extraSpace' : 10});
-
-    Publisher.form().find("textarea").bind("focus", function(evt) {
-      Publisher.open();
-    });
+    Publisher.input().keyup(Publisher.keyUp)
   }
 };
 
diff --git a/spec/javascripts/publisher-spec.js b/spec/javascripts/publisher-spec.js
index 56bebe5ffc..af84eeb396 100644
--- a/spec/javascripts/publisher-spec.js
+++ b/spec/javascripts/publisher-spec.js
@@ -174,259 +174,4 @@ describe("Publisher", function() {
       expect(Publisher.input().length).toBe(1);
     });
   });
-
-  describe("autocompletion", function(){
-    describe("searchTermFromValue", function(){
-      beforeEach(function(){
-        this.func = Publisher.autocompletion.searchTermFromValue;
-      });
-
-      it("returns nothing if the cursor is before the @", function(){
-        expect(this.func('not @dan grip', 2)).toBe('');
-      });
-
-      it("returns everything up to the cursor if the cursor is a word after that @", function(){
-        expect(this.func('not @dan grip', 13)).toBe('dan grip');
-      });
-
-      it("returns up to the cursor if the cursor is after that @", function(){
-        expect(this.func('not @dan grip', 7)).toBe('da');
-      });
-
-      it("returns everything after an @ at the start of the line", function(){
-        expect(this.func('@dan grip', 9)).toBe('dan grip');
-      });
-      it("returns nothing if there is no @", function(){
-        expect(this.func('dan', 3)).toBe('');
-      });
-      it("returns nothing for just an @", function(){
-        expect(this.func('@', 1)).toBe('');
-      });
-      it("returns nothing if there are letters preceding the @", function(){
-        expect(this.func('ioj@asdo', 8)).toBe('');
-      });
-      it("returns everything up to the cursor if there are 2 @s and the cursor is between them", function(){
-        expect(this.func('@asdpo  aoisdj @asodk', 8)).toBe('asdpo');
-      });
-      it("returns everything from the 2nd @ up to the cursor if there are 2 @s and the cursor after them", function(){
-        expect(this.func('@asod asdo @asd asok', 15)).toBe('asd');
-      });
-    });
-
-    describe("mentionList", function(){
-      beforeEach(function(){
-        spec.loadFixture('aspects_index');
-
-        this.list = Publisher.autocompletion.mentionList;
-        this.visibleInput = Publisher.input();
-        this.hiddenInput = Publisher.hiddenInput();
-        this.mention = { visibleStart : 0,
-          visibleEnd   : 5,
-          mentionString : "@{Danny; dan@pod.org}"
-        };
-
-        this.list.mentions = [];
-        this.list.push(this.mention);
-        this.visibleVal = "Danny loves testing javascript";
-        this.visibleInput.val(this.visibleVal);
-        this.hiddenVal = "@{Danny; dan@pod.org} loves testing javascript";
-        this.hiddenInput.val(this.hiddenVal);
-      });
-
-      describe("selectionDeleted", function(){
-        beforeEach(function(){
-          this.func = this.list.selectionDeleted;
-          this.visibleVal = "Danny Daniel David Darren";
-          this.visibleInput.val(this.visibleVal);
-          this.list.mentions = [];
-          this.danny = {
-            visibleStart : 0,
-            visibleEnd : 5,
-            mentionString : "@{Danny; danny@pod.org}"
-          };
-          this.daniel = {
-            visibleStart : 6,
-            visibleEnd : 12,
-            mentionString : "@{Daniel; daniel@pod.org}"
-          };
-          this.david = {
-            visibleStart : 13,
-            visibleEnd : 18,
-            mentionString : "@{David; david@pod.org}"
-          };
-          this.darren = {
-            visibleStart : 19,
-            visibleEnd : 25,
-            mentionString : "@{Darren; darren@pod.org}"
-          };
-
-          _.each([this.danny, this.daniel, this.david, this.darren], function(person){
-            this.list.push(person);
-          }, this);
-        });
-
-        it("destroys mentions within the selection", function(){
-          this.func(4,11);
-          expect(this.list.sortedMentions()).toEqual([this.darren, this.david])
-        });
-
-        it("moves remaining mentions back", function(){
-          this.func(7,14);
-          var length = 11 - 4;
-
-          expect(this.danny.visibleStart).toBe(0);
-          expect(this.darren.visibleStart).toBe(19-length);
-        });
-      });
-
-      describe("generateHiddenInput", function(){
-        it("replaces mentions in a string", function(){
-          expect(this.list.generateHiddenInput(this.visibleVal)).toBe(this.hiddenVal);
-        });
-      });
-
-      describe("push", function(){
-        it("adds mention to mentions array", function(){
-          expect(this.list.mentions.length).toBe(1);
-          expect(this.list.mentions[0]).toBe(this.mention)
-        });
-      });
-
-      describe("mentionAt", function(){
-        it("returns the location of the mention at that location in the mentions array", function(){
-          expect(this.list.mentions[this.list.mentionAt(3)]).toBe(this.mention);
-        });
-
-        it("returns null if there is no mention", function(){
-          expect(this.list.mentionAt(8)).toBeFalsy();
-        });
-      });
-
-      describe("insertionAt", function(){
-        it("does nothing if there is no visible mention at that index", function(){
-          this.list.insertionAt(8);
-          expect(this.visibleInput.val()).toBe(this.visibleVal);
-          expect(this.hiddenInput.val()).toBe(this.hiddenVal);
-        });
-
-        it("deletes the mention from the hidden field if there is a mention", function(){
-          this.list.insertionAt(3);
-          expect(this.visibleInput.val()).toBe(this.visibleVal);
-          expect(this.list.generateHiddenInput(this.visibleInput.val())).toBe(this.visibleVal);
-        });
-
-        it("deletes the mention from the list", function(){
-          this.list.insertionAt(3);
-          expect(this.list.mentionAt(3)).toBeFalsy();
-        });
-
-        it("calls updateMentionLocations", function(){
-          mentionTwo = { visibleStart : 8,
-            visibleEnd   : 15,
-            mentionString : "@{SomeoneElse; other@pod.org}"
-          };
-          this.list.push(mentionTwo);
-
-          spyOn(this.list, 'updateMentionLocations');
-          this.list.insertionAt(3,4, 60);
-          expect(this.list.updateMentionLocations).toHaveBeenCalled();
-        });
-      });
-
-      describe("updateMentionLocations", function(){
-        it("updates the offsets of the remaining mentions in the list", function(){
-          mentionTwo = { visibleStart : 8,
-            visibleEnd   : 15,
-            mentionString : "@{SomeoneElse; other@pod.org}"
-          };
-          this.list.push(mentionTwo);
-          this.list.updateMentionLocations(7, 1);
-          expect(mentionTwo.visibleStart).toBe(9);
-          expect(mentionTwo.visibleEnd).toBe(16);
-        });
-      });
-    });
-
-    describe("keyUpHandler", function(){
-      beforeEach(function(){
-        spec.loadFixture('aspects_index');
-        Publisher.initialize();
-        this.input = Publisher.input();
-        this.submit = Publisher.submit();
-        Publisher.open();
-      });
-
-      it("keep the share button disabled when adding only whitespaces", function(){
-        expect(this.submit.attr('disabled')).toBeTruthy();
-        this.input.val(' ');
-        this.input.keyup();
-        expect(this.submit.attr('disabled')).toBeTruthy();
-      });
-
-      it("enable the share button when adding non-whitespace characters", function(){
-        expect(this.submit.attr('disabled')).toBeTruthy();
-        this.input.val('some text');
-        this.input.keyup();
-        expect(this.submit.attr('disabled')).toBeFalsy();
-      });
-
-      it("should toggle share button disable/enable when playing with input", function(){
-        expect(this.submit.attr('disabled')).toBeTruthy();
-        this.input.val('         ');
-        this.input.keyup();
-        expect(this.submit.attr('disabled')).toBeTruthy();
-        this.input.val('text');
-        this.input.keyup();
-        this.expect(this.submit.attr('disabled')).toBeFalsy();
-        this.input.val('');
-        this.input.keyup();
-        expect(this.submit.attr('disabled')).toBeTruthy();
-      });
-    });
-
-    describe("addMentionToInput", function(){
-      beforeEach(function(){
-        spec.loadFixture('aspects_index');
-        this.func = Publisher.autocompletion.addMentionToInput;
-        this.input = Publisher.input();
-        this.replaceWith = "Replace with this.";
-        Publisher.autocompletion.mentionList.mentions = [];
-      });
-
-      it("replaces everything up to the cursor if the cursor is a word after that @", function(){
-        this.input.val('not @dan grip');
-        var cursorIndex = 13;
-        this.func(this.input, cursorIndex, this.replaceWith);
-        expect(this.input.val()).toBe('not ' + this.replaceWith);
-      });
-
-      it("replaces everything between @ and the cursor if the cursor is after that @", function(){
-        this.input.val('not @dan grip');
-        var cursorIndex = 7;
-        this.func(this.input, cursorIndex, this.replaceWith);
-        expect(this.input.val()).toBe('not ' + this.replaceWith + 'n grip');
-      });
-
-      it("replaces everything up to the cursor from @ at the start of the line", function(){
-        this.input.val('@dan grip');
-        var cursorIndex = 9;
-        this.func(this.input, cursorIndex, this.replaceWith);
-        expect(this.input.val()).toBe(this.replaceWith);
-      });
-
-      it("replaces everything between the first @ and the cursor if there are 2 @s and the cursor is between them", function(){
-        this.input.val('@asdpo  aoisdj @asodk');
-        var cursorIndex = 8;
-        this.func(this.input, cursorIndex, this.replaceWith);
-        expect(this.input.val()).toBe(this.replaceWith + 'aoisdj @asodk');
-      });
-
-      it("replaces everything after the 2nd @ if there are 2 @s and the cursor after them", function(){
-        this.input.val('@asod asdo @asd asok');
-        var cursorIndex = 15;
-        this.func(this.input, cursorIndex, this.replaceWith);
-        expect(this.input.val()).toBe('@asod asdo ' + this.replaceWith + ' asok');
-      });
-    });
-  });
 });
diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml
index d063d9925c..958a0fbe3b 100644
--- a/spec/javascripts/support/jasmine.yml
+++ b/spec/javascripts/support/jasmine.yml
@@ -11,6 +11,7 @@
 #   - dist/**/*.js
 #
 src_files:
+  - public/javascripts/vendor/underscore.js
   - public/javascripts/vendor/jquery-1.7.1.min.js
   - public/javascripts/vendor/jquery-ui-1.8.9.custom.min.js
   - public/javascripts/vendor/bootstrap/bootstrap-popover.js
@@ -20,11 +21,11 @@ src_files:
   - public/javascripts/vendor/jquery.autoresize.js
   - public/javascripts/vendor/jquery.expander.js
   - public/javascripts/vendor/jquery.charcount.js
+  - public/javascripts/vendor/jquery.mentionsInput.js
   - public/javascripts/vendor/timeago.js
   - public/javascripts/vendor/facebox.js
   - public/javascripts/vendor/markdown/*
   - public/javascripts/jquery.infieldlabel-custom.js
-  - public/javascripts/vendor/underscore.js
   - public/javascripts/vendor/backbone.js
   - public/javascripts/vendor/handlebars-1.0.0.beta.6.js
   - public/javascripts/fileuploader-custom.js
@@ -50,6 +51,7 @@ src_files:
   - public/javascripts/mobile.js
   - public/javascripts/contact-list.js
   - public/javascripts/view.js
+  - public/javascripts/mentions.js
   - public/javascripts/publisher.js
   - public/javascripts/stream.js
   - public/javascripts/validation.js
-- 
GitLab