0

我正在尝试向自定义调色板添加一个按钮,以调用位于使用小部件的工作区中的函数“uiFileInputDlg”。upbtn 出现在托盘上,但它没有触发在 postcreate 中连接的 DoUpload 函数,然后调用“uiFileInputDlg”。在 Firefox 中完美运行。我们是用户 dojo 1.7.2

------------模板-------------

<div class="dijitInline dijitColorPalette">
    <div class="dijitColorPaletteInner" data-dojo-attach-point="divNode" role="grid" tabIndex="${tabIndex}">
    </div>    
    <button type="button" id="upbtn"
        data-dojo-type="dijit.form.Button"
        data-dojo-props="id:'upbtn'"
        data-dojo-attach-point="btnUpNode">
        上传新图片
    </按钮>
</div>

-------------------------小工具------------------------ ---

//dojo.provide("meemli.UploadPalette"); 定义(['dojo/_base/declare', 'dijit/_WidgetBase', 'dijit/_TemplatedMixin', 'dojo/i18n!dijit/nls/common', 'dojo/text!./templates/UploadPalette.html', ' dijit/_WidgetsInTemplateMixin', 'dojo/_base/lang' ], function(declare, w, t, i18n, template, witm, lang){ console.log('meemli.UploadPalette: 需要 dijit/nls/common.js INSTEAD OF dojo/nls/common' + i18n.invalidMessage);

    return declare("meemli.UploadPalette",
    [dijit._Widget, dijit._Templated],
    {
    // summary: A keyboard accessible color-picking widget
    // description:
    //  Grid showing various colors, so the user can pick a certain color
    //  Can be used standalone, or as a popup.
    //
    // example:
    // |    <div dojoType="dijit.ColorPalette"></div>
    //
    // example:
    // |    var picker = new dijit.ColorPalette({ },srcNode);
    // |    picker.startup();
    //
    // defaultTimeout: Number
    //      number of milliseconds before a held key or button becomes typematic
    defaultTimeout: 500,

    // timeoutChangeRate: Number
    //      fraction of time used to change the typematic timer between events
    //      1.0 means that each typematic event fires at defaultTimeout intervals
    //      < 1.0 means that each typematic event fires at an increasing faster rate
    timeoutChangeRate: 0.90,

    // palette: String
    //      Size of grid, either "7x10" or "3x4".
    palette: "3x3",

    //_value: String
    //      The value of the selected color.
    value: null,

    //_currentFocus: Integer
    //      Index of the currently focused color.
    _currentFocus: 0,

    // _xDim: Integer
    //      This is the number of colors horizontally across.
    _xDim: null,

    // _yDim: Integer
    ///     This is the number of colors vertically down.
    _yDim: null,

    // _palettes: Map
    //      This represents the value of the colors.
    //      The first level is a hashmap of the different arrays available
    //      The next two dimensions represent the columns and rows of colors.
    _palettes: {
        "3x3": [],
        "3x2": ["/images/icons/1.png", "/images/icons/2.png", "/images/icons/3.png","/images/icons/4.png", "/images/icons/5.png", "/images/icons/6.png"]
    },

    // _imagePaths: Map
    //      This is stores the path to the palette images
//  _imagePaths: {
//      "3x3": dojo.moduleUrl("dijit", "templates/icons3x3.png")
//  },

    // _paletteCoords: Map
    //      This is a map that is used to calculate the coordinates of the
    //      images that make up the palette.
    _paletteCoords: {
        "leftOffset": 3, "topOffset": 3,
        "cWidth": 50, "cHeight": 50

    },

    // templatePath: String
    //      Path to the template of this widget.
//  templateString: dojo.cache("meemli", "templates/UploadPalette.html"),
    templateString: template,

    // _paletteDims: Object
    //      Size of the supported palettes for alignment purposes.
    _paletteDims: {
        "3x3": {"width": "156px", "height": "156px"}, // 48*3 + 3px left/top border + 3px right/bottom border...
        "3x2": {"width": "156px", "height": "109px"} // 48*3 + 3px left/top border + 3px right/bottom border...
    },

    // tabIndex: String
    //      Widget tabindex.
    maxCols: 3,
    tabIndex: "0",
    _curIndex: 0,

    DoUpload: function(){
        alert('hello');
        uiFileInputDlg(); // function out in the workspace
    },

    _addImage: function(url) {
        row = Math.floor(this._curIndex / this.maxCols);
        col = this._curIndex - (row * this.maxCols);
        this._curIndex++;
        this._yDim = Math.floor(this._curIndex / this.maxCols);
        this._xDim = this._curIndex - (row * this.maxCols);

        var imgNode = dojo.doc.createElement("img");
        imgNode.src = url;
        //imgNode.style.height = imgNode.style.width =  "48px";

        var cellNode = dojo.doc.createElement("span");
        cellNode.appendChild(imgNode);
        cellNode.connectionRefs = new Array();
        dojo.forEach(["Dijitclick", "MouseEnter", "Focus", "Blur"], function(handler) {
            cellNode.connectionRefs.push(this.connect(cellNode, "on" + handler.toLowerCase(), "_onCell" + handler));
        }, this);

        this.divNode.appendChild(cellNode);

        var cellStyle = cellNode.style;
        cellStyle.top = this._paletteCoords.topOffset + (row * this._paletteCoords.cHeight) + "px";
        cellStyle.left = this._paletteCoords.leftOffset + (col * this._paletteCoords.cWidth) + "px";
        cellStyle.height = this._paletteCoords.cHeight + "px";
        cellStyle.width = this._paletteCoords.cWidth + "px";
        // console.debug( "tlhw: " + cellStyle.top + ", " + cellStyle.left + ", " + cellStyle.height + ", " + cellStyle.width );

        // adjust size when the bits come...
//      this.xh = this.xw = "32px";
//console.log('this.xh => ' + this.xh);     
        dojo.connect( imgNode,"onload", this, function() {
//console.log('IN: CONNECT...this.xh => ' + this.xh);       
            this.xh = imgNode.height;
            this.xw = imgNode.width;
            this.xh = (this.xh==0) ? this.xh="32px" : (this.xh + "");
            this.xw = (this.xw==0) ? this.xw="32px" : (this.xw + "");
//          var h = parseInt( this.xh );
//          var w = parseInt( this.xw );
            var hArr = this.xh.split('p');
            var wArr = this.xw.split('p');
            var h =hArr[0];
            var w = wArr[0];

            var THUMBNAIL_MAX_WIDTH = 50;
            var THUMBNAIL_MAX_HEIGHT = 50;

            var hLim = Math.min(THUMBNAIL_MAX_HEIGHT, this._paletteCoords.cHeight);
            var wLim = Math.min(THUMBNAIL_MAX_WIDTH, this._paletteCoords.cWidth);

            var scale = 1.0;

            if( h > hLim || w > wLim ) {
                if( h / w < 1.0 ) { // width is bigger than height
                    scale = wLim / w;
                }
                else {
                    scale = hLim / h;
                }
            }

            imgNode.style.height = (scale * h) + "px";
            imgNode.style.width = (scale * w) + "px";
            console.debug( imgNode.src + ' loaded.'
                + "old: h " + h + ", w " + w + ", scale " + scale
                + ". new: h " + imgNode.style.height + ", w " + imgNode.style.width );
        } );
        if(dojo.isIE){
            // hack to force event firing in IE
            // image quirks doc'd in dojox/image/Lightbox.js :: show: function.
//          imgNode.src = imgNode.src;
        }

        dojo.attr(cellNode, "tabindex", "-1");
        dojo.addClass(cellNode, "imagePaletteCell");
        dijit.setWaiRole(cellNode, "gridcell");
        cellNode.index = this._cellNodes.length;
        this._cellNodes.push(cellNode);

    },

    _clearImages: function() {
        for(var i = 0; i < this._cellNodes.length; i++) {
            this._cellNodes[i].parentNode.removeChild(this._cellNodes[i]);
        }
        this._currentFocus = 0;
        this._curIndex = 0;
        this._yDim = 0;
        this._xDim = 0;
        this._cellNodes = [];
    },

    setImages: function(imageList) {
        this._clearImages();
        for(var i = 0; i < imageList.length; i++) {
            this._addImage(imageList[i]);
        }
    },

    postCreate: function(){
        // A name has to be given to the colorMap, this needs to be unique per Palette.
        dojo.mixin(this.divNode.style, this._paletteDims[this.palette]);
//      this.imageNode.setAttribute("src", this._imagePaths[this.palette]);
        this.domNode.style.position = "relative";
        this._cellNodes = [];   
        this.setImages(this._palettes[this.palette]);
        this.connect(this.divNode, "onfocus", "_onDivNodeFocus");
        this.connect(this.btnUpNode, "onclick", "DoUpload");

        // Now set all events
        // The palette itself is navigated to with the tab key on the keyboard
        // Keyboard navigation within the Palette is with the arrow keys
        // Spacebar selects the color.
        // For the up key the index is changed by negative the x dimension.     

        var keyIncrementMap = {
            UP_ARROW: -this._xDim,
            // The down key the index is increase by the x dimension.   
            DOWN_ARROW: this._xDim,
            // Right and left move the index by 1.
            RIGHT_ARROW: 1,
            LEFT_ARROW: -1
        };
        for(var key in keyIncrementMap){
            this._connects.push(dijit.typematic.addKeyListener(this.domNode,
                {keyCode:dojo.keys[key], ctrlKey:false, altKey:false, shiftKey:false},
                this,
                function(){
                    var increment = keyIncrementMap[key];
                    return function(count){ this._navigateByKey(increment, count); };
                }(),
                this.timeoutChangeRate, this.defaultTimeout));
        }
    },

    focus: function(){
        // summary:
        //      Focus this ColorPalette.  Puts focus on the first swatch.
        this._focusFirst();
    },

    onChange: function(url, hsz, wsz){
        // summary:
        //      Callback when a image is selected.
        // url, hsz, wsz, strings
        //      height and width string .
//      console.debug("img selected: "+url);
    },

    _focusFirst: function(){
        this._currentFocus = 0;
        var cellNode = this._cellNodes[this._currentFocus];
        window.setTimeout(function(){dijit.focus(cellNode);}, 0);
    },

    _onDivNodeFocus: function(evt){
        // focus bubbles on Firefox 2, so just make sure that focus has really
        // gone to the container
        if(evt.target === this.divNode){
            this._focusFirst();
        }
    },

    _onFocus: function(){
        // while focus is on the palette, set its tabindex to -1 so that on a
        // shift-tab from a cell, the container is not in the tab order
        dojo.attr(this.divNode, "tabindex", "-1");
    },

    _onBlur: function(){
        this._removeCellHighlight(this._currentFocus);
        // when focus leaves the palette, restore its tabindex, since it was
        // modified by _onFocus().
        dojo.attr(this.divNode, "tabindex", this.tabIndex);
    },

    _onCellDijitclick: function(/*Event*/ evt){
        // summary:
        //      Handler for click, enter key & space key. Selects the color.
        // evt:
        //      The event.
        var target = evt.currentTarget;
        if (this._currentFocus != target.index){
            this._currentFocus = target.index;
            window.setTimeout(function(){dijit.focus(target);}, 0);
        }
        this._selectColor(target);
        dojo.stopEvent(evt);
    },

    _onCellMouseEnter: function(/*Event*/ evt){
        // summary:
        //      Handler for onMouseOver. Put focus on the color under the mouse.
        // evt:
        //      The mouse event.
        var target = evt.currentTarget;
        window.setTimeout(function(){dijit.focus(target);}, 0);
    },

    _onCellFocus: function(/*Event*/ evt){
        // summary:
        //      Handler for onFocus. Removes highlight of
        //      the color that just lost focus, and highlights
        //      the new color.
        // evt:
        //      The focus event.
        this._removeCellHighlight(this._currentFocus);
        this._currentFocus = evt.currentTarget.index;
        dojo.addClass(evt.currentTarget, "imagePaletteCellHighlight");
    },

    _onCellBlur: function(/*Event*/ evt){
        // summary:
        //      needed for Firefox 2 on Mac OS X
        this._removeCellHighlight(this._currentFocus);
    },

    _removeCellHighlight: function(index){
        dojo.removeClass(this._cellNodes[index], "imagePaletteCellHighlight");
    },

    _selectColor: function(selectNode){ 
        // summary:
        //      This selects a color. It triggers the onChange event
        // area:
        //      The area node that covers the color being selected.
        var img = selectNode.getElementsByTagName("img")[0];
        this.onChange(this.value = img.src, this.xh, this.xw);
    },

    _navigateByKey: function(increment, typeCount){
        // summary:
        //      This is the callback for typematic.
        //      It changes the focus and the highlighed color.
        // increment:
        //      How much the key is navigated.
        // typeCount:
        //      How many times typematic has fired.

        // typecount == -1 means the key is released.
        if(typeCount == -1){ return; }

        var newFocusIndex = this._currentFocus + increment;
        if(newFocusIndex < this._cellNodes.length && newFocusIndex > -1)
        {
            var focusNode = this._cellNodes[newFocusIndex];
            focusNode.focus();
        }
    }
});

});

4

1 回答 1

1

更新

this.connect(this.btnUpNode, "onclick", "DoUpload");

成为

this.connect(this.btnUpNode, "onClick", "DoUpload");

onclick是一个dom事件,onClick是一个dijit事件。由于您使用的是 dijit 按钮,因此您希望使用后者。

于 2012-08-10T20:19:27.987 回答