[su_icon_text icon=”icon: info-circle” icon_color=”#5b63df” class=”saki-info-box”]Code in this post can be obsolete, however, principles and theory may still apply.[/su_icon_text] On the way of development of my project I came to need to have set methods for javascript Array object especially intersection. Googling has shown up some but they were not LGPL, the licence what I need, so I’ve written them.
The following override contains:
- Ext message box input type configuration
- Conditional Ext.overrideIf
- New Array methods:
- copy – one dimensional copy method
- indexOf – index of a value within the array. New browsers may already implement this method, therefore conditional override.
- lastIndexOf – last index of a value within the array. May be already implemented by browser too.
- intersect – returns intersection of arrays, for example:
[1,3,5,7].intersect([2,3,8,7,5])returns[3,5,7] - union – returns union of arrays, for example:
[1,3,5,7].union([2,3,8,7,5])returns[1,2,3,5,7,8] - unique – removes duplicate values from the array and returns result as new array
- Ext.ux.clone – deep object or array cloning function
Overrides code:
// vim: ts=4:sw=4:nu:fdc=4:nospell
/**
* Ext.ux.Overrides
*
* This files contains various fixes and/or overridesds:
*
* 1. inputType selection for Ext.Msg.show
* 2. Ext.overrideIf conditional override
* 3. Array methods: indexOf, lastIndexOf, copy, unique, intersect, union
* 4. Cloning function
*
* @author Ing. Jozef Sakalos
* @version $Id: Ext.ux.Overrides.js 158 2008-04-10 00:03:18Z jozo $
* @date 13. March 2008
*
* @license Ext.ux.Overrides is licensed under the terms of
* the Open Source LGPL 3.0 license. Commercial use is permitted
* to the extent that the code/component(s) do NOT become part of
* another Open Source or Commercially licensed development library or
* toolkit without explicit permission.
*
* License details: http://www.gnu.org/licenses/lgpl.html
*/
/*global Ext */
// {{{
// Ext.Msg input type and firefox cursor bug fix
Ext.Msg.show = Ext.Msg.show.createInterceptor(function(config) {
// get inputType
var inputType = config.inputType || 'text';
// get dialog content element
var content = Ext.Msg.getDialog().body.child('div.ext-mb-content');
// set inputType
var input = content.child('input');
if(input) {
input.set({type:inputType});
}
// this seems to be already handled in Ext
// fix firefox cursor bug
// var overflow = config.cursorFix ? 'auto' : 'hidden';
// if(Ext.isGecko) {
// content.applyStyles({overflow:overflow});
// }
}, Ext.Msg);
// }}}
// {{{
// conditional override
/**
* Same as Ext.override but overrides only if
* method doesn not exist in target class
*/
Ext.overrideIf = function(origclass, overrides) {
if(overrides) {
var p = origclass.prototype;
for(var method in overrides) {
if(!p[method]) {
p[method] = overrides[method];
}
}
}
};
// }}}
// {{{
// methods for Array object
Ext.overrideIf(Array, {
// {{{
/**
* One dimensional copy
* @return {Array} New array that is copy of this
*/
copy:function() {
var a = [];
for(var i = 0, l = this.length; i < l; i++) {
a.push(this[i]);
}
return a;
} // eo function copy
// }}}
// {{{
/**
* @return {Integer} index of v or -1 if not found
* @param {Mixed} v Value to find indexOf
* @param {Integer} b Starting index
*/
,indexOf:function(v, b) {
for(var i = +b || 0, l = this.length; i < l; i++) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eo function indexOf
// }}}
// {{{
/**
* @return {Array} intersection of this and passed arguments
*/
,intersect:function() {
if(!arguments.length) {
return [];
}
var a1 = this, a2, a;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a = [];
a2 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
if(-1 b) {
if(this[i] === v) {
return i;
}
}
return -1;
} // eof function lastIndexOf
// }}}
// {{{
/**
* @return {Array} New array that is union of this and passed arguments
*/
,union:function() {
var a = this.copy(), a1;
for(var k = 0, ac = arguments.length; k < ac; k++) {
a1 = arguments[k] || [];
for(var i = 0, l = a1.length; i < l; i++) {
a.push(a1[i]);
}
}
return a.unique();
} // eo function union
// }}}
// {{{
/**
* Removes duplicates from array
* @return {Array} new array with duplicates removed
*/
,unique:function() {
var a = [], i, l = this.length;
for(i = 0; i < l; i++) {
if(a.indexOf(this[i]) < 0) {
a.push(this[i]);
}
}
return a;
} // eo function unique
// }}}
});
// }}}
// {{{
// object and array cloning function
/**
* Clone Function
* @return {Object/Array} Deep clone of an object or an array
*/
Ext.ux.clone = function(o) {
if(!o || 'object' !== typeof o) {
return o;
}
var c = 'function' === typeof o.pop ? [] : {};
var p, v;
for(p in o) {
if(o.hasOwnProperty(p)) {
v = o[p];
if(v && 'object' === typeof v) {
c[p] = Ext.ux.clone(v);
}
else {
c[p] = v;
}
}
}
return c;
}; // eo function clone
// }}}
// eof
I'm a well seasoned developer, consultant and educator of web applications based mainly on Sencha libraries, PHP, MySQL and Node.js. Besides (Apple) computers, I love photography and mountain biking.
Follow me:
Latest posts by saki (see all)
- Ext, Angular, React, and Vue - 27. June 2019
- The Site Resurgence - 11. February 2018
- Configuring ViewModel Hierarchy - 19. June 2015