Permalink
Browse files

Initial.

  • Loading branch information...
rstacruz committed Aug 7, 2011
0 parents commit b981fb285590fa06020a11c81e09a6a6b16dca23
@@ -0,0 +1,59 @@
+(function($) {
+
+ /*
+ * Auto-growing textareas; technique ripped from Facebook
+ */
+ $.fn.autogrow = function(options) {
+ if (!options) { options = {}; }
+ if (!options.offset) { options.offset = 0; }
+
+ this.filter('textarea').each(function() {
+
+ var $this = $(this),
+ minHeight = $this.height(),
+ lineHeight = $this.css('lineHeight');
+
+ var shadow = $('<div></div>').css({
+ position: 'absolute',
+ top: -10000,
+ left: -10000,
+ width: $(this).width() - parseInt($this.css('paddingLeft')) - parseInt($this.css('paddingRight')),
+ fontSize: $this.css('fontSize'),
+ fontFamily: $this.css('fontFamily'),
+ lineHeight: $this.css('lineHeight'),
+ resize: 'none'
+ }).appendTo(document.body);
+
+ var update = function() {
+
+ var times = function(string, number) {
+ var _res = '';
+ for(var i = 0; i < number; i ++) {
+ _res = _res + string;
+ }
+ return _res;
+ };
+
+ var val = this.value.replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
+ .replace(/&/g, '&amp;')
+ .replace(/\n$/, '<br/>&nbsp;')
+ .replace(/\n/g, '<br/>')
+ .replace(/ {2,}/g, function(space) { return times('&nbsp;', space.length -1) + ' ' });
+
+ shadow.html(val);
+ $(this).css('height', Math.max(shadow.height() + 20 + options.offset, minHeight));
+
+ }
+
+ $(this).change(update).keyup(update).keydown(update);
+
+ update.apply(this);
+
+ });
+
+ return this;
+
+ }
+
+})(jQuery);
@@ -0,0 +1,46 @@
+// jQuery.ellipsify
+// Rico Sta. Cruz 2011
+// Based on http://snipplr.com/view/46615
+//
+// Usage:
+// $("article h2").livequery(function() { $(this).ellipsify(); });
+//
+jQuery.fn.ellipsify = function() {
+ var $this = $(this);
+
+ if ($this.length > 1) {
+ return $this.each(function() { $(this).ellipsify(); });
+ }
+
+ var $clone = $this.clone();
+ var text = $this.html();
+ var shortened = false;
+ var offset = 2;
+
+ $clone.css({
+ display: 'none',
+ position: 'absolute',
+ overflow: 'visible',
+ width: $this.width(),
+ height: 'auto'});
+
+ $this.after($clone);
+
+ function tooBig() {
+ return $clone.outerHeight() > ($this.outerHeight() + offset);
+ }
+
+ while (text.length > 0 && tooBig()) {
+ shortened = true;
+ text = text.substr(0, text.lastIndexOf(' '));
+ $clone.html(text + '&hellip;');
+ }
+
+ // Tooltip
+ if ((shortened) && (!$this.attr('title'))) {
+ $this.attr('title', $this.text());
+ }
+
+ $this.html($clone.html());
+ $clone.remove();
+};
@@ -0,0 +1,101 @@
+// Usage:
+//
+// $.growl("Message");
+// $.growl({message: "Message"});
+// $.growl({message: "Message", sticky: true, timeout: 10000, id: 'thnth', class: 'error'}); // Not impl yet
+// $.growl.kill(); // Hide all
+//
+// Todo:
+// - coallescing
+//
+;(function($) {
+ $.growl = function(message) {
+ return $.growl.showMessage(message);
+ };
+
+ $.extend($.growl, {
+ // Templates
+ templates: {
+ item: "<div class='item'><div class='text'></div><a class='close'>&times;</a></div>"
+ },
+
+ // Options
+ id: "growl",
+ timeout: 5000,
+ id_prefix: "growl-notification-",
+
+ // State
+ $notifs: null,
+
+ showMessage: function(args) {
+ // Alias for $.growl('message').
+ if (typeof args == "string")
+ { return $.growl.showMessage({ 'message': args }); }
+
+ if ((typeof args != "object") ||
+ (args.message === undefined))
+ { return; }
+
+ var message = args.message;
+ var self = this;
+
+ if (!self.$notifs) {
+ self.$notifs = $("<div id='" + self.id + "'><div class='items'></div></div>");
+ $(document.body).append(self.$notifs);
+ }
+
+ // Construct the item.
+ var item = $(self.templates.item);
+ if (args.classname) { item.addClass(args.classname); }
+ item.find('.text').html(message);
+ item.find('.close').click(function() {
+ self._killItem(item);
+ });
+
+ // Either replace the old item (coallesce), or add the new item.
+ var id = args.id ? (this.id_prefix + args.id) : null;
+ var $old = args.id ? $("#"+id) : [];
+ if ($old.length > 0) {
+ // Replace the old item with the new one.
+ $old.html(item.html());
+ $old.attr('class', item.attr('class'));
+ item = $old;
+ }
+ else {
+ // Add the new item.
+ item.attr('id', id);
+ self.$notifs.find('.items').prepend(item);
+ }
+
+ self.addTimer(item, args);
+ },
+
+ addTimer: function($item, args) {
+ var self = this;
+
+ // Clear a timeout, if there is one.
+ var timer = $item.data('growl-timer');
+ if (timer) { window.clearTimeout(timer); }
+
+ // Add a timeout.
+ if (args.sticky !== true) {
+ timer = window.setTimeout(function() {
+ self._killItem($item);
+ }, args.timeout || self.timeout);
+ $item.data('growl-timer', timer);
+ }
+ },
+
+ kill: function() {
+ var self = this;
+ self.$notifs.find('.items > *').each(function() {
+ self._killItem($(this));
+ });
+ },
+
+ _killItem: function($item) {
+ $item = $($item);
+ $item.slideUp(function() { $(this).remove(); });
+ }
+ });
+})(jQuery);
@@ -0,0 +1,107 @@
+$ = jQuery
+
+# Returns the scroll position thing.
+#
+# ## Example
+# $("#two").scrollPosition() // 0..1 or undefined
+#
+$.fn.scrollPosition = ->
+ top = $(this).position().top
+ min = Math.max(top - $(window).height(), 0)
+ max = Math.min($('body').height() - $(window).height(), top + $(this).outerHeight())
+
+ val = ($(window).scrollTop() - min) / (max - min)
+
+ val
+
+# Monitors scrolling.
+#
+# You can pass an attr delta to make it trigger before it's actually shown.
+#
+# ## Example
+# $("#two").monitorScroll({ callback: function(e, pos) { pos; } });
+# // `pos` will be 0..1 -- what scrollPosition will give
+#
+$.fn.monitorScroll = (options) ->
+ callback = options.callback || (->)
+ delta = options.delta || 0
+
+ $(this).each ->
+ $this = $(this)
+
+ if options.callback
+ $this.bind 'peek', callback
+
+ $(window).scroll ->
+ pos = $this.scrollPosition()
+ $this.trigger 'peek', pos if pos > (0.0-delta) && pos < (1.0+delta)
+
+ # Initialize it
+ callback.apply $(this), [null, ($this.scrollPosition() || 0)]
+
+$.fn.scrope = (elements) ->
+
+ for selector of elements
+ options = elements[selector]
+
+ $(this).monitorScroll delta: options.top, callback: (e, pos) ->
+ if options.fromTop
+ p = -pos
+ else
+ p = (-pos + 0.5) * 2 # -1..1
+
+ $el = if selector == '&'
+ $(this)
+ else
+ $(this).find(selector)
+
+ $el.setOffset top: options.top * p, bgTop: (options.bgTop||0) * p
+
+# Adds two CSS values together.
+#
+# ## Example
+# add('5px', 10) //=> 15px
+#
+add = (a, b) ->
+ return a if parseFloat(b) == 0.0
+ unit = "#{a}".match(/[^0-9]*$/)
+ (parseFloat(a) + parseFloat(b)) + unit
+
+$.fn.setOffset = (options) ->
+ console.log $(this).attr('id'), options if $(this).attr('id') == 'two'
+ $(this).each ->
+ $this = $(this)
+
+ # Ensure that the element has positioning.
+ if !$this.css('position') == 'static'
+ $this.css position: 'relative', top: 0, left: 0
+
+ fadeIn = false
+
+ # Store the old
+ if !$this.data('original_offset')
+ bp = $this.css('backgroundPosition').split(' ')
+
+ $this.data 'original_offset',
+ top: $this.css('top'),
+ left: $this.css('left')
+ bgTop: bp[0]
+ bgLeft: bp[1]
+
+ fadeIn = true
+
+ origin = $this.data('original_offset')
+
+ if options.top or options.left
+ $this.css
+ top: add origin.top, (options.top || 0)
+ left: add origin.left, (options.left || 0)
+
+ if options.bgTop or options.bgLeft
+ top = add origin.bgTop, (options.bgTop || 0)
+ left = add origin.bgLeft, (options.bgLeft || 0)
+
+ $this.css
+ backgroundPosition: "#{left} #{top}"
+
+ $this.fadeIn() if fadeIn
Oops, something went wrong.

0 comments on commit b981fb2

Please sign in to comment.