Blame view

app-ht/web/exts-src/catalog/js/dyno-table-1.0.0.js 7.25 KB
8ec727c1   曹明   初始化代码提交
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
$(document).ready(function() {
    (function($){
        $.fn.extend({
            dynoTable: function(options) {

                var defaults = {
                    removeClass: '.row-remover',
                    cloneClass: '.row-cloner',
                    addRowTemplateId: '#add-template',
                    addRowButtonId: '#add-row',
                    lastRowRemovable: true,
                    orderable: true,
                    dragHandleClass: ".drag-handle",
                    insertFadeSpeed: "slow",
                    removeFadeSpeed: "fast",
                    hideTableOnEmpty: true,
                    onRowRemove: function(){},
                    onRowClone: function(clonedRow){},
                    onRowAdd: function(){},
                    onTableEmpty: function(){},
                    onRowReorder: function(){}
                };

                options = $.extend(defaults, options);

                var cloneRow = function(btn) {
                    var clonedRow = $(btn).closest('tr').clone();
                    var tbod = $(btn).closest('tbody');
                    insertRow(clonedRow, tbod);
                    options.onRowClone(clonedRow);
                }

                var insertRow = function(clonedRow, tbod) {
                    var numRows = $(tbod).children("tr").length;
                    if(options.hideTableOnEmpty && numRows == 0) {
                        $(tbod).parents("table").first().show();
                    }

                    $(clonedRow).find('*').andSelf().filter('[id]').each( function() {
                        //change to something else so we don't have ids with the same name
                        this.id += '__c';
                    });

                    //finally append new row to end of table
                    $(tbod).append( clonedRow );
                    bindActions(clonedRow);
                    $(tbod).children("tr:last").hide().fadeIn(options.insertFadeSpeed);
                }

                var removeRow = function(btn) {
                    var tbod = $(btn).parents("tbody:first");
                    var numRows = $(tbod).children("tr").length;

                    if(numRows > 1 || options.lastRowRemovable === true) {
                        var trToRemove = $(btn).parents("tr:first");
                        $(trToRemove).fadeOut(options.removeFadeSpeed, function() {
                            $(trToRemove).remove();
                            options.onRowRemove();
                            if(numRows == 1) {
                                if(options.hideTableOnEmpty) {
                                    $(tbod).parents('table').first().hide();
                                }
                                options.onTableEmpty();
                            }
                        });
                    }
                }

                var bindClick = function(elem, fn) {
                    $(elem).click(fn);
                }

                var bindCloneLink = function(lnk) {
                    bindClick(lnk, function(){
                        var btn = $(this);
                        cloneRow(btn);
                        return false;
                    });
                }

                var bindRemoveLink = function(lnk) {
                    bindClick(lnk, function(){
                        var btn = $(this);
                        removeRow(btn);
                        return false;
                    });
                }

                var bindActions = function(obj) {
                    obj.find(options.removeClass).each(function() {
                        bindRemoveLink($(this));
                    });

                    obj.find(options.cloneClass).each(function() {
                        bindCloneLink($(this));
                    });
                }

                return this.each(function() {
                    //Sanity check to make sure we are dealing with a single case
                    if(this.nodeName.toLowerCase() == 'table') {
                        var table = $(this);
                        var tbody = $(table).children("tbody").first();

                        if(options.orderable && jQuery().sortable) {
                            $(tbody).sortable({
                                handle : options.dragHandleClass,
                                helper:  function(e, ui) {
                                    ui.children().each(function() {
                                        $(this).width($(this).width());
                                    });
                                    return ui;
                                },
                                items: "tr",
                                update : function (event, ui) {
                                    options.onRowReorder();
                                }
                            });
                        }

                        $(table).find(options.addRowTemplateId).each(function(){
                            $(this).removeAttr("id");
                            var tmpl = $(this);
                            tmpl.remove();
                            bindClick($(options.addRowButtonId), function(){
                                var newTr = tmpl.clone();
                                insertRow(newTr, tbody);
                                options.onRowAdd();
                                return false;
                            });
                        });
                        bindActions(table);

                        var numRows = $(tbody).children("tr").length;
                        if(options.hideTableOnEmpty && numRows == 0) {
                            $(table).hide();
                        }
                    }
                });
            }
        });
    })(jQuery);

    /*
     * dynoTable configuration options
     * These are the options that are available with their default values
     */
    $('#add_prop').dynoTable({
        removeClass: '.row-remover', //class for the clickable row remover
        cloneClass: '.row-cloner', //class for the clickable row cloner
        addRowTemplateId: '#add-template', //id for the "add row template"
        addRowButtonId: '#add-row', //id for the clickable add row button, link, etc
        lastRowRemovable: true, //If true, ALL rows in the table can be removed, otherwise there will always be at least one row
        orderable: true, //If true, table rows can be rearranged
        dragHandleClass: ".drag-handle", //class for the click and draggable drag handle
        insertFadeSpeed: "slow", //Fade in speed when row is added
        removeFadeSpeed: "fast", //Fade in speed when row is removed
        hideTableOnEmpty: true, //If true, table is completely hidden when empty
        onRowRemove: function() {
            //Do something when a row is removed
        },
        onRowClone: function(clonedRow) {
            //Do something when a row is cloned
            clonedRow.find('input[name="PropValue[value_id][]"]').val("");
        },
        onRowAdd: function() {
            //Do something when a row is added
        },
        onTableEmpty: function() {
            //Do something when ALL rows have been removed
        },
        onRowReorder: function() {
            //Do something when table rows have been rearranged
        }
    });
});