(function() { define(["../restful", "utils", 'shared/product', "t!/views/menu-aim.html", "v/jquery.menu-aim"], function(_restful, _utils, _sharedProduct, _template) { var Menu; Menu = (function() { function Menu() { this.initElement(); this.initTemplate(_template); } Menu.prototype.initElement = function() { this.elements = { menu: $("#main-menu"), leaveAll: false, leaveAllTimeout: 1500, categorySubmenu: $('#category_submenu') }; this.bindEvent(); return this.initSubmenu(); }; Menu.prototype.initTemplate = function(text) { return this.template = { d_submenu: Handlebars.compile($(text).find('[name="submenu"]').val()) }; }; Menu.prototype.initSubmenu = function() { var self; self = this; return _restful.get('product/category/tree', function(data) { var secondSubmenuData; secondSubmenuData = self.getSecondSubmenuData(data); self.generalSecondSubmenuDom(secondSubmenuData); return self.beautySubmenu(); }); }; Menu.prototype.getSecondSubmenuData = function(data) { var first, second, subData, _i, _j, _len, _len1, _ref; subData = []; for (_i = 0, _len = data.length; _i < _len; _i++) { first = data[_i]; if (!first.child_count) { continue; } _ref = first.children; for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { second = _ref[_j]; if (second.child_count) { subData.push(second); } } } return subData; }; Menu.prototype.getColnum = function(number) { if (number instanceof Array) { number = number.length; } if (number > 12) { return 4; } if (number > 6) { return 3; } return 2; }; Menu.prototype.estimatePopmenuWidth = function(node) { var colWidth, maxWidth; colWidth = 210; maxWidth = colWidth * Math.ceil(node.all_child_count / 15); return maxWidth = Math.min(colWidth * 3, maxWidth); }; Menu.prototype.generalNextSubmenuDom = function(nodes) { var hasChildren, html, klass, node, self, tag, _i, _len, _ref; self = this; html = ''; for (_i = 0, _len = nodes.length; _i < _len; _i++) { node = nodes[_i]; tag = node.deep === 2 ? 'div' : 'li'; hasChildren = ((_ref = node.children) != null ? _ref.length : void 0) > 0; klass = hasChildren ? ' hasChildren' : ' noChildren'; html += "<" + tag + " class='deep_" + (node.deep - 1) + klass + "'>"; html += _utils.format("{0}", node["title_" + _utils.language]); if (hasChildren) { html += "'; html += ""; } html += ""; } return html; }; Menu.prototype.generalSecondSubmenuDom = function(data) { var $submenu, html, index, maxWidth, nodes, self, t_submenu, _i, _len; self = this; t_submenu = this.template.d_submenu; nodes = []; for (_i = 0, _len = data.length; _i < _len; _i++) { index = data[_i]; maxWidth = self.estimatePopmenuWidth(index); if (!index.child_count) { continue; } $submenu = $(t_submenu(index)); $submenu.css('width', maxWidth); html = self.generalNextSubmenuDom(index.children); $submenu.find('div').html(html); nodes.push($submenu); } self.elements.categorySubmenu.append(nodes); }; Menu.prototype.bindEvent = function() { var $menu, self; self = this; $menu = this.elements.menu; $menu.children('li').children('h2').click(function() { var brotherDom; if (self.elements.show) { self.elements.show.hide('slow'); } brotherDom = $(this).next('div'); if (/^(\s)+$/.test(brotherDom.text())) { return; } $(this).parent('li').siblings().children('div').slideUp('fast'); return brotherDom.slideToggle('fast'); }); return $menu.find('ul').each(function() { if ($(this).find('li').length) { return $(this).menuAim({ activate: function(row) { return self.activateSubmenu(row); }, deactivate: function(row) { return self.deactivateSubmenu(row); }, exitMenu: function() { self.elements.leaveAll = true; setTimeout(function() { if (self.elements.leaveAll && self.elements.show) { return self.elements.show.hide('slow'); } }, self.elements.leaveAllTimeout); return false; } }); } }); }; Menu.prototype.deactivateSubmenu = function(row) { var submenuId; submenuId = "#" + ($(row).data('submenuId')); return $(submenuId).hide(); }; Menu.prototype.activateSubmenu = function(row) { var $menu, $row, $sub, css, left, self, submenuId, top; self = this; $row = $(row); submenuId = "#" + ($row.data('submenuId')); $sub = $(submenuId); if (!$sub.length) { return; } self.elements.show = $sub; $menu = this.elements.menu; left = $menu.width() + $menu.offset().left + 4; top = self.beautyTop($row); css = { left: left, top: top, "z-index": 999999 }; $sub.one('mouseleave', function() { return $sub.hide('slow'); }); $sub.one('mouseenter', function() { return self.elements.leaveAll = false; }); return $sub.css(css).show(); }; Menu.prototype.beautyTop = function($row) { var $menu, $menu_li, $menu_li_title, $submenu, defaultTop, menu_height, menu_li_height, menu_li_top, menu_top, row_height, row_top, subMenu_height; $menu = this.elements.menu; $submenu = this.elements.show; $menu_li = $row.parents('li').first(); menu_li_height = $menu_li.height(); $menu_li_title = $menu_li.find('h2:first'); defaultTop = $menu.offset().top; menu_height = $menu.height(); menu_top = $menu.offset().top; subMenu_height = $submenu.height(); row_top = $row.offset().top; row_height = $row.height(); menu_li_top = $menu_li.offset().top + $menu_li_title.outerHeight(); if (((row_top + row_height + (subMenu_height / 2)) < menu_height) && ((subMenu_height / 2) < (row_top - menu_top))) { return (row_top + row_height / 2) - (subMenu_height / 2); } if (menu_li_top + subMenu_height < menu_height) { if (menu_li_height <= subMenu_height) { return menu_li_top; } return row_top; } if (menu_li_top + menu_li_height - subMenu_height > defaultTop) { if (menu_li_height <= subMenu_height) { return menu_li_top + menu_li_height - subMenu_height; } return row_top - subMenu_height + row_height; } return defaultTop; }; Menu.prototype.beautySubmenu = function() { var menus, self; self = this; menus = this.elements.categorySubmenu.children(); return menus.each(function() { var area, area_height, cols, item, node, _i, _len; cols = self.doBeauty($(this)); node = $(this); area_height = 0; for (_i = 0, _len = cols.length; _i < _len; _i++) { item = cols[_i]; if (item.top > area_height) { area_height = item.top; } } area = { height: area_height, width: cols[cols.length - 1]['left'] }; node.css({ width: area.width, height: area.height + node.height() }); return node.children('div:first').css(area); }); }; Menu.prototype.doBeauty = function(node) { var cols, currentLeft, currentTop, h3, heightRang, self; self = this; h3 = node.children('h3').first(); heightRang = self.isInRange([0, 400]); cols = [ { left: 0, top: 0 } ]; currentTop = 0; currentLeft = 0; node.children('div:first').children('div').each(function() { var cLength, sHeight, sWidth, submenu; submenu = $(this); cLength = cols.length; sHeight = submenu.height() + 10; sWidth = submenu.width(); if (!heightRang(sHeight + currentTop)) { self.setPosition(submenu, cols[cLength - 1]['left'], 0); currentTop = sHeight; cols.push({ left: cols[cLength - 1]['left'] + sWidth, top: sHeight }); return; } if (currentTop === 0) { cols.push({ left: cols[cLength - 1]['left'] + sWidth, top: sHeight }); } else { cols[cLength - 1] = { left: Math.max(cols[cLength - 1]['left'], cols[cLength - 2]['left'] + sWidth), top: currentTop + sHeight }; } currentLeft = cols[cols.length - 2]["left"]; submenu.css({ top: currentTop, left: currentLeft }); return currentTop = currentTop + sHeight; }); return cols; }; Menu.prototype.setPosition = function(menu, left, top) { return menu.css({ top: top, left: left }); }; Menu.prototype.isInRange = function(range) { return function(number) { if (number < range[1]) { return true; } return false; }; }; return Menu; })(); return $(document).ready(function() { return new Menu(); }); }); }).call(this);