选择框以在图像上单击

时间:2016-02-08 14:33:19

标签: javascript jquery html drop-down-menu jquery-ui-dialog

我正在尝试修改此示例http://waynegm.github.io/imgNotes/examples/basic_interactive.html# 当用户单击图像时,为了使选择选项显示在对话框内。目前,代码在用户在编辑模式下选择的区域上放置一个标记,并打开一个对话框,允许他们输入文本,并带有保存/取消选项。 在对话框中,我想将textarea更改为选择列表,如:

<select>
<option value="head">Head</option>
<option value="leftarm">Left Arm</option>
<option value="rightarm">Right Arm</option>
<option value="leg">Leg</option>
</select>

看起来它应该是一个简单的改变,我已经制作了其他样本,其中单击图像上的简单使下拉出现。但是这个例子还有我需要的所有东西,比如删除和记录地点标记的共同点的选项。 当前的HTML:

<html>
    <head>
        <title>jQuery imgNotes - Interactive Base</title>
        <style type="text/css" media="all">@import "css/marker.css";</style>

        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" media="screen">
        <script type="text/javascript" src="src/imgNotes.js"></script>

    </head>
    <body>
        <div id="imgdiv" style="text-align: center">
            <img id="image" src="image/personsit.jpg" style="border: 30px solid #ccc; padding:20px;" width=400/>
            <br/>
            <button id="toggleEdit">Edit</button>   <button id="export">Export</button>

        </div>
        <div id=txt></div>

<script type="text/javascript">
;(function($) {
    $(document).ready(function() {
        var $img = $("#image").imgNotes();
        //$img.imgNotes("import", [ {x: "0.5", y:"0.5", note:"AFL Grand Final Trophy"}, 
        //                          {x: "0.4", y:"0.269", note: "Shoulder"},
        //                          {x: "0.824", y: "0.593", note: "Fluffy microphone"}]);
        var $toggle = $("#toggleEdit");
        if ($img.imgNotes("option","canEdit")) {
            $toggle.text("View");
        } else {
            $toggle.text("Edit");
        }
        $toggle.on("click", function() {
                                    var $this = $(this);
                                    if ($this.text()=="Edit") {
                                        $this.text("View");
                                        $img.imgNotes("option", "canEdit", true);
                                    } else {
                                        $this.text('Edit');
                                        $img.imgNotes('option', 'canEdit', false);
                                    }
        });
        var $export = $("#export");
        $export.on("click", function() {
                                    var $table = $("<table/>").addClass("gridtable");
                                    var notes = $img.imgNotes('export');
                                    $table.append("<th>X</th><th>Y</th><th>NOTE</th>"); 
                                    $.each(notes, function(index, item) {
                                        $table.append("<tr><td>" + item.x + "</td><td>" + item.y + "</td><td>" + item.note + "</td></tr>");
                                    });
                                    $('#txt').html($table);
        });
    });
})(jQuery);
</script>
</body>
</html>

imgNotes.js文件

