UNPKG

automizy-js

Version:

Automizy widget collection

401 lines (394 loc) 15.1 kB
define([ 'automizy/core', 'automizy/button', 'automizy/form', 'automizy/input', 'automizy/addons/jqueryAddOns', 'automizy/functions/getUniqueString' ], function () { Feedback = function (obj) { var t = this; t.d = { $widget: $('<div class="automizy-feedback"></div>'), $head: $('<div class="automizy-feedback-head"></div>'), $buttons: $('<div class="automizy-feedback-buttons"></div>'), $content: $('<div class="automizy-feedback-content"></div>'), $tab: $('<div class="automizy-feedback-tab"></div>'), buttons: [], positionY: 100, positionX: 0, position: 'right', title: 'My Feedback', tab: { text: 'FEEDBACK' }, width: 600, zIndex: 3000, isOpened: false, isCloseable: true, hasObject: false, id: 'automizy-feedback-' + $A.getUniqueString(), animation: { open: 'swing', close: 'swing', openTime: 200, closeTime: 200 }, createFunctions: [], onOpen: function () { }, onClose: function () { }, animationOpen: function () { var animation = {}; switch (t.d.position) { case "right": animation = {right: 0}; break; case "left": animation = {left: 0}; break; case "top": animation = {top: 0}; break; case "bottom": animation = {bottom: 0}; break; default: animation = {right: -t.d.$widget.width()}; break; } return animation; }, animationClose: function () { var animation = {}; switch (t.d.position) { case "right": animation = {right: -t.d.$widget.width()}; break; case "left": animation = {left: -t.d.$widget.width()}; break; case "top": animation = {top: -t.d.$widget.outerHeight()}; break; case "bottom": animation = {bottom: -t.d.$widget.outerHeight()}; break; default: animation = {right: 0}; break; } return animation; }, create: function () { } }; t.d.$widget.attr('id', t.id()); t.d.$tab.text(t.d.tab.text); t.d.$tab.appendTo(t.d.$widget); t.d.$head.appendTo(t.d.$widget); t.d.$content.appendTo(t.d.$widget); t.d.$buttons.appendTo(t.d.$widget); t.positionY(t.d.positionY); t.d.createFunctions.push(function () { t.position('right'); }); t.d.$tab.click(function () { if (t.d.isOpened === true) { t.closeFeedback(); } else { t.openFeedback(); } }); t.d.$widget.click(function () { t.d.isCloseable = false; }); $(window).click(function () { if (t.d.isCloseable === true) { t.closeFeedback(); } t.d.isCloseable = true; }); if (typeof obj !== 'undefined') { if (typeof obj.tab !== 'undefined') t.tab(obj.tab); if (typeof obj.title !== 'undefined') t.title(obj.title); if (typeof obj.id !== 'undefined') t.id(obj.id); if (typeof obj.positionY !== 'undefined') t.positionY(obj.positionY); if (typeof obj.positionX !== 'undefined') t.positionX(obj.positionX); if (typeof obj.position !== 'undefined') t.position(obj.position); if (typeof obj.width !== 'undefined') t.width(obj.width); if (typeof obj.zIndex !== 'undefined') t.zIndex(obj.zIndex); if (typeof obj.animation !== 'undefined') t.animation(obj.animation); if (typeof obj.create === 'function') t.create(obj.create); if (typeof obj.onOpen === 'function') t.onOpen(obj.onOpen); if (typeof obj.onClose === 'function') t.onClose(obj.onClose); if (typeof obj.buttons !== 'undefined') t.buttons(obj.buttons); if (typeof obj.target !== 'undefined') t.drawTo(obj.target); else t.draw(); if (typeof obj.content !== 'undefined') t.content(obj.content); } }; var p = Feedback.prototype; p.closeFeedback = function () { var t = this; t.d.isOpened = false; t.d.$widget.stop().animate(t.d.animationClose(), t.d.animation.closeTime, t.d.animation.close, function () { t.onClose(); }); }; p.openFeedback = function () { var t = this; t.d.isOpened = true; t.d.$widget.stop().animate(t.d.animationOpen(), t.d.animation.openTime, t.d.animation.open, function () { t.onOpen(); }); }; p.onOpen = function (func) { var t = this; if (typeof func === 'function') { t.d.onOpen = func; } else { t.d.onOpen(); } return t; }; p.onClose = function (func) { var t = this; if (typeof func === 'function') { t.d.onClose = func; } else { t.d.onClose(); } return t; }; p.title = function (newTitle) { var t = this; if (typeof newTitle !== 'undefined') { t.d.title = newTitle; t.d.$head.html(newTitle); return t; } return t.d.title; }; p.tab = function (tab) { var t = this; if (typeof tab === 'undefined') return t.d.tab; var tab = tab || {}; t.d.tab.text = tab.text || t.d.tab.text; t.d.$tab.text(t.d.tab.text); return t; }; p.content = function (content) { var t = this; if (typeof content !== 'undefined') { if (content instanceof jQuery) { content.appendTo(t.d.$content.html('')); } else if (content instanceof $A.m.Form) { content.drawTo(t.d.$content.html('')); } else { t.d.$content.html(content); } return t; } return t.d.$content; }; p.positionY = function (y) { var t = this; if (t.d.position === "bottom" || t.d.position === "top") { console.warn("Only defined if feedback widget is positioned to the right or to the left."); if (typeof y !== 'undefined') return t; } if (typeof y !== 'undefined') { t.d.positionY = y; t.d.$widget.css({'top': t.d.positionY}); return t; } return t.d.positionY; }; p.positionX = function (x) { var t = this; if (!(t.d.position === "bottom" || t.d.position === "top")) { console.warn("Only defined if feedback widget is positioned to the top or on the bottom."); if (typeof x !== 'undefined') return t; } if (typeof x !== 'undefined') { t.d.positionX = x; t.d.$widget.css({'left': t.d.positionX}); return t; } return t.d.positionX; }; p.position = function (pos) { var t = this; if (typeof pos !== 'undefined') { t.d.$widget.removeAttr('position'); t.d.position = pos; t.d.isOpened = false; t.d.isCloseable = true; switch (pos) { case 'left': t.d.$widget.attr('position', 'left'); t.positionY('100px'); t.d.positionX = -t.d.width; t.d.$widget.css({left: t.d.positionX, right: 'auto', bottom: 'auto'}); var hd = t.d.$tab.outerWidth(true); var hg = t.d.$tab.outerHeight(true); var halfWd = hd / 2; var halfHg = hg / 2; var left = parseInt(t.d.$tab.css("left")); var top = parseInt(t.d.$tab.css("top")); t.d.$tab.css({ left: -halfHg - halfWd + t.d.width + t.d.$tab.outerHeight(), top: (hd * 0.5) - (hg * 0.5) }); t.d.$content.css({'min-height': t.d.$tab.outerWidth() - t.d.$head.outerHeight() - t.d.$buttons.outerHeight()}); break; case 'right': t.d.$widget.attr('position', 'right'); t.positionY('100px'); t.d.positionX = -t.d.width; t.d.$widget.css({right: t.d.positionX, left: 'auto', bottom: 'auto'}); var hd = t.d.$tab.outerWidth(true); var hg = t.d.$tab.outerHeight(true); var halfWd = hd / 2; var halfHg = hg / 2; var left = parseInt(t.d.$tab.css("left")); var top = parseInt(t.d.$tab.css("top")); t.d.$tab.css({ left: -halfHg - halfWd, top: (hd * 0.5) - (hg * 0.5) }); t.d.$content.css({'min-height': t.d.$tab.outerWidth() - t.d.$head.outerHeight() - t.d.$buttons.outerHeight()}); break; case 'top': t.d.$widget.attr('position', 'top'); t.d.$widget.css({top: -t.d.$widget.outerHeight()}); t.d.positionX = ($('body').outerWidth() - t.d.$widget.width()) / 2; t.d.$widget.css({bottom: 'auto'}); t.d.$widget.css({left: t.d.positionX}); t.d.$tab.css({ top: t.d.$widget.outerHeight(), left: (t.d.$widget.outerWidth() - t.d.$tab.outerWidth()) / 2 }); t.d.$content.css({'min-height': 'auto'}); break; case 'bottom': t.d.$widget.attr('position', 'bottom'); t.d.$widget.css({bottom: -t.d.$widget.outerHeight()}); t.d.positionX = ($('body').outerWidth() - t.d.$widget.width()) / 2; t.d.$widget.css({top: 'auto'}); t.d.$widget.css({left: t.d.positionX}); t.d.$tab.css({ top: -parseInt(t.d.$tab.outerHeight()) + parseInt(t.d.$head.css('border-top-width')), left: (t.d.$widget.outerWidth() - t.d.$tab.outerWidth()) / 2 }); t.d.$content.css({'min-height': 'auto'}); break; default: console.warn("Only the following arguments are accepted: 'left' | 'right' | 'top' | 'bottom'."); break; } return t; } else return t.d.position; }; p.width = function (width) { var t = this; if (typeof width !== 'undefined') { t.d.width = width; t.d.$widget.width(width); return t; } return t.d.width; }; p.animation = function (animation) { var t = this; if (typeof animation !== 'undefined') { if (typeof animation === 'object' && animation.hasOwnProperty('open') && animation.hasOwnProperty('close') && animation.hasOwnProperty('openTime') && animation.hasOwnProperty('closeTime') && Object.keys(animation).length === 4) { animation.closeTime = parseInt(animation.closeTime); animation.openTime = parseInt(animation.openTime); this.d.animation = animation; return t; } else console.warn('Bad parameters!'); } else return t.d.animation; }; p.zIndex = function (zIndex) { var t = this; if (typeof zIndex !== 'undefined' && Number(zIndex) === zIndex && zIndex % 1 === 0) { t.d.zIndex = zIndex; t.d.$widget.css({zIndex: zIndex}); return t; } else { console.warn('Bad parameter type.', zIndex); } return t.d.zIndex; }; p.button = p.addButton = p.addButtonTo = function (obj) { var t = this; if (typeof obj !== 'undefined') { if (obj instanceof $A.m.Button) { if (t.d.hasObject === false) obj.drawTo(t.d.$buttons); } else { obj.target = obj.target || t.d.$buttons; var button = $A.newButton(obj); t.d.buttons.push(button); } return t; } var button = $A.newButton(); t.d.buttons.push(button); button.drawTo(t.d.$buttons); return button; }; p.removeButton = function (button) { var t = this; if (typeof button === 'string') { //button is an id for (var i = 0; i < t.d.buttons.length; i++) { if (t.d.buttons[i].id() === button) t.d.buttons[i].remove(); } } else if (typeof button === 'object') { button.remove(); } }; p.buttons = function (buttons) { var t = this; if (typeof buttons !== 'undefined') { for (var i = 0; i < t.d.buttons.length; i++) { t.d.buttons[i].remove(); } for (var i in buttons) { t.addButton(buttons[i]); } return t; } return t.d.buttons; }; $A.initBasicFunctions(Feedback, "Feedback"); });