define([ ], function() { function NewPost() { this.entities = JSON.parse(controller.getCachedEntities()); this.mentions = []; this.is_private = false; document.body.className = "new_post"; // Textarea this.container = $("
" + "" + "" + "" + "
"); this.buttons = { images: buttons.find("#images"), is_private: buttons.find("#private"), send: buttons.find("#send") } //this.buttons.images.bind("click", this.addImage.bind(this)); //this.buttons.is_private.bind("click", this.togglePrivate.bind(this)); this.buttons.send.bind("click", this.send.bind(this)); this.container.find("#status_bar").append(this.counter); this.container.find("#status_bar").append(buttons); this.textarea.focus() } NewPost.prototype.setString = function(string) { this.textarea.val(string); } NewPost.prototype.setMentions = function(mentions) { if(mentions && mentions.length > 0) { var mentions_string = " "; for (var i = 0; i < mentions.length; i++) { mentions_string += mentions[i].name + " "; } this.textarea.val(this.textarea.val() + " " + mentions_string); this.mentions = mentions; } this.keyup(); } NewPost.prototype.setIsPrivate = function(is_private) { this.is_private = is_private; } NewPost.prototype.toggleIsPrivate = function() { this.is_private = !this.is_private; }; NewPost.prototype.keyup = function(e) { if(!e) return; var key = e.which; if(key != 38 && key != 40 && key != 13) { this.applyText($(this.textarea).val()); } else { var lis = this.suggestions.find("li"); if (lis.length > 0) { e.preventDefault(); var active = this.suggestions.find(".active"); if(key == 38) { // up var prev = active.prev(); if(active.lentgh == 0) { lis.last().addClass("active"); } else if(prev) { active.removeClass("active"); prev.addClass("active"); } } else if(key == 40) { // down var next = active.next(); if(active.length == 0) { lis.first().addClass("active"); } else if(next) { active.removeClass("active"); next.addClass("active"); } } else if(key == 13) { // enter if(active.length > 0) { this.replaceWithName(this.textarea.val(), this.suggestions.find("li.active").get(0).item); } } } } } NewPost.prototype.keydown = function(e) { var key = e.which; var lis = this.suggestions.find("li"); if(lis.length > 0 && (key == 38 || key == 40 || key == 13)) { e.preventDefault(); } } NewPost.prototype.replaceAll = function(txt, replace, with_this) { return txt.replace(new RegExp(replace, 'g'),with_this); } NewPost.prototype.replaceWithName = function(txt, with_item) { var words = txt.match(/(^|\s)\^([^\s]+)/); var replace = words[2]; var original = txt.replace("^" + replace, with_item.name); this.textarea.val(original); this.mentions.push(with_item); this.applyText(original); } NewPost.prototype.applyText = function (text) { var words = text.match(/(^|\s)\^([^\s]+)/); this.suggestions.html(""); if(words) { var name = words[2]; for (var key in this.entities.length) { var item = this.entities[key]; if(item.name.toLowerCase().indexOf(name.toLowerCase()) != -1 || item.entity.toLowerCase().indexOf(name.toLowerCase()) != -1) { var li = $("