Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* The Codezzz

  • Loading branch information...
commit 2cd83b1c9391b02a0e908abab5d90881424b7761 0 parents
@cpojer authored
Showing with 168 additions and 0 deletions.
  1. +24 −0 LICENSE
  2. +127 −0 Source/Form.Placeholder.js
  3. +17 −0 package.yml
24 LICENSE
@@ -0,0 +1,24 @@
+MooTools Custom Event - MIT License
+
+Copyright (c) 2010 Christoph Pojer
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
127 Source/Form.Placeholder.js
@@ -0,0 +1,127 @@
+/*
+---
+
+name: Form.Placeholder
+
+description: Provides a fallback for the placeholder property on input elements for older browsers.
+
+authors: Christoph Pojer (@cpojer)
+
+license: MIT-style license.
+
+requires: [Core/Class.Extras, Core/Element, Core/Element.Style, Class-Extras/Class.Binds, Class-Extras/Class.Singleton]
+
+provides: Form.Placeholder
+
+...
+*/
+
+(function(){
+
+if (!this.Form) this.Form = {};
+
+var supportsPlaceholder = ('placeholder' in document.createElement('input'));
+// Safari4 does not properly support placeholders. Forcing to use the fallback.
+if (Browser.safari && Browser.version <= 4) supportsPlaceholder = false;
+
+if (!('supportsPlaceholder' in this) && this.supportsPlaceholder !== false && supportsPlaceholder) return;
+
+var wrapSet = function(self, set){
+ return function(key, value){
+ if (key == 'value' && !value){
+ self.reset();
+ return this;
+ }
+
+ return set.apply(this, arguments);
+ };
+};
+
+var wrapGet = function(get, self){
+ return function(key){
+ if (key == 'value' && !self.hasValue())
+ return '';
+
+ return get.apply(this, arguments);
+ };
+};
+
+var Placeholder = this.Form.Placeholder = new Class({
+
+ Implements: [Class.Singleton, Class.Binds, Options],
+
+ options: {
+ defaultColor: '#777',
+ defaultValue: null
+ },
+
+ initialize: function(element, options){
+ this.setOptions(options);
+ element = this.element = document.id(element);
+
+ return this.check(element) || this.setup();
+ },
+
+ setup: function(){
+ var element = this.element;
+
+ this.defaultValue = this.options.defaultValue || element.get('placeholder') || element.value;
+ this.color = element.getStyle('color');
+
+ element.erase('placeholder');
+ element.set = wrapSet(element.set, this);
+ element.get = wrapGet(element.get, this);
+
+ this.attach();
+
+ if (!this.hasValue()) this.reset();
+ },
+
+ attach: function(){
+ this.element.addEvents({
+ focus: this.bound('focus'),
+ blur: this.bound('blur')
+ });
+
+ return this;
+ },
+
+ detach: function(){
+ this.element.removeEvents({
+ focus: this.bound('focus'),
+ blur: this.bound('blur')
+ });
+
+ return this;
+ },
+
+ clear: function(){
+ var element = this.element;
+ element.setStyle('color', this.color);
+ if (element.value == this.defaultValue) element.value = '';
+
+ return this;
+ },
+
+ reset: function(){
+ this.element.setStyle('color', this.options.defaultColor).value = this.defaultValue;
+
+ return this;
+ },
+
+ focus: function(){
+ this.clear();
+ },
+
+ blur: function(){
+ if (!this.element.value) this.reset();
+ },
+
+ hasValue: function(){
+ var value = this.element.value;
+ return (value && value != this.defaultValue);
+ }
+
+});
+
+})();
17 package.yml
@@ -0,0 +1,17 @@
+name: "Form-Placeholder"
+
+exports: "form-placeholder.js"
+
+web: "[cpojer.net](http://cpojer.net)"
+
+description: "Provides a fallback for the placeholder property on input elements for older browsers."
+
+license: "[MIT License](http://mootools.net/license.txt)"
+
+copyright: "&copy; [Christoph Pojer](http://cpojer.net/)"
+
+author: "cpojer"
+authors: "[Christoph Pojer](http://cpojer.net)"
+
+sources:
+ - "Source/Form.Placeholder.js"
Please sign in to comment.
Something went wrong with that request. Please try again.