;(function($) {
    $.widget("wgm.imgNotes", {
        options: {
            zoom: 1,
            zoomStep: 0.1,
            zoomable: true,
            canEdit: false,
            vAll: "middle",
            hAll: "middle",
/*
* Default callback to create a marker indicating a note location
*   See the examples for more elaborate alternatives.
*/
            onAdd: function() {
                this.options.vAll = "bottom";
                this.options.hAll = "middle";
                return  $(document.createElement('span')).addClass("marker black").html(this.noteCount);
            },
/*
*   Default callback when the marker is clicked and the widget has canEdit = true
*   Opens a dialog with a textarea to write a note.
*   See the examples for a more elaborate alternative that includes a WYSIWYG editor
*/
            onEdit: function(ev, elem) {
                var $elem = $(elem);
                $("#dialog-2").remove();
                return $('<div id="dialog-2"></div>').dialog({
                    title: "Body Parts",
                    resizable: false,
                    modal: true,
                    height: "300",
                    width: "450",
                    position: { my: "left bottom", at: "right top", of: elem},
                    buttons: {
                        "Save": function() {
                            var txt = $('textarea', this).val();
//          Put the editied note back into the data area of the element
//          Very important that this step is included in custom callback implementations
                            $elem.data("note", txt);
                            $(this).dialog("close");
                        },
                        "Delete": function() {
                            $elem.trigger("remove");
                            $(this).dialog("close");
                        },
                        Cancel: function() {
                            $(this).dialog("close");
                        }
                    },
                    open: function() {
                        $(this).css("overflow", "hidden");
                        var textarea = $('<textarea id="txt" style="height:100%; width:100%;">');
                        $(this).html(textarea);
//          Get the note text and put it into the textarea for editing
                        textarea.val($elem.data("note"));
                    }
                })          
            },
/*
*   Default callback when the marker is clicked and the widget has canEdit = false
*   Opens a dialog displaying the contents of the marker's note
*   See examples for alternatives such as using tooltips.
*/
            onShow: function(ev, elem) {
                var $elem = $(elem);
                $('#NoteDialog').remove();
                return $('<div id="NoteDialog"></div>').dialog({
                    modal: false,
                    resizable: false,
                    height: 300,
                    width: 250,
                    position: { my: "left bottom", at: "right top", of: elem},
                    buttons: {
                        "Close" : function() {
                            $(this).dialog("close");
                        }
                    },
                    open: function() {
//          Get the note text and put it into the textarea for editing
                        $(this).html($elem.data("note"));
                        $(this).closest(".ui-dialog").find(".ui-dialog-titlebar:first").hide();

                    },
                    close: function() {
                        $(this).dialog("destroy");
                    }
                });
            },
/*
*   Default callback when the markers are repainted
*/
            onUpdateMarker: function(elem) {
                var $elem = $(elem);
                var $img = $(this.img);
                var pos = $img.imgViewer("imgToView", $elem.data("relx"), $elem.data("rely"));
                if (pos) {
                    $elem.css({
                        left: (pos.x - $elem.data("xOffset")),
                        top: (pos.y - $elem.data("yOffset")),
                        position: "absolute"
                    });
                }
            },
/*
*   Default callback when the image view is repainted
*/
            onUpdate: function() {
                var self = this;
                $.each(this.notes, function() {
                    self.options.onUpdateMarker.call(self, this);
                });
            }
        },


        _create: function() {
            var self = this;
            if (!this.element.is("img")) {
                $.error('imgNotes plugin can only be applied to img elements');
            }
//      the note/marker elements
            self.notes = [];
//      the number of notes
            self.noteCount = 0;
//      the original img element
            self.img = self.element[0];
            var $img = $(self.img);
//      attach the imgViewer plugin for zooming and panning with a custon click and update callbacks
            $img.imgViewer({
                            onClick: function(ev, imgv) {
                                if (self.options.canEdit) {
                                    ev.preventDefault();
                                    var rpos = imgv.cursorToImg(ev.pageX, ev.pageY);
                                    if (rpos) {
                                        var elem = self.addNote(rpos.x, rpos.y);
                                        self._trigger("onEdit", ev, elem);
                                    }
                                }
                            },
                            onUpdate: function(ev, imgv) {
                                self.options.zoom = imgv.options.zoom;
                                self.options.onUpdate.call(self);
                            },
                            zoom: self.options.zoom,
                            zoomStep: self.options.zoomStep,
                            zoomable: self.options.zoomable
            });
            $img.imgViewer("update");
        },
/*
*   Remove the plugin
*/  
        destroy: function() {
            this.clear();
            $(this.img).imgViewer("destroy");
            $.Widget.prototype.destroy.call(this);
        },

        _setOption: function(key, value) {
            switch(key) {
                case 'vAll':
                    switch(value) {
                        case 'top': break;
                        case 'bottom': break;
                        default: value = 'middle';
                    }
                    break;
                case 'hAll':
                    switch(value) {
                        case 'left': break;
                        case 'right': break;
                        default: value = 'middle';
                    }
                    break;
            }
            var version = $.ui.version.split('.');
            if (version[0] > 1 || version[1] > 8) {
                this._super(key, value);
            } else {
                $.Widget.prototype._setOption.apply(this, arguments);
            }
            switch(key) {
                case 'zoom':
                    $(this.img).imgViewer("option", "zoom", value);
                    break;
                case 'zoomStep':
                    $(this.img).imgViewer("option", "zoomStep", value);
                    break;
                case 'zoomable':
                    $(this.img).imgViewer("option", "zoomable", value);
                    break;
            }
        },
/*
*   Pan the view to be centred at the given relative image location
*/
        panTo: function(relx, rely) {
            return $(this.img).imgViewer("panTo", relx, rely);
        },

/*
*   Add a note
*/
        addNote: function(relx, rely, text) {
            var self = this;
            this.noteCount++;
            var elem = this.options.onAdd.call(this);
            var $elem = $(elem);
            $(this.img).imgViewer("addElem",elem);
            $elem.data("relx", relx).data("rely", rely).data("note", text);

            switch (this.options.vAll) {
                case "top": $elem.data("yOffset", 0); break;
                case "bottom": $elem.data("yOffset", $elem.height()); break;
                default: $elem.data("yOffset", Math.round($elem.height()/2));
            }
            switch (this.options.hAll) {
                case "left": $elem.data("xOffset", 0); break;
                case "right": $elem.data("xOffset", $elem.width()); break;
                default: $elem.data("xOffset", Math.round($elem.width()/2));
            }
            $elem.click(function(ev) {
                ev.preventDefault();
                if (self.options.canEdit) {
                    self._trigger("onEdit", ev, elem);
                } else {
                    self._trigger("onShow", ev, elem);
                }
            });
            $elem.on("remove", function() {
                self._delete(elem);
            });
            this.notes.push(elem);
            $(this.img).imgViewer("update");
            return elem;
        },
/*
*   Number of notes
*/
        count: function() {
            return this.noteCount;
        },
/*
*   Delete a note
*/
        _delete: function(elem) {
            this.notes = this.notes.filter(function(v) { return v!== elem; });
            $(elem).off();
            $(elem).remove();
            $(this.img).imgViewer("update");
        },
/*
*   Clear all notes
*/
        clear: function() {
            var self = this;
            var total = self.notes.length;
            for ( var i = 0; i < total; i++ ){
                var $this = self.notes[i];
                $this.off();
                $this.remove();
            }
            self.notes=[];
            self.noteCount = 0;
        },
/*
*   Add notes from a javascript array
*/
        import: function(notes) {
            var self = this;
            $.each(notes, function() {
                self.addNote(this.x, this.y, this.note);
            });
            $(this.img).imgViewer("update");
        },
/*
*   Export notes to an array
*/
        export: function() {
            var notes = [];
            $.each(this.notes, function() {
                var $elem = $(this);
                notes.push({
                        x: $elem.data("relx"),
                        y: $elem.data("rely"),
                        note: $elem.data("note")
                });
            });
            return notes;
        }
    });
})(jQuery);

如果有人能帮助我,我真的很感激。

0 个答案:

没有答案