{I" class:ETI"BundledAsset;FI"logical_path;TI"#expandable_tree/initializer.js;FI" pathname;TI"n/home/nyitrai/RubymineProjects/jquery_sortable_tree/app/assets/javascripts/expandable_tree/initializer.js;FI"content_type;TI"application/javascript;TI" mtime;Tl+õ²ÏSI" length;TiÖdI" digest;TI"%716ec105c022b98a60602620367b06ab;FI" source;TI"Öd/* * jQuery UI Nested Sortable * v 1.3.4 / 28 apr 2011 * http://mjsarfatti.com/sandbox/nestedSortable * * Depends: * jquery.ui.sortable.js 1.8+ * * License CC BY-SA 3.0 * Copyright 2010-2011, Manuele J Sarfatti */ (function($) { $.widget("ui.nestedSortable", $.extend({}, $.ui.sortable.prototype, { options: { tabSize: 20, disableNesting: 'ui-nestedSortable-no-nesting', errorClass: 'ui-nestedSortable-error', listType: 'ol', listClass: 'nested_set', maxLevels: 0, noJumpFix: 0 }, _create: function(){ if (this.noJumpFix == false) this.element.height(this.element.height()); this.element.data('sortable', this.element.data('nestedSortable')); return $.ui.sortable.prototype._create.apply(this, arguments); }, _mouseDrag: function(event) { //Compute the helpers position this.position = this._generatePosition(event); this.positionAbs = this._convertPositionTo("absolute"); if (!this.lastPositionAbs) { this.lastPositionAbs = this.positionAbs; } //Do scrolling if(this.options.scroll) { var o = this.options, scrolled = false; if(this.scrollParent[0] != document && this.scrollParent[0].tagName != 'HTML') { if((this.overflowOffset.top + this.scrollParent[0].offsetHeight) - event.pageY < o.scrollSensitivity) this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop + o.scrollSpeed; else if(event.pageY - this.overflowOffset.top < o.scrollSensitivity) this.scrollParent[0].scrollTop = scrolled = this.scrollParent[0].scrollTop - o.scrollSpeed; if((this.overflowOffset.left + this.scrollParent[0].offsetWidth) - event.pageX < o.scrollSensitivity) this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft + o.scrollSpeed; else if(event.pageX - this.overflowOffset.left < o.scrollSensitivity) this.scrollParent[0].scrollLeft = scrolled = this.scrollParent[0].scrollLeft - o.scrollSpeed; } else { if(event.pageY - $(document).scrollTop() < o.scrollSensitivity) scrolled = $(document).scrollTop($(document).scrollTop() - o.scrollSpeed); else if($(window).height() - (event.pageY - $(document).scrollTop()) < o.scrollSensitivity) scrolled = $(document).scrollTop($(document).scrollTop() + o.scrollSpeed); if(event.pageX - $(document).scrollLeft() < o.scrollSensitivity) scrolled = $(document).scrollLeft($(document).scrollLeft() - o.scrollSpeed); else if($(window).width() - (event.pageX - $(document).scrollLeft()) < o.scrollSensitivity) scrolled = $(document).scrollLeft($(document).scrollLeft() + o.scrollSpeed); } if(scrolled !== false && $.ui.ddmanager && !o.dropBehaviour) $.ui.ddmanager.prepareOffsets(this, event); } //Regenerate the absolute position used for position checks this.positionAbs = this._convertPositionTo("absolute"); //Set the helper position if(!this.options.axis || this.options.axis != "y") this.helper[0].style.left = this.position.left+'px'; if(!this.options.axis || this.options.axis != "x") this.helper[0].style.top = this.position.top+'px'; //Rearrange for (var i = this.items.length - 1; i >= 0; i--) { //Cache variables and intersection, continue if no intersection var item = this.items[i], itemElement = item.item[0], intersection = this._intersectsWithPointer(item); if (!intersection) continue; if(itemElement != this.currentItem[0] //cannot intersect with itself && this.placeholder[intersection == 1 ? "next" : "prev"]()[0] != itemElement //no useless actions that have been done before && !$.contains(this.placeholder[0], itemElement) //no action if the item moved is the parent of the item checked && (this.options.type == 'semi-dynamic' ? !$.contains(this.element[0], itemElement) : true) //&& itemElement.parentNode == this.placeholder[0].parentNode // only rearrange items within the same container ) { this.direction = intersection == 1 ? "down" : "up"; if (this.options.tolerance == "pointer" || this._intersectsWithSides(item)) { this._rearrange(event, item); } else { break; } // Clear emtpy ul's/ol's this._clearEmpty(itemElement); this._trigger("change", event, this._uiHash()); break; } } var parentItem = (this.placeholder[0].parentNode.parentNode && $(this.placeholder[0].parentNode.parentNode).closest('.ui-sortable').length) ? $(this.placeholder[0].parentNode.parentNode) : null; var level = this._getLevel(this.placeholder); var childLevels = this._getChildLevels(this.helper); var previousItem = this.placeholder[0].previousSibling ? $(this.placeholder[0].previousSibling) : null; if (previousItem != null) { while (previousItem[0].nodeName.toLowerCase() != 'li' || previousItem[0] == this.currentItem[0]) { if (previousItem[0].previousSibling) { previousItem = $(previousItem[0].previousSibling); } else { previousItem = null; break; } } } newList = document.createElement(o.listType); $(newList).addClass(o.listClass) this.beyondMaxLevels = 0; // If the item is moved to the left, send it to its parent level if (parentItem != null && this.positionAbs.left < parentItem.offset().left) { parentItem.after(this.placeholder[0]); this._clearEmpty(parentItem[0]); this._trigger("change", event, this._uiHash()); } // If the item is below another one and is moved to the right, make it a children of it else if (previousItem != null && this.positionAbs.left > previousItem.offset().left + o.tabSize) { this._isAllowed(previousItem, level+childLevels+1); if (!previousItem.children(o.listType).length) { previousItem[0].appendChild(newList); } previousItem.children(o.listType)[0].appendChild(this.placeholder[0]); this._trigger("change", event, this._uiHash()); } else { this._isAllowed(parentItem, level+childLevels); } //Post events to containers this._contactContainers(event); //Interconnect with droppables if($.ui.ddmanager) $.ui.ddmanager.drag(this, event); //Call callbacks this._trigger('sort', event, this._uiHash()); this.lastPositionAbs = this.positionAbs; return false; }, _mouseStop: function(event, noPropagation) { // If the item is in a position not allowed, send it back if (this.beyondMaxLevels) { var parent = this.placeholder.parent().closest(this.options.items); for (var i = this.beyondMaxLevels - 1; i > 0; i--) { parent = parent.parent().closest(this.options.items); } this.placeholder.removeClass(this.options.errorClass); parent.after(this.placeholder); this._trigger("change", event, this._uiHash()); } $.ui.sortable.prototype._mouseStop.apply(this, arguments); }, serialize: function(o) { var items = this._getItemsAsjQuery(o && o.connected); var str = []; o = o || {}; $(items).each(function() { var res = ($(o.item || this).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); var pid = ($(o.item || this).parent(o.listType).parent('li').attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); if(res) str.push((o.key || res[1]+'['+(o.key && o.expression ? res[1] : res[2])+']')+'='+(pid ? (o.key && o.expression ? pid[1] : pid[2]) : 'root')); }); if(!str.length && o.key) { str.push(o.key + '='); } return str.join('&'); }, toHierarchy: function(o) { o = o || {}; var sDepth = o.startDepthCount || 0; var ret = []; $(this.element).children('li').each(function() { var level = _recursiveItems($(this)); ret.push(level); }); return ret; function _recursiveItems(li) { var id = ($(li).attr(o.attribute || 'id') || '').match(o.expression || (/(.+)[-=_](.+)/)); if (id != null) { var item = {"id" : id[2]}; if ($(li).children(o.listType).children('li').length > 0) { item.children = []; $(li).children(o.listType).children('li').each(function () { var level = _recursiveItems($(this)); item.children.push(level); }); } return item; } } }, toArray: function(o) { o = o || {}; var sDepth = o.startDepthCount || 0; var ret = []; var left = 2; ret.push({"item_id": 'root', "parent_id": 'none', "depth": sDepth, "left": '1', "right": ($('li', this.element).length + 1) * 2}); $(this.element).children('li').each(function () { left = _recursiveArray(this, sDepth + 1, left); }); function _sortByLeft(a,b) { return a['left'] - b['left']; } ret = ret.sort(_sortByLeft); return ret; function _recursiveArray(item, depth, left) { right = left + 1; if ($(item).children(o.listType).children('li').length > 0) { depth ++; $(item).children(o.listType).children('li').each(function () { right = _recursiveArray($(this), depth, right); }); depth --; } id = ($(item).attr(o.attribute || 'id')).match(o.expression || (/(.+)[-=_](.+)/)); if (depth === sDepth + 1) pid = 'root'; else { parentItem = ($(item).parent(o.listType).parent('li').attr('id')).match(o.expression || (/(.+)[-=_](.+)/)); pid = parentItem[2]; } if (id != null) { ret.push({"item_id": id[2], "parent_id": pid, "depth": depth, "left": left, "right": right}); } return left = right + 1; } }, _clear: function(event, noPropagation) { $.ui.sortable.prototype._clear.apply(this, arguments); // Clean last empty ul/ol for (var i = this.items.length - 1; i >= 0; i--) { var item = this.items[i].item[0]; this._clearEmpty(item); } return true; }, _clearEmpty: function(item) { if (item.children[1] && item.children[1].children.length == 0) { item.removeChild(item.children[1]); } }, _getLevel: function(item) { var level = 1; if (this.options.listType) { var list = item.closest(this.options.listType); while (!list.is('.ui-sortable')/* && level < this.options.maxLevels*/) { level++; list = list.parent().closest(this.options.listType); } } return level; }, _getChildLevels: function(parent, depth) { var self = this, o = this.options, result = 0; depth = depth || 0; $(parent).children(o.listType).children(o.items).each(function (index, child) { result = Math.max(self._getChildLevels(child, depth + 1), result); }); return depth ? result + 1 : result; }, _isAllowed: function(parentItem, levels) { var o = this.options // Are we trying to nest under a no-nest or are we nesting too deep? if (parentItem == null || !(parentItem.hasClass(o.disableNesting))) { if (o.maxLevels < levels && o.maxLevels != 0) { this.placeholder.addClass(o.errorClass); this.beyondMaxLevels = levels - o.maxLevels; } else { this.placeholder.removeClass(o.errorClass); this.beyondMaxLevels = 0; } } else { this.placeholder.addClass(o.errorClass); if (o.maxLevels < levels && o.maxLevels != 0) { this.beyondMaxLevels = levels - o.maxLevels; } else { this.beyondMaxLevels = 1; } } } })); $.ui.nestedSortable.prototype.options = $.extend({}, $.ui.sortable.prototype.options, $.ui.nestedSortable.prototype.options); })(jQuery); (function() { $(function() { $('ol.sortable_tree').siblings('form').on('ajax:success', function(event, data, status, xhr) { var list_item, sortable_tree; sortable_tree = $(this).siblings('ol.sortable_tree'); $(this).find('input').val(''); list_item = sortable_tree.children('li:first').clone(); list_item.find('ol.nested_set').remove(); list_item.find('h4 > a').text(data.name); list_item.find('a.delete').attr('href', function(index, attr) { return attr.replace(list_item.data('node-id'), data.id); }); list_item.data('node-id', data.id); return list_item.appendTo(sortable_tree); }); $('a.delete').on('ajax:success', function() { return $('div.item').has($(this)).parent().remove(); }); return $('ol.sortable_tree h4 > a.edit').on('click', function(event) { var edit_box, edit_done, edit_link, h4, sortable_tree, text, url; event.preventDefault(); sortable_tree = $(this).parents('ol.sortable_tree'); edit_link = $(this); h4 = $(this).parent(); $(this).hide(); url = $(this).attr('href'); text = $(this).text(); edit_box = $(''); edit_box.appendTo(h4.parent()); edit_box.focus(); edit_done = function() { var attributes, data, hide_input_box, title_field; console.log(edit_box.val().trim().length); hide_input_box = function() { edit_link.show(); return edit_box.remove(); }; if (edit_box.val().trim().length > 0) { data = {}; attributes = {}; title_field = 'title'; if (sortable_tree.data('title')) { title_field = sortable_tree.data('title'); } attributes[title_field] = edit_box.val(); data[sortable_tree.data('model')] = attributes; return $.ajax({ type: 'PUT', data: data, url: edit_link.attr('href'), success: function() { edit_link.text(edit_box.val()); return hide_input_box(); }, error: hide_input_box }); } else { return hide_input_box(); } }; edit_box.on('keypress', function(event) { if (event.which === 13) { return $(this).trigger('focusout'); } }); return edit_box.on('focusout', edit_done); }); }); }).call(this); (function() { (function(factory) { if (typeof define === "function" && define.amd) { return define(["jquery"], factory); } else { return factory(jQuery); } })(function($) { var config, converted, decoded, pluses, raw; pluses = /\+/g; raw = function(s) { return s; }; decoded = function(s) { return decodeURIComponent(s.replace(pluses, " ")); }; converted = function(s) { if (s.indexOf("\"") === 0) { s = s.slice(1, -1).replace(/\\"/g, "\"").replace(/\\\\/g, "\\"); } try { return (config.json ? JSON.parse(s) : s); } catch (_error) {} }; config = $.cookie = function(key, value, options) { var cookie, cookies, days, decode, i, l, name, parts, result, t; if (value !== undefined) { options = $.extend({}, config.defaults, options); if (typeof options.expires === "number") { days = options.expires; t = options.expires = new Date(); t.setDate(t.getDate() + days); } value = (config.json ? JSON.stringify(value) : String(value)); return (document.cookie = [(config.raw ? key : encodeURIComponent(key)), "=", (config.raw ? value : encodeURIComponent(value)), (options.expires ? "; expires=" + options.expires.toUTCString() : ""), (options.path ? "; path=" + options.path : ""), (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "")].join("")); } decode = (config.raw ? raw : decoded); cookies = document.cookie.split("; "); result = (key ? undefined : {}); i = 0; l = cookies.length; while (i < l) { parts = cookies[i].split("="); name = decode(parts.shift()); cookie = decode(parts.join("=")); if (key && key === name) { result = converted(cookie); break; } if (!key) { result[name] = converted(cookie); } i++; } return result; }; config.defaults = {}; return $.removeCookie = function(key, options) { if ($.cookie(key) !== undefined) { $.cookie(key, '', $.extend({}, options, { expires: -1 })); return true; } return false; }; }); }).call(this); (function() { window.is_restorable_tree = true; this.TSTconst = (function() { function TSTconst() {} TSTconst._name = 'TST'; TSTconst.delimiter = ';'; TSTconst.separator = '|'; TSTconst.re = function() { return new RegExp(this.cookie_name() + '\\' + this.separator); }; TSTconst.hash_prefix = function() { return this.cookie_name() + this.separator; }; TSTconst.cookie_scope = function() { return $('.sortable_tree').data('cookie_scope') || $('.sortable_tree').data('cookie-scope'); }; TSTconst.cookie_name = function() { if (!this.cookie_scope()) { return this._name; } return this._name + '_' + this.cookie_scope(); }; return TSTconst; })(); this._get_hash = function() { return document.location.hash; }; this._set_hash = function(str) { window.skip_expandable_tree_hashchange = true; return document.location.hash = str; }; this._uniqueArray = function(arr) { var item, key, output, val, _i, _len, _results; if (arr == null) { arr = []; } output = {}; for (_i = 0, _len = arr.length; _i < _len; _i++) { item = arr[_i]; output[item] = item; } _results = []; for (key in output) { val = output[key]; _results.push(val); } return _results; }; this._compactArray = function(array) { return array.filter(function(e) { return e; }); }; this._nested_set_hash_arr = function(hash) { var arr, prefix, _ref; if (!hash) { return []; } _ref = hash.split(TSTconst.separator), prefix = _ref[0], arr = _ref[1]; return _compactArray(_uniqueArray(arr.split(TSTconst.delimiter))); }; this.nested_tree_get_path = function() { var hash; hash = _get_hash(); if (!hash.match(TSTconst.re())) { return false; } return _nested_set_hash_arr(hash); }; this.hash_and_cookie_accordance = function() { var hash, str; if (window.is_cookie_restoreable_tree) { hash = _get_hash(); if (hash.length === 0) { $.removeCookie(TSTconst.cookie_name()); } else { if (!hash.match(TSTconst.re())) { return false; } str = hash.split(TSTconst.separator)[1]; $.cookie(TSTconst.cookie_name(), str, { expires: 14 }); } } return false; }; this.nested_tree_path_remove = function(id) { var arr, hash, index, str; hash = _get_hash(); if (!hash.match(TSTconst.re())) { return false; } arr = _nested_set_hash_arr(hash); index = arr.indexOf(id + ''); if (index === -1) { return hash_and_cookie_accordance(); } arr.splice(index, 1); str = _uniqueArray(arr).join(TSTconst.delimiter); if (str.length === 0) { _set_hash(''); } else { _set_hash(TSTconst.hash_prefix() + str); } hash_and_cookie_accordance(); return true; }; this.nested_tree_path_add = function(id) { var arr, hash, str; str = id; hash = _get_hash(); if (hash.match(TSTconst.re())) { arr = _nested_set_hash_arr(hash); arr.push(id); arr = _uniqueArray(arr); str = arr.join(TSTconst.delimiter); } _set_hash(TSTconst.hash_prefix() + str); return hash_and_cookie_accordance(); }; this.load_nested_nodes = function(arr, expand_node_url) { var ctrl_items, id, node, tree; if (arr.length === 0) { window.skip_expandable_tree_hashchange = false; return false; } id = arr.shift(); tree = $('.sortable_tree'); node = $("[data-node-id=" + id + "]"); ctrl_items = $('i.handle, b.expand', tree); if (node.length === 0) { return load_nested_nodes(arr, expand_node_url); } else { return $.ajax({ type: 'POST', dataType: 'html', data: { id: id }, url: expand_node_url, beforeSend: function(xhr) { ctrl_items.hide(); return window.skip_expandable_tree_hashchange = true; }, success: function(data, status, xhr) { ctrl_items.show(); append_children_to_node(node, data); return load_nested_nodes(arr, expand_node_url); }, error: function(xhr, status, error) { try { return console.log(error); } catch (_error) {} } }); } }; this.restore_nested_tree = function(sortable_tree, expand_node_url) { var arr; arr = nested_tree_get_path(); if (!arr) { return false; } return load_nested_nodes(arr, expand_node_url); }; }).call(this); (function() { this._arrays_diff = function(a, b) { return b.filter(function(i) { return !(a.indexOf(i) > -1); }); }; this.expandable_tree_hashchange = function(hash_event) { var btn, diff_ids, id, new_arr, new_hash, new_url, oEvent, old_arr, old_hash, old_url, _i, _len, _results; if (window.skip_expandable_tree_hashchange) { window.skip_expandable_tree_hashchange = false; return true; } hash_and_cookie_accordance(); oEvent = hash_event.originalEvent; new_url = oEvent.newURL; old_url = oEvent.oldURL; new_hash = new_url.split('#')[1]; old_hash = old_url.split('#')[1]; if (!(('#' + new_hash).match(TSTconst.re()) || ('#' + old_hash).match(TSTconst.re()))) { return false; } new_arr = _nested_set_hash_arr(new_hash); old_arr = _nested_set_hash_arr(old_hash); diff_ids = new_arr.length >= old_arr.length ? _arrays_diff(old_arr, new_arr) : _arrays_diff(new_arr, old_arr); _results = []; for (_i = 0, _len = diff_ids.length; _i < _len; _i++) { id = diff_ids[_i]; btn = $("[data-node-id=" + id + "] > .item .expand"); btn.click(); _results.push(setTimeout(function() { return window.skip_expandable_tree_hashchange = false; })); } return _results; }; $(window).bind('hashchange', function(hash_event) { return expandable_tree_hashchange(hash_event); }); }).call(this); (function() { this.add_to_restorable_path = function(node) { var id; if (window.is_restorable_tree) { id = node.data('node-id'); nested_tree_path_add(id); return true; } return false; }; this.remove_from_restorable_path = function(node) { var id; if (window.is_restorable_tree) { id = node.data('node-id'); nested_tree_path_remove(id); return true; } return false; }; this.nested_tree_toggle = function(button) { if (button.hasClass('minus')) { return button.removeClass('minus').addClass('plus').html('+'); } else { return button.removeClass('plus').addClass('minus').html('–'); } }; this.append_children_to_node = function(node, html) { var button, item; html = html.trim(); item = node.children('.item'); button = node.children('.item').children('.expand'); if (html.length === 0) { button.addClass('empty'); } if (html.length > 0) { item.after(html); nested_tree_toggle(button); return add_to_restorable_path(node); } }; this.upload_nodes_children = function(node, expand_node_url) { var ctrl_items, node_id, tree; node_id = node.data('node-id'); tree = $('.sortable_tree'); ctrl_items = $('i.handle, b.expand', tree); return $.ajax({ type: 'POST', dataType: 'html', data: { id: node_id }, url: expand_node_url, beforeSend: function(xhr) { ctrl_items.hide(); return window.skip_expandable_tree_hashchange = true; }, success: function(data, status, xhr) { ctrl_items.show(); return append_children_to_node(node, data); }, error: function(xhr, status, error) { try { return console.log(error); } catch (_error) {} } }); }; this.init_expandable_tree = function() { var expand_node_url, sortable_tree, steps; sortable_tree = $('ol.sortable_tree'); if (sortable_tree.length === 0) { return false; } window.is_restorable_tree || (window.is_restorable_tree = false); window.is_cookie_restoreable_tree = sortable_tree.data('cookie_store') || sortable_tree.data('cookie-store'); if (window.is_cookie_restoreable_tree) { steps = $.cookie(TSTconst.cookie_name()); if (steps) { _set_hash(TSTconst.hash_prefix() + steps); } } expand_node_url = sortable_tree.data('expand_node_url') || sortable_tree.data('expand-node-url'); if (window.is_restorable_tree) { restore_nested_tree(sortable_tree, expand_node_url); } sortable_tree.on('click', '.expand.minus', function(e) { var button, node; button = $(this); node = button.parent().parent(); nested_tree_toggle(button); remove_from_restorable_path(node); node.children('.nested_set').hide(); return false; }); sortable_tree.on('click', '.expand.plus', function(e) { var button, nested_set, node; button = $(this); node = button.parent().parent(); nested_set = node.children('.nested_set'); if (nested_set.length === 0) { upload_nodes_children(node, expand_node_url); } else { nested_set.show(); nested_tree_toggle(button); add_to_restorable_path(node); } return false; }); return true; }; $(function() { return init_expandable_tree(); }); }).call(this); ;TI"required_assets_digest;TI"%33cc380674fe7f59b00ebf508dd2d2f2;FI" _version;TI"%3e3098c8a5e9a7f3b984ba95ac600637;F