From f8d564659fcc4c88721d7460a1c93e7ae0068a05 Mon Sep 17 00:00:00 2001 From: dariaplotnikova Date: Thu, 20 Apr 2017 13:28:05 +0700 Subject: [PATCH 1/2] tree representations for select2 added --- .../spicy.core.admin/js/jquery.select2tree.js | 107 ++++++++++++++++++ .../spicy.core.admin/admin/formfield.html | 52 +++++++-- 2 files changed, 148 insertions(+), 11 deletions(-) create mode 100644 src/spicy/core/admin/static/spicy.core.admin/js/jquery.select2tree.js diff --git a/src/spicy/core/admin/static/spicy.core.admin/js/jquery.select2tree.js b/src/spicy/core/admin/static/spicy.core.admin/js/jquery.select2tree.js new file mode 100644 index 0000000..508a76c --- /dev/null +++ b/src/spicy/core/admin/static/spicy.core.admin/js/jquery.select2tree.js @@ -0,0 +1,107 @@ +(function($) { + $.fn.select2tree = function(options) { + var defaults = { + language: "en", + theme: "bootstrap" + }; + var opts = $.extend(defaults, options); + opts.templateResult = function(data, container) { + if(data.element) { + //insert span element and add 'parent' property + var $wrapper = $("" + data.text + ""); + var $element = $(data.element); + $(container).attr("val", $element.val()); + if($element.attr("parent")) { + $(container).attr("parent", $element.attr("parent")); + } + return $wrapper; + } else { + return data.text; + } + }; + $(this).select2(opts).on("select2:open", open); + }; + function recursiveParentSelection(child, parentVals){ + var parentVal = child.attr('parent'); + var parent = $('li[val=' + parentVal + ']'); + parentVals.push(parentVal); + if (parent.attr('parent') != undefined) { recursiveParentSelection(parent, parentVals); } + else { $('#id_{{ field.html_name }}').val(parentVals).trigger('select2:select'); } + } + function getExistedValues(){ + var vals = []; + $('#id_{{ field.html_name }} option').each(function(){ + var optionText = $(this).text(); + var optVal = $(this).val(); + $('.select2-selection__choice').each(function(){ + if ($(this).text().replace('×', '') == optionText) { + vals.push(optVal); + } + }); + }); + return vals; + } + function selectTree(evt){ + var $this = $(this); + var parentVals = getExistedValues(); + recursiveParentSelection($this, parentVals); + } + function moveOption(id) { + if(id) { + $(".select2-results__options li[parent=" + id + "]").insertAfter(".select2-results__options li[val=" + id + "]"); + $(".select2-results__options li[parent=" + id + "]").each(function() { + moveOption($(this).attr("val")); + }); + } else { + $(".select2-results__options li:not([parent])").appendTo(".select2-results__options ul"); + $(".select2-results__options li:not([parent])").each(function() { + moveOption($(this).attr("val")); + }); + } + } + //deal switch action + function switchAction(id, open) { + $(".select2-results__options li[parent='" + id + "']").each(function() { + switchAction($(this).attr("val"), open); + }); + if(open) { + $(".select2-results__options li[val=" + id + "] span[class]:eq(0)").removeClass("glyphicon-chevron-right").addClass("glyphicon-chevron-down"); + $(".select2-results__options li[parent='" + id + "']").slideDown(); + } else { + $(".select2-results__options li[val=" + id + "] span[class]:eq(0)").addClass("glyphicon-chevron-right").removeClass("glyphicon-chevron-down"); + $(".select2-results__options li[parent='" + id + "']").slideUp(); + } + } + //get the level of li + function getLevel(id) { + var level = 0; + while($(".select2-results__options li[parent][val='" + id + "']").length > 0) { + id = $(".select2-results__options li[val='" + id + "']").attr("parent"); + level++; + } + return level; + } + function open() { + setTimeout(function() { + moveOption(); + $(".select2-results__options li").each(function() { + var $this = $(this); + //loop li add some classes and properties + if($this.attr("parent")) { + + $(this).siblings("li[val=" + $this.attr("parent") + "]").find("span:eq(0)").addClass("glyphicon glyphicon-chevron-down switch").css({ + "cursor": "default" + }); + + $(this).siblings("li[val=" + $this.attr("parent") + "]").find("span:eq(1)").css("font-weight", "bold"); + } + //add gap for children + if(!$this.attr("style")) { + var paddingLeft = getLevel($this.attr("val")) * 2; + $("li[parent='" + $this.attr("parent") + "']").css("padding-left", paddingLeft + "em"); + } + $this.on('mouseup', selectTree); + }); + }, 0); + } +})(jQuery); \ No newline at end of file diff --git a/src/spicy/core/admin/templates/spicy.core.admin/admin/formfield.html b/src/spicy/core/admin/templates/spicy.core.admin/admin/formfield.html index f2e6778..5b24646 100755 --- a/src/spicy/core/admin/templates/spicy.core.admin/admin/formfield.html +++ b/src/spicy/core/admin/templates/spicy.core.admin/admin/formfield.html @@ -1,5 +1,5 @@ {% spaceless %} -{% load url from future %} +{% load url from future %}{% load staticfiles %} {# new admin #} {% if type == 'li-file' %} @@ -286,9 +286,10 @@ }) {% else %} + {% endif %} @@ -312,6 +313,33 @@ {% endif %} +{% elif type == 'li-tree-select2' %} + + + + + + + + + +
  • + {{ label }} + + +
  • + {% elif type == 'table-formset'%}
    @@ -351,18 +379,20 @@ {% endfor %} - + + + {% endif %} -{% endspaceless %} +{% endspaceless %} \ No newline at end of file From 940a55509c82127a6b5aa7d34a34f10ebe07e17b Mon Sep 17 00:00:00 2001 From: dariaplotnikova Date: Thu, 20 Apr 2017 13:41:28 +0700 Subject: [PATCH 2/2] changelog added, verion changed to 1.2.2 --- CHANGELOG.md | 9 +++++++++ setup.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2ed4f7b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +## [1.2.2] 2017-04-19 + +* Добавлен виджет для древовидного отображения опций выпадающего списка + + + +## [1.2.1] +{TODO описать возможности 1.2.1} + diff --git a/setup.py b/setup.py index 2416743..8d046fe 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def long_description(): setup( name='spicy', - version='1.2.1', + version='1.2.2', author='Burtsev Alexander', author_email='ab@bramabrama.com', @@ -86,7 +86,7 @@ def long_description(): }, classifiers=[ 'Framework :: Django', - 'Development Status :: 1.2.1', + 'Development Status :: 1.2.2', 'Topic :: Internet', 'License :: OSI Approved :: BSD License', 'Intended Audience :: Developers',