MGrid.js 31.4 KB
Newer Older
R
roo00 已提交
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
MWF.xDesktop.requireApp("Template", "MDomItem", null, false);
var MGrid = new Class({
    Extends: MWF.widget.Common,
    Implements: [Options, Events],
    options: {
        style: "default",
        isNew: false,
        isEdited: false,
        showNotEmptyFlag : true,
        verifyType : "batch",	//batch一起校验,或alert弹出
        //batch,所有item的错误都校验,每个item所有错误一起校验,错误显示在Item的字段后或指定区域;
        //batchSingle, 所有item的错误都校验,每个item只校验一个错误,错误显示在Item的字段后或指定区域;
        //alert,验证时遇到一个不成功的Item用app.notice()弹出消息后中断,
        //single,验证时遇到一个不成功的Item在字段后面或指定区域显示错误信息后中断

        itemTemplate : null,

        objectId : "",
        hasSequence  : true,
        hasOperation : false,
        isCreateTh : true,
        containerIsTable : false,
        isCreateTrOnNull : true,
        minTrCount : 0,
        maxTrCount : 0,

        tableAttributes : null,
        thAttributes : null,
        tdAttributes : null,

        tableClass : "formTable",
        thClass : "formTableTitle",
        tdClass : "formTableValue",
        thAlign : "center",
        tdAlign : "left",
        sequenceClass : "formTableSequence",
        addActionTdClass : "formTableAddTd",
        removeActionTdClass : "formTableRemoveTd",
        lp : {
            remove : "",
            add : ""
        }

    },
    initialize: function (container, data, options, app, css) {

        this.setOptions(options);

        this.path = "/x_component_Template/$MGrid/";
        this.cssPath = "/x_component_Template/$MGrid/" + this.options.style + "/css.wcss";
        this._loadCss();
        if (css) {
            this.css = Object.merge(this.css, css)
        }

        this.app = app;
        this.container = $(container);

        this.data = data;

        this.isSourceDataEmpty = false;
        if (!this.data || this.data == "") {
            this.isSourceDataEmpty = true;
            this.data = [{}];
        }

        this.itemTemplate = this.options.itemTemplate;
        this.items = null;

        this.th = null;
        this.trIndex = 0;

        this.trList = [];
        this.trObjs = null;
        this.trObjs_removed = null;
        this.trObjs_new = null;

        this.thTemplate = null;	//属性  lable button_add
        this.trTemplate = null; //属性 item sequence button_remove lable

        this.valSeparator = /,|;|\^\^|\|/g; //如果是多值对象,作为用户选择的多个值的分隔符
    },
    load: function () {
        this.fireEvent("queryLoad");


        //如果itemTemplate没有name赋值Key
        for (var it in this.itemTemplate) {
            if (!this.itemTemplate[it]["name"]) {
                this.itemTemplate[it]["name"] = it;
            }
            this.itemTemplate[it]["key"] = it;
        }

        //如果itemTemplate没有name和Key不一致,那么根据name赋值itemTemplate
        var json = {};
        for (var it in this.itemTemplate) {
            if (it != this.itemTemplate[it]["name"]) {
                json[this.itemTemplate[it]["name"]] = this.itemTemplate[it]
            }
        }
        for (var it in json) {
            this.itemTemplate[it] = json[it];
        }
        this.createTable( this.itemTemplate );
        this.createHead( this.itemTemplate );
        this.trObjs = {};
        this.trObjs_removed = {};
        this.trObjs_new = {};
        this.trList = [];
        (this.options.isEdited || this.options.isNew) ? this.loadEdit() : this.loadRead();
        this.fireEvent("postLoad", [this]);

    },
    setTrTemplate: function( template ){
        if( typeOf( template ) == "string"){
            this.trTemplate = this.string2DOM( template )[0];
        }else{
            this.trTemplate = template;
        }
    },
    setThTemplate : function( template ){
        if( typeOf( template ) == "string"){
            this.thTemplate = this.string2DOM( template )[0]
        }else{
            this.thTemplate = template;
        }
    },
    loadEdit:function(){
        if( !this.isSourceDataEmpty ){
            if( typeOf( this.data ) != "array" ){
                this.data = [ this.data ]
            }
            for( var i=0; i<this.data.length; i++ ){
                var d = this.data[i];
                var items = this.itemTemplate;
                for (var it in d ){
                    if ( items[ it ] ){
                        items[ it ].value  = d[it];
                    }
                }
                this.createTr( items, false, null, d );
            }
            for( var it in this.itemTemplate ){
                this.itemTemplate[it].value = "";
            }
        }else if( this.options.isCreateTrOnNull ){
            this.createTr( this.itemTemplate, true );
        }
    },
    loadRead : function(){
        if( !this.isSourceDataEmpty ){
            if( typeOf( this.data ) != "array" ){
                this.data = [ this.data ]
            }
            for( var i=0; i<this.data.length; i++ ){
                var d = this.data[i];
                var items = this.itemTemplate;
                for (var it in d ){
                    if ( items[ it ] ){
                        items[ it ].value  = d[it];
                    }
                }
                //this.createTr( items, false );
                this.createTr_read( items );
            }
        }else if( this.options.isCreateTrOnNull ){
            this.createTr_read( this.itemTemplate );
        }
    },
    createTable : function( itemData ){
        if( this.options.containerIsTable ){
            this.table = this.container;
            var labelContainers = this.table.getElements("[lable]");
            labelContainers.each(function( el ) {
                var obj = itemData[el.get("lable")];
                if (!obj)return;
                if(obj.text)el.set("text",obj.text);
            });
            this.fireEvent( "postCreateTable", [this] );
        }else{
            var styles = {};
            if( this.options.tableClass && this.css[this.options.tableClass] )styles = this.css[this.options.tableClass];
            var tableAttr =  this.options.tableAttributes || {};
            this.table = new Element( "table", {
                styles : styles
            }).inject(this.container);
            this.table.set( tableAttr );
            this.fireEvent( "postCreateTable", [this] );
        }
    },
    createTr_read : function( itemData ){
        var self = this;
        this.trIndex ++;
        if( this.trTemplate ){
            this.createTr_read_byTemplate( itemData );
        }else{
            this.createTr_read_noTemplate( itemData );
        }
    },
    createTr_read_byTemplate : function( itemData ){
        var tr = Object.clone( this.trTemplate );
        tr.set( "data-id", "_"+ this.trIndex );
        var labelContainers = tr.getElements("[lable]");
        var itemContainers = tr.getElements("[item]");
        var sequenceContainers = tr.getElements( "[sequence]" );

        labelContainers.each(function( el ) {
            var obj = itemData[el.get("lable")];
            if (!obj)return;
            if(obj.text)el.set("text", obj.text);
        });

        itemContainers.each(function( el ) {
            var obj = itemData[el.get("item")];
            if (!obj)return;
            var val = obj.value ? obj.value : "";
            if( obj.selectValue && obj.selectText ){
                var vals = this.replaceText( val.replace(/\n/g,"<br/>"), obj.selectValue, obj.selectText );
                val = vals.join(",");
            }else{
                val = val.replace(/\n/g,"<br/>").replace( this.valSeparator,"," )
            }
            el.set("html", val );
        }.bind(this));

        sequenceContainers.set("text", this.trIndex );
    },
    createTr_read_noTemplate : function( itemData ){
        var tr = new Element("tr" , { "data-id" : "_"+this.trIndex });
        if( this.options.hasSequence  ){
            var td = new Element("td", { align : "center", text : this.trIndex }).inject( tr );
            if( this.options.sequenceClass && this.css[this.options.sequenceClass] )td.setStyles(this.css[this.options.sequenceClass]);
        }

        var attr = {};
        if( this.options.tdAlign )attr.align = this.options.tdAlign;

        var idx = 1;
        for (var it in itemData ){
            var tdAttributes = {};
            if(this.options.tdAttributes && this.options.tdAttributes["_"+idx] ){
                tdAttributes = this.options.tdAttributes["_"+idx];
            }
            var obj = itemData[it];
            var val = obj.value || "";
            if( obj.selectValue && obj.selectText ){
                var vals = this.replaceText( val.replace(/\n/g,"<br/>"), obj.selectValue, obj.selectText );
                val = vals.join(",");
            }else{
                val = val.replace(/\n/g,"<br/>").replace( this.valSeparator,"," )
            }
            var td = new Element("td", tdAttributes).inject( tr );
            td.set( "text", val );
            if( this.options.tdAlign )td.set( "align" , this.options.tdAlign);
            if( this.options.tdClass && this.css[this.options.tdClass] )td.setStyles(this.css[this.options.tdClass]);
            idx ++;
        }
        tr.inject( this.table );

    },
    createHead : function( itemData ){
        if( !this.options.isCreateTh )return;
        if( this.thTemplate ){
            this.createHead_byTemplate( itemData );
        }else{
            this.createHead_noTemplate( itemData );
        }
    },
    createHead_byTemplate : function( itemData ){
        var th = this.tableHead = this.thTemplate;
        var labelContainers = th.getElements("[lable]");
        labelContainers.each(function(el) {
            var obj = itemData[el.get("lable")];
            if (!obj)return;
            if(obj.text)el.set("text", obj.text);
        });
        if( this.options.hasOperation && this.options.isEdited ){
            var add_button = th.getElement("[button_add]");
            this.createAddButton( add_button );
        }
        th.inject( this.table );
    },
    createHead_noTemplate : function( itemData ){
        var tr = this.tableHead = new Element("tr");

        var align = this.options.thAlign == "" ? {} : { align : this.options.thAlign  };
        var styles = (this.options.thClass && this.css[this.options.thClass]) ? this.css[this.options.thClass] : {};

        if( this.options.hasSequence  ){
            var th = new Element("th", { text : "序号" }).inject( tr );
            th.set( align );
            th.setStyles( styles );
        }

        var idx = 1;
        for (var it in this.itemTemplate){
            var thAttr = {};
            if(this.options.thAttributes && this.options.thAttributes["_"+idx] ){
                thAttr = this.options.thAttributes["_"+idx];
            }

            var th = new Element("th").inject(tr);
            if( this.options.showNotEmptyFlag && this.itemTemplate[it].notEmpty ){
                new Element( "span" , { styles : { color : "red", text : "*" } }).inject( th )
            }
            th.set( align );
            th.setStyles( styles );
            if(this.itemTemplate[it].text)th.set("text", this.itemTemplate[it].text );
            idx++;
        }
        if( this.options.hasOperation && this.options.isEdited ){
            var th = new Element("th", { align : "center", styles : { width : "24px", styles : {"text-align" : "center" }} } ).inject(tr);
            if( this.options.addActionTdClass && this.css[this.options.addActionTdClass] )th.setStyles(this.css[this.options.addActionTdClass]);
            this.createAddButton( th );
        }
        tr.inject( this.table );
        this.fireEvent("postCreateHead", [tr] );
    },
    createAddButton : function( container ){
        var button = new Element("div", { title : "添加" }).inject( container );
        if( this.options.lp.add )button.set("text",this.options.lp.add );
        if( this.css.actionAdd )button.setStyles( this.css.actionAdd );
        button.addEvent("click", function( e ){
            this.fireEvent("queryAddTr");
            this.createTr( this.itemTemplate, true );
            this.fireEvent("postAddTr");
        }.bind(this));
        if( this.css.actionAdd && this.css.actionAdd_over){
            button.addEvents({
                "mouseover" : function( e ){ this.node.setStyles( this.obj.css.actionAdd_over ) }.bind({ node : button, obj : this }),
                "mouseout" : function( e ){ this.node.setStyles( this.obj.css.actionAdd ) }.bind({ node : button, obj : this })
            })
        }
        return button;
    },
    addTrs : function( count ){
        if( 100 < count )count = 100;
        for( var i=0 ; i<count; i++ ){
            this.createTr( this.itemTemplate, true );
        }
    },
    appendTr : function( d, isNew, unid, sourceData ){
        var items = this.itemTemplate;
        for (var it in d ){
            if ( items[ it ] ){
                items[ it ].value  = d[it];
            }
        }
        this.createTr( items, isNew, unid, sourceData );
        for( var it in this.itemTemplate ){
            this.itemTemplate[it].value = "";
        }
    },
    getTrCounts : function(){
        return  this.trList.length;
    },
    createTr : function( itemData, isNew, unid, sourceData ){
        if( this.options.maxTrCount  ){
            if( this.getTrCounts() < this.options.maxTrCount ){
                this._createTr( itemData, isNew, unid, sourceData )
            }else{
NoSubject's avatar
NoSubject 已提交
363 364 365
                if( this.app && this.app.notice ){
                    this.app.notice("最多只能添加"+this.options.maxTrCount+"项目","error");
                }
R
roo00 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
            }
        }else{
            this._createTr( itemData, isNew, unid, sourceData )
        }
    },
    _createTr : function( itemData, isNew, unid, sourceData ){
        this.fireEvent("queryCreateTr", [this]);

        var d;
        if( isNew ){
            this.fireEvent("newData", [this, function( data ){
                d = data;
            }.bind(this) ]);
            if( d ){
                //itemData, isNew, unid, sourceData
                itemData = Object.clone( this.itemTemplate );
                for (var it in d ){
                    if ( itemData[ it ] ){
                        itemData[ it ].value  = d[it];
                    }
                }
                isNew = false;
                sourceData = d;
            }
        }

        this.trIndex ++;
        var trOptions = {
            objectId : unid ? unid : "_"+this.trIndex,
            isEdited : this.options.isEdited,
            id : "_"+this.trIndex,
            index : this.trIndex,
            indexText : (this.maxIndexText ? this.maxIndexText++ : this.trIndex),
            hasSequence : this.options.hasSequence,
            hasOperation : this.options.hasOperation,
            align : this.options.tdAlign,
            isNew : isNew,
            className : this.options.tdClass,
            tdAttributes : this.options.tdAttributes
        };
        var template = null;
        if( this.trTemplate ){
            template = this.trTemplate.clone();
        }
        var trObj = new MGridTr(this.table, trOptions, itemData, this, template, sourceData );
        trObj.load();

        this.trObjs[ trOptions.objectId ] = trObj;
        this.trList.push( trObj );

        if( isNew ){
            this.trObjs_new[ trOptions.objectId ] = trObj;
        }

        this.fireEvent("postCreateTr",[this, trObj]);
    },
    replaceTr : function( oldTrObjOr_Index, data, isNew, unid, sourceData ){
        if( typeof oldTrObjOr_Index == "string" ){ //如果传入的是  _index
            var oldTrObj = this.trObjs[ oldTrObjOr_Index ];
        }else{
            var oldTrobj = oldTrObjOr_Index;
        }
        var itemData = this.itemTemplate;
        for (var it in data ){
            if ( itemData[ it ] ){
                itemData[ it ].value  = data[it];
            }
        }
        var trIndex = oldTrObj.options.index;
        var trOptions = {
            objectId : unid ? unid : "_"+trIndex,
            isEdited : this.options.isEdited,
            id : "_"+trIndex,
            index : trIndex,
            indexText : trIndex,
            hasSequence : this.options.hasSequence,
            hasOperation : this.options.hasOperation,
            align : this.options.tdAlign,
            isNew : isNew,
            className : this.options.tdClass,
            tdAttributes : this.options.tdAttributes
        };
        var template = null;
        if( this.trTemplate ){
            template = this.trTemplate.clone();
        }
        var trObj = new MGridTr(this.table, trOptions, itemData, this, template, sourceData );
        trObj.load();

        //oldTrObj.mElement.replaceWith( trObj.mElement );
        trObj.mElement.inject( oldTrObj.mElement, "before" );

        var idx = this.trList.indexOf( oldTrObj );
        this.trList[idx] = trObj;

        this.trObjs[ trOptions.objectId ] = trObj;


        if( oldTrObj.options.isNew  ){
            this.trObjs_new[ oldTrObj.options.objectId ] = null;
        }else{
            this.trObjs_removed[ oldTrObj.options.objectId ] = oldTrObj;
        }

        if( isNew ){
            this.trObjs_new[ trOptions.objectId ] = trObj;
        }

        oldTrObj.mElement.destroy();

        for( var it in this.itemTemplate ){
            this.itemTemplate[it].value = "";
        }
    },
    createRemoveButton : function( trObj, container ){
        var button = new Element("div", { title : "删除" }).inject( container );
        if( this.options.lp.remove )button.set("text",this.options.lp.remove );
        if( this.css.actionRemove )button.setStyles( this.css.actionRemove );
        button.addEvents( {
            "click": function( e ){ this.removeTr( e, e.target, trObj ); }.bind(this)
        });
        if( this.css.actionRemove && this.css.actionRemove_over){
            button.addEvents({
                "mouseover" : function( e ){ this.node.setStyles( this.obj.css.actionRemove_over ) }.bind({ node : button, obj : this }),
                "mouseout" : function( e ){ this.node.setStyles( this.obj.css.actionRemove ) }.bind({ node : button, obj : this })
            })
        }
        return button
    },
    removeTr : function(e, el, trObj ){
        this.fireEvent("queryRemoveTr", [e, el, trObj]);

        var s = trObj.options;
        var id = s.objectId;

        if( ! s.isNew ){
            this.trObjs_removed[ id ] = this.trObjs[ id ];
        }

        this.trList.erase( trObj );

        this.trObjs[ id ] = null;
        //delete this.trObjs[ id ];

        if( this.trObjs_new[ id ] ){
            this.trObjs_new[ id ]= null;
            //delete this.trObjs_new[ id ];
        }

        //this.fireEvent("queryRemoveTr", [e, el.getParent("tr")]);

        trObj.destroy();

        this.adjustSequenceText();
        this.fireEvent("postRemoveTr", [e, el, trObj ]);
    },
    replaceText : function( value, selectValue, selectText ){
        var vals = typeOf( value ) == "array" ? value : value.split( this.valSeparator );
        var selectValues = typeOf( selectValue ) == "array" ? selectValue : selectValue.split( this.valSeparator );
        var selectTexts = typeOf( selectText ) == "array" ? selectText : selectText.split(this.valSeparator);
        for( var i=0 ;i<vals.length; i++ ){
            for( var j= 0; j<selectValues.length; j++){
                if( vals[i] == selectValues[j] ){
                    vals[i] = selectTexts[j]
                }
            }
        }
        return vals;
    },
    getResult : function( verify, separator, isAlert, onlyModified, keepAllData ){
        var result = [];
        var trObjs = this.trObjs;
        var flag = true;
        for( var tr in trObjs ){
            if( tr && trObjs[tr] ){
                var data = trObjs[tr].getResult( verify, separator, isAlert, onlyModified , keepAllData );
                if( !data ){
                    if( this.options.verifyType == "batch" ){
                        flag= false;
                    }else{
                        return false;
                    }
                }else{
                    result.push( data )
                }
            }
        }
        if( flag ){
            return result;
        }else{
            return false ;
        }
    },
    enableItem : function( itemName ){
        for( var tr in this.trObjs ){
            if( tr && this.trObjs[tr] ){
                this.trObjs[tr].enableItem( itemName );
            }
        }
    },
    disableItem : function( itemName ){
        for( var tr in this.trObjs  ){
            if( tr && this.trObjs[tr] ){
                this.trObjs[tr].disableItem( itemName );
            }
        }
    },
    adjustSequenceText : function(){
        var array = [];
        for( var tr in this.trObjs  ){
            if( tr && this.trObjs[tr] ){
                array.push( this.trObjs[tr] )
            }
        }
        array.sort( function( a, b ){
            return a.options.index - b.options.index
        });
        array.each( function( o, index ){
            o.setSequenceText(index + 1 );
            this.maxIndexText = index + 2;
        }.bind(this))
    },
    string2DOM: function( str, container, callback ){
        var wrapper =	str.test('^<the|^<tf|^<tb|^<colg|^<ca') && ['<table>', '</table>', 1] ||
            str.test('^<col') && ['<table><colgroup>', '</colgroup><tbody></tbody></table>',2] ||
            str.test('^<tr') && ['<table><tbody>', '</tbody></table>', 2] ||
            str.test('^<th|^<td') && ['<table><tbody><tr>', '</tr></tbody></table>', 3] ||
            str.test('^<li') && ['<ul>', '</ul>', 1] ||
            str.test('^<dt|^<dd') && ['<dl>', '</dl>', 1] ||
            str.test('^<le') && ['<fieldset>', '</fieldset>', 1] ||
            str.test('^<opt') && ['<select multiple="multiple">', '</select>', 1] ||
            ['', '', 0];
        if( container ){
            var el = new Element('div', {html: wrapper[0] + str + wrapper[1]}).getChildren();
            while(wrapper[2]--) el = el[0].getChildren();
            el.inject( container );
            if( callback )callback( container );
            return el;
        }else{
            var div = new Element('div', {html: wrapper[0] + str + wrapper[1]});
            div.setStyle("display","none").inject( $(document.body) );
            if( callback )callback( div );
            var el = div.getChildren();
            while(wrapper[2]--) el = el[0].getChildren();
            div.dispose();
            return el;
        }
    }
});

