Learn from Saki

Knowledge is power

This content is for registered users only. Please login.
Hello! To access your account, please Log in. Not a member? Sign up
  • Videos
  • Blog
  • Examples
  • Services
  • Add-ons
  • About

Array Overrides

April 17, 2008 by saki 2 Comments

Code in this post can be obsolete, however, principles and theory may still apply.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// 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
  • Author
  • Recent Posts
Follow me:
saki
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 - June 27, 2019
  • The Site Resurgence - February 11, 2018
  • Configuring ViewModel Hierarchy - June 19, 2015

Filed Under: Know-how Tagged With: extjs, javascript

Comments

  1. Lucian says

    April 18, 2008 at 1:19 pm

    Do you also have a String overrider?…

    Log in to Reply
  2. jsakalos says

    April 18, 2008 at 6:25 pm

    Not yet, I didn’t need any so far.

    Log in to Reply

We will be happy to hear back from you Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • Addons (2)
  • Architecture (14)
  • Examples (2)
  • ExtJS (26)
  • Howtos (16)
  • Javascript (1)
  • Know-how (32)
  • Linux (1)
  • Mac OS X (2)
  • SASS/CSS (2)
  • Snippets (9)
  • Theory (14)
  • Touch (6)
  • Tutorials (5)
  • What is a … (9)

Tag cloud

abstract class accordion application button class cluster column component config css definition deprecated design education event example extension extjs factory function form grid html initComponent items javascript Know-how knowledge layout Linux listener mysql old panel pattern php plugin render snippet sql sqlite state table touch tree viewpoint

Membership

Become a Member
Affiliate Program

Support

FAQ
Contact

Legal

Terms and Conditions
Licensing
Privacy Policy

Copyright © 2021 · Dynamik-Gen on Genesis Framework · WordPress · Log in