object.keys workaround

This commit is contained in:
vitalets 2012-11-27 00:37:15 +04:00
parent 612ecc5e55
commit 396775bf8c
2 changed files with 107 additions and 91 deletions
src

@ -2,104 +2,120 @@
* EditableForm utilites * EditableForm utilites
*/ */
(function ($) { (function ($) {
$.extend($.fn.editableform, { $.fn.editableform.utils = {
utils: { /**
/** * classic JS inheritance function
* classic JS inheritance function */
*/ inherit: function (Child, Parent) {
inherit: function (Child, Parent) { var F = function() { };
var F = function() { }; F.prototype = Parent.prototype;
F.prototype = Parent.prototype; Child.prototype = new F();
Child.prototype = new F(); Child.prototype.constructor = Child;
Child.prototype.constructor = Child; Child.superclass = Parent.prototype;
Child.superclass = Parent.prototype; },
},
/** /**
* set caret position in input * set caret position in input
* see http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area * see http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area
*/ */
setCursorPosition: function(elem, pos) { setCursorPosition: function(elem, pos) {
if (elem.setSelectionRange) { if (elem.setSelectionRange) {
elem.setSelectionRange(pos, pos); elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) { } else if (elem.createTextRange) {
var range = elem.createTextRange(); var range = elem.createTextRange();
range.collapse(true); range.collapse(true);
range.moveEnd('character', pos); range.moveEnd('character', pos);
range.moveStart('character', pos); range.moveStart('character', pos);
range.select(); range.select();
} }
}, },
/** /**
* function to parse JSON in *single* quotes. (jquery automatically parse only double quotes) * function to parse JSON in *single* quotes. (jquery automatically parse only double quotes)
* That allows such code as: <a data-source="{'a': 'b', 'c': 'd'}"> * That allows such code as: <a data-source="{'a': 'b', 'c': 'd'}">
* safe = true --> means no exception will be thrown * safe = true --> means no exception will be thrown
* for details see http://stackoverflow.com/questions/7410348/how-to-set-json-format-to-html5-data-attributes-in-the-jquery * for details see http://stackoverflow.com/questions/7410348/how-to-set-json-format-to-html5-data-attributes-in-the-jquery
*/ */
tryParseJson: function(s, safe) { tryParseJson: function(s, safe) {
if (typeof s === 'string' && s.length && s.match(/^[\{\[].*[\}\]]$/)) { if (typeof s === 'string' && s.length && s.match(/^[\{\[].*[\}\]]$/)) {
if (safe) { if (safe) {
try { try {
/*jslint evil: true*/
s = (new Function('return ' + s))();
/*jslint evil: false*/
} catch (e) {} finally {
return s;
}
} else {
/*jslint evil: true*/ /*jslint evil: true*/
s = (new Function('return ' + s))(); s = (new Function('return ' + s))();
/*jslint evil: false*/ /*jslint evil: false*/
} catch (e) {} finally {
return s;
} }
} else {
/*jslint evil: true*/
s = (new Function('return ' + s))();
/*jslint evil: false*/
} }
return s; }
}, return s;
},
/** /**
* slice object by specified keys * slice object by specified keys
*/ */
sliceObj: function(obj, keys, caseSensitive /* default: false */) { sliceObj: function(obj, keys, caseSensitive /* default: false */) {
var key, keyLower, newObj = {}; var key, keyLower, newObj = {};
if (!$.isArray(keys) || !keys.length) {
return newObj;
}
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
if(caseSensitive === true) {
continue;
}
//when getting data-* attributes via $.data() it's converted to lowercase.
//details: http://stackoverflow.com/questions/7602565/using-data-attributes-with-jquery
//workaround is code below.
keyLower = key.toLowerCase();
if (obj.hasOwnProperty(keyLower)) {
newObj[key] = obj[keyLower];
}
}
if (!$.isArray(keys) || !keys.length) {
return newObj; return newObj;
}, }
/** for (var i = 0; i < keys.length; i++) {
* exclude complex objects from $.data() before pass to config key = keys[i];
*/ if (obj.hasOwnProperty(key)) {
getConfigData: function($element) { newObj[key] = obj[key];
var data = {}; }
$.each($element.data(), function(k, v) {
if(typeof v !== 'object' || (v && typeof v === 'object' && v.constructor === Object)) { if(caseSensitive === true) {
data[k] = v; continue;
} }
});
return data; //when getting data-* attributes via $.data() it's converted to lowercase.
} //details: http://stackoverflow.com/questions/7602565/using-data-attributes-with-jquery
} //workaround is code below.
}); keyLower = key.toLowerCase();
if (obj.hasOwnProperty(keyLower)) {
newObj[key] = obj[keyLower];
}
}
return newObj;
},
/**
* exclude complex objects from $.data() before pass to config
*/
getConfigData: function($element) {
var data = {};
$.each($element.data(), function(k, v) {
if(typeof v !== 'object' || (v && typeof v === 'object' && v.constructor === Object)) {
data[k] = v;
}
});
return data;
},
objectKeys: function(o) {
if (Object.keys) {
return Object.keys(o);
} else {
if (o !== Object(o)) {
throw new TypeError('Object.keys called on a non-object');
}
var k=[], p;
for (p in o) {
if (Object.prototype.hasOwnProperty.call(o,p)) {
k.push(p);
}
}
return k;
}
}
};
}(window.jQuery)); }(window.jQuery));

@ -1,5 +1,5 @@
/** /**
Select (dropdown) input Select (dropdown)
@class select @class select
@extends list @extends list