standard-controls.js
147 lines
| 1 | (function($){ |
| 2 | |
| 3 | if ( !$.factory ) $.factory = {} |
| 4 | if ( $.factory.widget ) return; |
| 5 | |
| 6 | /** |
| 7 | * OnePress Widget Factory. |
| 8 | */ |
| 9 | $.factory.widget = function (pluginName, pluginObject) { |
| 10 | |
| 11 | var factory = { |
| 12 | |
| 13 | createWidget: function (element, options) { |
| 14 | var widget = $.extend(true, {}, pluginObject); |
| 15 | |
| 16 | widget.element = $(element); |
| 17 | widget.options = $.extend(true, widget.options, options); |
| 18 | |
| 19 | if (widget._init) widget._init(); |
| 20 | if (widget._create) widget._create(); |
| 21 | |
| 22 | $.data(element, 'plugin_' + pluginName, widget); |
| 23 | }, |
| 24 | |
| 25 | callMethod: function (widget, methodName) { |
| 26 | widget[methodName] && widget[methodName](); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | $.fn[pluginName] = function () { |
| 31 | var args = arguments; |
| 32 | var argsCount = arguments.length; |
| 33 | |
| 34 | this.each(function () { |
| 35 | |
| 36 | var widget = $.data(this, 'plugin_' + pluginName); |
| 37 | |
| 38 | // a widget is not created yet |
| 39 | if (!widget && argsCount <= 1) { |
| 40 | factory.createWidget(this, argsCount ? args[0] : false); |
| 41 | |
| 42 | // a widget is created, the public method with no args is being called |
| 43 | } else if (argsCount == 1) { |
| 44 | factory.callMethod(widget, args[0]); |
| 45 | } |
| 46 | }); |
| 47 | }; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Radio #1 |
| 52 | */ |
| 53 | |
| 54 | $.fn.onpRadioCircles = function( options ) { |
| 55 | if ( !options ) options = {}; |
| 56 | options.theme = 'circles'; |
| 57 | $(this).onpRadio(options); |
| 58 | }; |
| 59 | |
| 60 | $.fn.onpRadioSquares = function( options ) { |
| 61 | if ( !options ) options = {}; |
| 62 | options.theme = 'squares'; |
| 63 | $(this).onpRadio(options); |
| 64 | }; |
| 65 | |
| 66 | $.factory.widget('onpRadio', { |
| 67 | options: { |
| 68 | theme: 'squares', |
| 69 | selected: null |
| 70 | }, |
| 71 | _create: function() { |
| 72 | var self = this; |
| 73 | this._createMarkup(); |
| 74 | |
| 75 | this.radio.find('.onp-radio-option').click(function(){ |
| 76 | if ( $(this).is(".disabled") ) return; |
| 77 | var selected = self.radio.find('.onp-radio-option.selected'); |
| 78 | if ( selected.data('value') == $(this).data('value')) return; |
| 79 | |
| 80 | selected.removeClass('selected'); |
| 81 | $(this).addClass("selected"); |
| 82 | |
| 83 | var value = $(this).data('value'); |
| 84 | self.element.val(value); |
| 85 | self.element.trigger( "change", $(this).data('value') ); |
| 86 | }); |
| 87 | }, |
| 88 | _createMarkup: function() { |
| 89 | |
| 90 | var wrap = $("<ul class='onp-radio-wrap'></ul>").addClass('onp-radio-' + this.options.theme); |
| 91 | this.element.find("option").each(function(){ |
| 92 | var $this = $(this); |
| 93 | |
| 94 | var value = $this.attr('value'); |
| 95 | var text = $this.html(); |
| 96 | var icon = $this.data('icon'); |
| 97 | var disabled = $this.attr('disabled'); |
| 98 | |
| 99 | var option = $("<li class='onp-radio-option' data-value='" + value + "'></li>"); |
| 100 | var innerOptionWrap = $("<span class='onp-radio-option-inner-wrap'></span>").appendTo(option); |
| 101 | option.addClass('onp-radio-option-' + value); |
| 102 | |
| 103 | if ( icon ) innerOptionWrap.append("<i class='" + icon + "'></i>"); |
| 104 | if ( disabled ) option.addClass('disabled'); |
| 105 | |
| 106 | wrap.append(option); |
| 107 | }); |
| 108 | |
| 109 | this.options.selected = this.options.selected || this.element.find("option:selected").attr('value'); |
| 110 | wrap.find('.onp-radio-option-' + this.options.selected).addClass("selected"); |
| 111 | |
| 112 | this.element.hide(); |
| 113 | this.element.after(wrap); |
| 114 | this.radio = wrap; |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | var factoryForms = { |
| 119 | |
| 120 | collapsedGroups: function( $target ) { |
| 121 | if ( !$target ) $target = $("body"); |
| 122 | |
| 123 | $target.find(".fy-collapsed-show").click(function(){ |
| 124 | $( $(this).attr('href') ).fadeIn(); |
| 125 | $(this).hide(); |
| 126 | return false; |
| 127 | }); |
| 128 | |
| 129 | $target.find(".fy-collapsed-hide").click(function(){ |
| 130 | var content = $( $(this).attr('href') ); |
| 131 | content.fadeOut(300, function(){ |
| 132 | content.prev().show(); |
| 133 | }); |
| 134 | return false; |
| 135 | }); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | $(function(){ |
| 140 | $(".onp-radio-circles.auto").onpRadioCircles(); |
| 141 | $(".onp-radio-squares.auto").onpRadioSquares(); |
| 142 | factoryForms.collapsedGroups(); |
| 143 | }); |
| 144 | |
| 145 | })(jQuery) |
| 146 | |
| 147 |