[Kimchi-devel] [PATCH] UI: Adding selectmenu widget for kimchi new UI

Wen Wang wenwang at linux.vnet.ibm.com
Wed Jan 28 08:41:01 UTC 2015


Signed-off-by: Wen Wang <wenwang at linux.vnet.ibm.com>
---
 ui/css/theme-default/selectmenu-flat.css |  84 +++++++++++++++++++++
 ui/js/widgets/selectmenu-flat.js         | 122 +++++++++++++++++++++++++++++++
 2 files changed, 206 insertions(+)
 create mode 100644 ui/css/theme-default/selectmenu-flat.css
 create mode 100644 ui/js/widgets/selectmenu-flat.js

diff --git a/ui/css/theme-default/selectmenu-flat.css b/ui/css/theme-default/selectmenu-flat.css
new file mode 100644
index 0000000..584b535
--- /dev/null
+++ b/ui/css/theme-default/selectmenu-flat.css
@@ -0,0 +1,84 @@
+/*
+ * Project Kimchi
+ *
+ * Copyright IBM, Corp. 2015
+ *
+ * Licensed under the Apache License, Version 2.0 (the 'License');
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an 'AS IS' BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+.selected-box {
+    width: 100px;
+    height: 30px;
+    border: 1px solid #d8d8d8;
+    border-radius: 3px;
+    cursor: pointer;
+    margin-left: 50px;
+    background: #FFFFFF;
+}
+
+.select-val {
+    display: none;
+}
+
+.select-label {
+    line-height: 32px;
+    vertical-align: middle;
+    position: relative;
+    margin-left: 20px;
+}
+
+.select-icon {
+    position: relative;
+    float: right;
+    line-height: 30px;
+    vertical-align: middle;
+    padding-right: 10px;
+    padding-left: 10px;
+    color: #555555;
+    font-size: 10px;
+}
+
+.select-icon:hover {
+    background: #FCFCFC;
+}
+
+.selectmenu-container {
+    border: 1px solid #999999;
+    width: 100px;
+}
+
+.selectmenu-opt {
+    background: #FFFFFF;
+}
+
+.selectmenu-list {
+    border: 1px solid #d8d8d8;
+    width: 150px;
+    padding-left: 0;
+    margin-top: 0;
+    cursor: pointer;
+}
+
+.selectmenu-list li {
+    list-style: none;
+    position: relative;
+    height: 20px;
+    padding: 5px 0 5px 20px;
+    line-height: 21px;
+    vertical-align: middle;
+    border-bottom: 1px solid #d8d8d8;
+}
+
+.selectmenu-opt:hover {
+    background: #FCFCFC;
+}
diff --git a/ui/js/widgets/selectmenu-flat.js b/ui/js/widgets/selectmenu-flat.js
new file mode 100644
index 0000000..a6db865
--- /dev/null
+++ b/ui/js/widgets/selectmenu-flat.js
@@ -0,0 +1,122 @@
+/*
+ * Project Kimchi
+ *
+ * Copyright IBM, Corp. 2015
+ *
+ * Licensed under the Apache License, Version 2.0 (the 'License');
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an 'AS IS' BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+// How to use:
+//     $(".selector").selectmenuFlat({
+//         content: [1,2,3,4,5,6],  //Set content of the select menu
+//         selected: 2  // set the selected option, starts from "1"
+//     });
+//     $(".selector").selectmenuFlat("value", "4"); //Set value.
+//     var t = $(".selector").selectmenuFlat("value");  //Get value
+//     console.log(t);
+
+
+ (function( $ ) {
+    $.widget("kimchi.selectmenuFlat", {
+
+        options: {
+            content: null,
+            name: null,
+            selected: null,
+            parentid: null
+        },
+
+        _create: function() {
+            var that = this;
+            var name = that.options.name || $(this.element).attr("id");
+            var value = that.options.content;
+            var parentid = $(this.element).attr("id");
+            that.options.parentid = parentid;
+            var html = "<div class='selected-box' id='select-" + name + "'>" +
+                    "<input class='select-val'>" +
+                    "<span class='select-label'></span>" +
+                    "<span class='select-icon icon-down-open'></span>" +
+                    "</div>";
+            $(html).appendTo(that.element);
+            html = that._setValue(value);
+            $(html).appendTo(that.element);
+            var sel = that.options.selected || value[0];
+            that.options.selected = $.inArray(sel, that.options.content).toString() + 1;
+            $(".select-val", "#" + parentid).text(sel);
+            $(".select-label", "#" + parentid).text(sel);
+            $(".selected-box", "#" + parentid).on("click", this._toggleOpt);
+            $(".selectmenu-opt", "#" + parentid).on("click", function() {
+                var selectedText = $(this).text();
+                that.options.selected = $.inArray(selectedText, that.options.content) +1;
+                $(".selected-box .select-label", "#" + parentid).text(selectedText);
+                $(".select-val", "#" + parentid).text(selectedText);
+                that._toggleOpt();
+            });
+            $(document).mouseup(function(e) {
+                var container = $(".selectmenu-opt");
+                if(!container.is(e.target) && container.has(e.target).length === 0 && $(".select-icon").hasClass("icon-up-open")) {
+                    $(".selectmenu-list", "#" + parentid).prop("style", "display:none");
+                    $(".select-icon", "#" + parentid).removeClass("icon-up-open");
+                    $(".select-icon", "#" + parentid).addClass("icon-down-open").css({
+                        "border-left": "none"
+                    });
+                }
+            });
+        },
+
+        _setValue: function(value) {
+            var that = this;
+            var html = "<ul class='selectmenu-list' name='" + name + "' style='display:none'>";
+            var name = this.options.name || $(this.element).attr("id");
+            $.each(value, function(index, data) {
+                that.options.content[index] = data.toString();
+                html += "<li id='" + name + index + "' class='selectmenu-opt'>" + data + "</li>";
+            });
+            html += "</ul>"
+            return html;
+        },
+
+        _toggleOpt: function() {
+            if($(".select-icon").hasClass("icon-down-open")) {
+                $(".selectmenu-list").prop("style", "display");
+                $(".select-icon").removeClass("icon-down-open");
+                $(".select-icon").addClass("icon-up-open").css({
+                    "border-left": "1px solid #d8d8d8"
+                });
+            } else {
+                $(".selectmenu-list").prop("style", "display:none");
+                $(".select-icon").removeClass("icon-up-open");
+                $(".select-icon").addClass("icon-down-open").css({
+                    "border-left": "none"
+                });
+            }
+        },
+
+        value: function(value) {
+            var parentid = this.options.parentid;
+            if(!value) {
+                return $(".selected-box .select-label", "#" + parentid).text();
+            }
+            if (value <= this.options.content.length) {
+                this.options.selected = value;
+                var selectedText = this.options.content[value-1];
+                $(".selected-box .select-label", "#" + parentid).text(selectedText);
+            }
+        },
+
+        _destroy: function() {
+            this.element.remove();
+        }
+    });
+ })(jQuery);
\ No newline at end of file
-- 
2.1.0




More information about the Kimchi-devel mailing list