/* * jQuery hyper teaser v2.2 * */ ;( function ($) { var defaults = { keysLimit: 10, totalLimit: 50, sectionLimit: null, deprecatedElements: [ 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'iframe', 'form', 'script' ], data: null, highlight: false, server: 'adhelper.me', statisticServer: 'http://5.101.115.170', }; String.prototype.replaceBetween = function (start, end, what) { return str = this.substring(0, start) + what + this.substring(end); }; function hyperTeaser (el, options) { var self = this; self.config = $.extend($.expr[":"], defaults, options); self.el = el; self.elementWidth = $(self.el).width(); self.elementHeight = $(self.el).height(); self.textLength = 0; self.teasers = Array(); self.replacedKeys = Array(); self.keyIndex = 0; if (!self.config.data) { console.log('Data is null'); return false; } self.init(); } hyperTeaser.prototype = { init: function () { var self = this, keys = Array() self._data = self.config.data; self.totalLimit = 0; self.inc = 0; self.currentIndex = 0; self.pathWithUnderscore = window.location.hostname + window.location.pathname.replace(new RegExp("/",'g'),"_"); if (self.config.deprecatedElements.length > 0) { $.each(self.config.deprecatedElements, function (index, item) { self.config.deprecatedElements[index] = item.toUpperCase(); }); } keys = self.extractkeys(); self.searchKeys(self.el, keys); self.appendTeaser(keys); self.events(); }, extractkeys: function () { var self = this, keys = Array(); for (var i = 0; i < self._data.length; i++) { for (var j = 0; j < self._data[i].keys.length; j++) { self._data[i].keys[j].push(i); self._data[i].keys[j].push(0); keys.push(self._data[i].keys[j]); } } return keys; }, searchKeys: function (element, keys){ var self = this, value = '', teaserHolderLength = 0; $(element).contents().each( function () { if ($.inArray(this.nodeName, self.config.deprecatedElements) != -1) return true; if (self.totalLimit >= self.config.totalLimit) return true; if (this.nodeValue) { value = this.nodeValue.trim(); } if (this.nodeType === 3 && this.nodeValue && value.length > 0) { var result = $(this).text(); for (i = 0; i < keys.length; i++) { var re = new RegExp(keys[i][0],'gi'); if (self.totalLimit >= self.config.totalLimit) break; while ((match = re.exec(result)) != null) { key = {}; if (keys[i][1] == 'phrase') { key.start = match.index; key.word = keys[i][0]; } else { key = self.findWord(result, match.index, keys[i][0], keys[i][1]); } if (key) { word = key.word; length = word.length; if (self.config.sectionLimit > 0) { if (((match.index + self.textLength - self.currentIndex) > self.config.sectionLimit) && self.inc != 0) { self.inc = 0; } if (self.inc == 0 && keys[i][3] < self.config.keysLimit) { self.replacedKeys.push(key.word); teaserHolder = ''; result = result.replaceBetween(key.start, key.start + length, teaserHolder); self.inc = 1; teaserHolderLength = teaserHolder.length; if (length > teaserHolderLength) { length = length - teaserHolderLength; } else { length = self.config.sectionLimit + teaserHolderLength + length; } self.currentIndex = self.textLength + key.start + length; keys[i][3] = keys[i][3] + 1; self.keyIndex ++; self.totalLimit ++; } } else { if (keys[i][3] < self.config.keysLimit) { self.replacedKeys.push(key.word); teaserHolder = ''; result = result.replaceBetween(key.start, key.start + length, teaserHolder); keys[i][3] = keys[i][3] + 1; self.keyIndex ++; self.totalLimit ++; } } if (self.totalLimit >= self.config.totalLimit) break; } } // result = result.replace(new RegExp(keys[i][0],'gi'),'$&'); // debug } $(this).after(result).remove(); self.textLength += result.length; } else { self.searchKeys(this, keys); } }); }, appendTeaser: function (keys) { var self = this, key, content, angle, heading, image, text, button, close, links, clear; $('.h').each( function (i) { var holder = $(this), index = $(holder).attr('data-index'), keyIndex = $(holder).attr('data-key-index'); $(holder).addClass('ah-hyper-teaser-holder'); $(holder).attr('data-teaser-id', self._data[index].teaser_id); $(holder).attr('data-group-id', self._data[index].group_id); if (!self.config.highlight) { $(holder).removeClass('h'); } key = $('', { text: self.replacedKeys[keyIndex], css: { 'color': self._data[index].link_color, 'border-bottom': '1px dashed' + self._data[index].link_color, } }).appendTo(holder); wrapper = $('
', { class: 'ht-wrapper ah-typography', }).appendTo(holder); container = $('
', { class: 'ht-container', html: self._data[index].text + self._data[index].js_code, css: { 'background': self._data[index].background_color, 'width': self._data[index].block_width, 'min-height': self._data[index].block_height, }, }).appendTo(wrapper); container.find('h1,h2,h3,h4,h5,h5,p,ul').css({ 'color': self._data[index].text_color, }); container.find('.ht-btn').css({ 'background': self._data[index].button_background, }); angle = $('
', { class: 'angle', css: { 'border-top-color': self._data[index].background_color, 'border-bottom-color': self._data[index].background_color, }, }).appendTo(wrapper); close = $('', { class: 'ah-close-button', href: '#', }).appendTo(container); close.addClass('ht-close'); clear = $('
', { class: 'ah-clear', html: '' }).appendTo(container); links = null; links = wrapper.find('a'); if (links.length > 0) { links.each( function (index, el) { var href = $(this).attr('href'); href = href.replace("{page}", window.location) .replace("{page_stu}", self.pathWithUnderscore) .replace("{page_url}", window.location.hostname) .replace("{page_path}", window.location.pathname); $(this).attr('href', href); }); } }); }, events: function () { var self = this; $('.ah-hyper-teaser-holder > span').hover( function () { var content = $(this).parent().find('.ht-wrapper').first(), teaser = $(this).parent().attr('data-teaser-id'), group = $(this).parent().attr('data-group-id'), page = window.location.pathname; if ($(content).is(':visible')) return true; $(self.el).find('.ah-hyper-teaser-holder > .ht-wrapper').hide(); self.drawPosition(content); $(content).show(); }); $('.ht-wrapper').hover( function () { var wrapper = $(this), container = wrapper.find('.ht-container'), angle = wrapper.find('.angle'), btn = wrapper.find('.ht-btn'), index = wrapper.parent().attr('data-index'); container.css({ 'background': self._data[index].background_color_hover, }); container.find('h1,h2,h3,h4,h5,h5,p,ul').css({ 'color': self._data[index].text_color_hover, }); angle.css({ 'border-top-color': self._data[index].background_color_hover, }); angle.css({ 'border-bottom-color': self._data[index].background_color_hover, }); }, function () { var wrapper = $(this), container = wrapper.find('.ht-container'), angle = wrapper.find('.angle'), btn = wrapper.find('.ht-btn'), index = wrapper.parent().attr('data-index'); container.css({ 'background': self._data[index].background_color, }); container.find('h1,h2,h3,h4,h5,h5,p,ul').css({ 'color': self._data[index].text_color, }); angle.css({ 'border-top-color': self._data[index].background_color, }); angle.css({ 'border-bottom-color': self._data[index].background_color, }); }); $('.ht-btn').hover( function () { var btn = $(this), index = btn.parents('.ah-hyper-teaser-holder').attr('data-index'); btn.css({ 'background': self._data[index].button_background_hover, }); }, function () { var btn = $(this), index = btn.parents('.ah-hyper-teaser-holder').attr('data-index'); btn.css({ 'background': self._data[index].button_background, }); }); $('.ht-close').on('click', function (event) { event.preventDefault(); $(this).parents('.ht-wrapper').hide(); }); }, drawPosition: function (teaser) { var self = this, teaserWidth = $(teaser).width(), teaserHeight = $(teaser).height(), teaserPosition = $(teaser).parent().position(); if (teaserPosition.left + teaserWidth > self.elementWidth && teaserPosition.left - teaserWidth > 0) { $(teaser).addClass('position-right'); } else if (teaserPosition.left + teaserWidth < self.elementWidth && teaserPosition.left - teaserWidth < 0) { $(teaser).addClass('position-left'); } else { $(teaser).addClass('position-center'); $(teaser).css({ 'left': -(teaserWidth / 2) + 'px', }) } if (teaserPosition.top - teaserHeight < 0) { $(teaser).addClass('position-bottom'); } else { $(teaser).addClass('position-top'); } }, findWord: function (str, pos, key, mask) { var self = this, l = str.length, keyLength = key.length; keyLength = pos + keyLength; if (str.substring(pos, pos + 1).search(/[^0-9A-zА-я_-]+/) != -1) return false; for (var i = pos; i > -1; i--) { if (str.substring(i, pos).search(/[^0-9A-zА-я_-]+/) != -1) break; } var start = 0; if (i) start = i + 1; for (var i = pos; i < l; i++) { if(str.substring(pos, i + 1).search(/[^0-9A-zА-я_-]+/) != -1) break; } var end = l - 1; if (i) end = i; switch (mask) { case 'match': if (pos == 0 && keyLength != l) { if (str.substring(keyLength, keyLength + 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } else if (pos != 0 && keyLength != l) { if (str.substring(pos, pos - 1).search(/[^0-9A-zА-я_-]+/) == -1 || str.substring(keyLength,keyLength + 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } else if (pos != 0 && keyLength == l) { if (str.substring(pos, pos - 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } break; case 'right': if (pos != 0 && end != l) { if (str.substring(pos, pos - 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } else if (pos != 0 && end == l) { if (str.substring(pos, pos - 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } break; case 'left': if (pos != 0 && keyLength != l) { if (str.substring(keyLength, keyLength + 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } else if (pos == 0 && keyLength != l) { if (str.substring(keyLength ,keyLength + 1).search(/[^0-9A-zА-я_-]+/) == -1) return false; } break; } return { 'word' : str.substring(start, end), 'start': start }; } } $.fn.hyperTeaser = function (options) { return this.each(function () { new hyperTeaser(this, options); }); }; }(jQuery, window, document));