var MGridTr = new Class({
    Implements: [Options, Events],
    options: {
        objectId : "",
        isEdited : true,
        id : "",
        index : 0,
        indexText : "0",
        hasSequence  : true,
        hasOperation : true,
        align : "left",
        isNew : true,
        className : "",
        tdAttributes : null
    },
    initialize: function (container,options,itemData, parent, template, sourceData) {

        this.setOptions(options);

        this.container = container;
        this.mElement = null;
        this.items = {};
        this.itemData = itemData;
        this.parent = parent;
        this.template = template;
        this.sourceData = sourceData;
        this.css = this.parent.css || {};
        this.app = this.parent.app;
    },
    load:function(){
        if( this.options.isEdited ){
            this.create_Edit();
        }else{
            this.create_Read();
        }
    },
    create_Read : function(){
        var tr = this.mElement = new Element("tr", { "data-id" : this.options.id }).inject(this.container);

        var attr = {};
        if( this.options.align )attr.align = this.options.align;

        var styles = {};
        if( this.options.className && this.css[this.options.className] )styles = this.css[this.options.className];

        if( this.options.hasSequence  ){
            var td = this.sequenceTd = new Element("td", { align : "center", text : ( this.options.indexText || this.options.index) }).inject( tr );
            if( this.parent.options.sequenceClass && this.css[this.parent.options.sequenceClass] )td.setStyles(this.css[this.parent.options.sequenceClass]);
        }

        var idx = 1;
        for (var it in this.itemData ){
            var tdAttr = this.options.tdAttributes && this.options.tdAttributes["_"+idx] ? this.options.tdAttributes["_"+idx] : {};
            var td = new Element("td", { "text" : this.itemData[it].value }).inject( tr );
            td.set( attr );
            td.set( tdAttr );
            td.setStyle( styles );
            idx++;
        }
    },
    create_Edit : function(e, el){
        if( this.template ){
            this.create_Edit_byTemplate( e, el );
        }else{
            this.create_Edit_noTemplate( e, el );
        }
    },
    setSequenceText : function( text ){
        if(this.sequenceTd)this.sequenceTd.set("text",text);
    },
    create_Edit_byTemplate : function(){
        this.mElement = this.template;
        this.mElement.set("data-id", this.options.id );

        if( this.options.hasSequence  ){
            this.sequenceTd = this.mElement.getElement( "[sequence]" );
            if(this.sequenceTd)this.sequenceTd.set( "text" , ( this.options.indexText || this.options.index) );
        }

        this.mElement.getElements("[lable]").each(function( el ) {
            var itData = this.itemData[el.get("lable")];
            if (!itData)return;
            if(itData.text)el.set("text", itData.text);
        }.bind(this));

        this.mElement.getElements("[item]").each(function( el ) {
            var itData = this.itemData[el.get("item")];
            if (!itData)return;
            this.createItem( el, itData );
        }.bind(this));

        if( this.options.hasOperation && this.options.isEdited ){
            if( this.parent.options.minTrCount && this.options.index > this.parent.options.minTrCount ){
                var removeContainer = this.mElement.getElement("[button_remove]");
                this.parent.createRemoveButton( this, removeContainer );
            }
        }
        this.mElement.setStyle("display","");
        this.mElement.inject( this.container );
    },
    create_Edit_noTemplate : function(){
        this.mElement = new Element("tr", { "data-id" : this.options.id });

        var attr = {};
        if( this.options.align )attr.align = this.options.align;
        var styles = {};
        if( this.options.className && this.css[this.options.className] )styles = this.css[this.options.className];


        if( this.options.hasSequence  ){
            var td = this.sequenceTd = new Element("td", { align : "center", text : ( this.options.indexText || this.options.index) }).inject( this.mElement );
            if( this.parent.options.sequenceClass && this.css[this.parent.options.sequenceClass] )td.setStyles(this.css[this.parent.options.sequenceClass]);
        }

        var idx = 1;
        for (var it in this.itemData){
            var tdAttr = this.options.tdAttributes && this.options.tdAttributes["_"+idx] ? this.options.tdAttributes["_"+idx] : {};
            var td = new Element("td").inject( this.mElement );
            td.set(attr);
            td.set( tdAttr );
            td.setStyles( styles );
            var itData = this.itemData[it];
            this.createItem( td, itData );
            idx++;
        }
        if( this.options.hasOperation && this.options.isEdited ){
            if( this.parent.options.minTrCount && this.options.index > this.parent.options.minTrCount ) {
                var t = new Element("td", {align: "center", style: {width: "30px"}}).inject(this.mElement);
                var className = this.parent.options.removeActionTdClass;
                if( className && this.css[className] )t.setStyles(this.css[className]);
                this.parent.createRemoveButton(this, t);
            }else{
                var t = new Element("td").inject(this.mElement);
                var className = this.parent.options.removeActionTdClass;
                if( className && this.css[className] )t.setStyles(this.css[className]);
            }
        }
        this.mElement.inject( this.container );
    },
    createItem : function( container, itData ){
        //if( itData.disable )return;

        itData.objectId = itData.name;
        var item = new MDomItem(container, itData, this, this.app, this.css );
        if( this.parent.options.verifyType == "batchSingle" ){
            item.options.warningType = "single";
        }else{
            item.options.warningType = this.parent.options.verifyType;
        }
        item.options.name = itData.name + "_" + this.options.index;
        item.index = this.options.index;
        item.parent = this;
        item.load();

        this.items[itData.objectId] = item;
    },
    //remove : function(){
    //    this.mElement.destroy();
    //},
    destroy : function(){
        Object.each(this.items, function(item){
            item.destroy();
        }.bind(this));
        this.mElement.destroy();
        MWF.release(this);
    },
    enableItem: function( itemName ){
        if( itemName && this.items[itemName] ){
            var item = this.items[itemName];
            if( item.options.disable ){
                item.enable();
            }
        }
    },
    disableItem: function( itemName ){
        if( itemName && this.items[itemName] ){
            var item = this.items[itemName];
            if( !item.options.disable ){
                item.disable();
            }
        }
    },
    getPrevSilbing : function(){
        var idx = this.parent.trList.indexOf( this );
        if( idx > 0 ){
            return this.parent.trList[ idx -1 ];
        }else{
            return null;
        }
    },
    getNextSilbing : function(){
        var idx = this.parent.trList.indexOf( this );
        if( idx!=-1 && idx < this.parent.trList.length - 1 ){
            return this.parent.trList[ idx + 1 ];
        }else{
            return null;
        }
    },
    verify : function(isShowWarming) {
        var flag = true;
        for (var it in this.items ) {
            if (!this.items[it].verify(isShowWarming)) {
                if (this.parent.options.verifyType == "batch" || this.parent.options.verifyType == "batchSingle") {
                    flag = false;
                } else {
                    return false;
                }
            }
        }
        return flag;
    },

    getItemsKeyValue : function(separator , onlyModified ) {
        //separator 多值合并分隔符
        var key_value = {};
        for (var it in this.items ) {
            var item = this.items[it];
            var value = onlyModified ? item.getModifiedValue() : item.getValue();
            if( value != null ){
                if (typeOf(value) === "array") {
                    key_value[item.options.objectId] = (typeOf(separator) == "string" ? value.join(separator) : value );
                } else {
                    key_value[item.options.objectId] = value;
                }
            }
        }
        return key_value;
    },

    getResult : function(verify, separator, isShowWarming, onlyModified, keepAllData ) {
        if ( !verify || this.verify(isShowWarming)) {
            if( keepAllData && this.sourceData ){
                var result = this.sourceData;
                var map = this.getItemsKeyValue(separator, onlyModified);
                for( var key in map ){
                    result[key] = map[key];
                }
                return result;
            }else{
                return this.getItemsKeyValue(separator, onlyModified);
            }

        } else {
            return false;
        }
    }
});