From 8ed60313ac8677ebf06b10537fc721036b5e16a5 Mon Sep 17 00:00:00 2001 From: Chris Bell Date: Tue, 14 Aug 2012 16:32:25 +0100 Subject: [PATCH] Initial version --- README.md | 8 ++++ src/.DS_Store | Bin 0 -> 6148 bytes src/gist.js | 87 ++++++++++++++++++++++++++++++++++++++++++ src/markdown.js | 43 +++++++++++++++++++++ src/ordered-list.js | 45 ++++++++++++++++++++++ src/tweet.js | 91 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 274 insertions(+) create mode 100644 README.md create mode 100644 src/.DS_Store create mode 100644 src/gist.js create mode 100644 src/markdown.js create mode 100644 src/ordered-list.js create mode 100644 src/tweet.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..1f297a3 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Sir Trevor Blocks + +A place for Sir Trevor Blocks that don't come packaged as the default set. + +# Documentation + +Each BlockType should be documented with a header + diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0", + + dropzoneHTML: template, + + validate: function() {}, + + loadData: function(data){ + this.loading(); + this._super("loadGist", data.id); + }, + + loadGist: function(gist_id) { + // Get the gist data (too big to store in JSON) + var callbackSuccess = function(data) { + // Streamlined (we can't store the div item, we'll need to grab it each time) + var obj = { + id: gist_id + }; + + var dataStruct = this.$el.data('block'); + dataStruct.data = obj; + + $('head').append(''); + + this.$el.html(data.div); + this.$dropzone.fadeOut(250); + this.$el.show(); + this.ready(); + }; + + var callbackFail = function(){ + this.ready(); + }; + + // Make our AJAX call + $.ajax({ + url: "https://gist.github.com/" + gist_id + ".json", + dataType: "JSONP", + success: _.bind(callbackSuccess, this), + error: _.bind(callbackFail, this) + }); + }, + + onContentPasted: function(event){ + // Content pasted. Delegate to the drop parse method + var input = $(event.target), + val = input.val(); + this._super("handleDropPaste", val); + }, + + handleDropPaste: function(url) { + if(_.isURI(url)) + { + if (url.indexOf("gist") != -1) { + // Twitter status + var ID = url.match(/[^\/]+$/); + + if (!_.isEmpty(ID)) { + this.loading(); + + ID = ID[0]; + this._super("loadGist", ID); + } + } + } + }, + + onDrop: function(transferData){ + var url = transferData.getData('text/plain'); + this._super("handleDropPaste", url); + } +}); + +SirTrevor.BlockTypes.Gist = new Gist(); \ No newline at end of file diff --git a/src/markdown.js b/src/markdown.js new file mode 100644 index 0000000..01e0715 --- /dev/null +++ b/src/markdown.js @@ -0,0 +1,43 @@ +/* + Text Block +*/ + +var md_template = '

'; + +var Markdown = SirTrevor.BlockType.extend({ + + title: "Markdown", + className: "markdown", + + editorHTML: function() { + return _.template(md_template, this); + }, + + loadData: function(data){ + this.$('.markdown').html(data.text); + }, + + onBlockRender: function(){ + /* Make our expanding text area */ + + var cont = this.$('.expanding-textarea'), + area = cont.find('textarea'), + span = cont.find('span'); + + area.bind('input', function(){ + span.text(area.val()); + }); + + cont.addClass('active'); + }, + + toData: function() { + var bl = this.$el, + dataStruct = bl.data('block'), + content; + + dataStruct.data.text = this.$('.markdown').val(); + } +}); + +SirTrevor.BlockTypes.Markdown = new Markdown(); \ No newline at end of file diff --git a/src/ordered-list.js b/src/ordered-list.js new file mode 100644 index 0000000..0925a41 --- /dev/null +++ b/src/ordered-list.js @@ -0,0 +1,45 @@ +/* + Ordered List +*/ + +var od_template = '
'; + +var OrderedList = SirTrevor.BlockType.extend({ + + title: "List", + className: "ordered-list", + + editorHTML: function() { + return _.template(od_template, this); + }, + + onBlockRender: function(){ + + this.$('.text-block').bind('click', function(){ + if($(this).html().length === 0){ + document.execCommand("insertOrderedList",false,false); + } + }); + + // Put in a list + if (_.isEmpty(this.data)) { + this.$('.text-block').focus().click(); + } + }, + + loadData: function(data){ + this.$('.text-block').html("
    " + this.instance._toHTML(data.text, this.type) + "
"); + }, + + toMarkdown: function(markdown){ + return markdown.replace(/<\/li>/mg,"\n") + .replace(/<\/?[^>]+(>|$)/g, "") + .replace(/^(.+)$/mg," 1. $1"); + }, + + toHTML: function(html) { + return html.replace(/^ 1. (.+)$/mg,"
  • $1
  • "); + } +}); + +SirTrevor.BlockTypes.OrderedList = new OrderedList(); \ No newline at end of file diff --git a/src/tweet.js b/src/tweet.js new file mode 100644 index 0000000..35d8241 --- /dev/null +++ b/src/tweet.js @@ -0,0 +1,91 @@ + +var t_template = '

    Drop tweet link here

    '; +var tweet_template = '

    @<%= user.screen_name %>: <%= text %>

    '; + +var Tweet = SirTrevor.BlockType.extend({ + + title: "Tweet", + className: "tweet", + dropEnabled: true, + + dropzoneHTML: template, + + validate: function() {}, + + loadData: function(data){ + this.$block.find(".dropzone").hide(); + this.$el.show(); + this.$el.html(_.template(t_template, data)); + }, + + onContentPasted: function(event){ + // Content pasted. Delegate to the drop parse method + var input = $(event.target), + val = input.val(); + + // Pass this to the same handler as onDrop + this._super("handleTwitterDropPaste", val); + }, + + handleTwitterDropPaste: function(url){ + + if(_.isURI(url)) + { + if (url.indexOf("twitter") != -1 && url.indexOf("status") != -1) { + // Twitter status + var tweetID = url.match(/[^\/]+$/); + if (!_.isEmpty(tweetID)) { + + this.loading(); + + tweetID = tweetID[0]; + + var tweetCallbackSuccess = function(data) { + // Parse the twitter object into something a bit slimmer.. + var obj = { + user: { + profile_image_url: data.user.profile_image_url, + profile_image_url_https: data.user.profile_image_url_https, + screen_name: data.user.screen_name, + name: data.user.name + }, + text: data.text, + created_at: data.created_at, + status_url: url + }; + + // Save this data on the block + var dataStruct = this.$el.data('block'); + dataStruct.data = obj; + + this.$el.html(_.template(tweet_template, obj)); // Render + this.$dropzone.hide(); + this.$el.show(); + + this.ready(); + }; + + var tweetCallbackFail = function(){ + this.ready(); + }; + + // Make our AJAX call + $.ajax({ + url: "http://api.twitter.com/1/statuses/show/" + tweetID + ".json", + dataType: "JSONP", + success: _.bind(tweetCallbackSuccess, this), + error: _.bind(tweetCallbackFail, this) + }); + } + } + } + + }, + + onDrop: function(transferData){ + var url = transferData.getData('text/plain'); + this._super("handleTwitterDropPaste", url); + } +}); + +SirTrevor.BlockTypes.Tweet = new Tweet(); \ No newline at end of file