1/** 2 * Jquery plugin pro obsluhu FEA widgetu. 3 * 4 * Pri inciialziaci lze zadat nastaveni, popripade se dokaze nacist z data atributu. 5 * Akce lze provadet Jquery syntaxy 6 * 7 * $('[data-fea-widget]').feaWidget({renderUrl: 'aaa'}).feaWidget('activate'); 8 * 9 * @param {type} $ 10 * @param {type} FEA 11 * @param {type} window 12 * @param {type} document 13 * @param {type} undefined 14 * @returns {undefined} 15 */ 16;(function($, FEA, window, document, undefined) { 17 18'use strict'; 19 20/** @constant */ 21var 22 FEA_WIDGET_PROPERTY = '__fea_widget', 23 FEA_WIDGET_SELECTOR = 'data-fea-widget' 24 ; 25 26// constructor 27var FeaWidget = function() { 28 /** hlavni obalovaci element*/ 29 var $el = null, 30 /** nasatveni */ 31 settings = {}, 32 /** aktualni stav */ 33 state 34 ; 35}; 36 37// static 38 39/** Typy */ 40FeaWidget.TypeEdit = { 41 IFRAME: 'iframe', 42 FEAFORM: 'feaform', 43 EVENT: 'event' 44}; 45 46/**Vychozi hodnoty pro nastaveni */ 47FeaWidget.defaults = { 48 'formUrl': '/admin/fea/widgets/form/name/%name%?options=%options%', 49 'renderUrl': '/admin/fea/widgets/render/name/%name%?options=%options%', 50 'clsActive': 'fea-widget-active', 51 'cls': 'fea-widget', 52 'typeEdit': FeaWidget.TypeEdit.IFRAME 53}; 54 55FeaWidget.prototype = { 56 init: function(element, options ) { 57 var me = this; 58 me.$el = $(element); 59 60 me.settings = $.extend( {}, FeaWidget.defaults, me.$el.data() , options ); 61 62 me.$el.addClass(me.settings['cls']); 63 64 if ( !me.settings.name) { 65 throw "FEA Widget: widget must have name!"; 66 } 67 68 if ( me.settings.hasOwnProperty('state') && FEA.isAllowedState(me.settings['state']) ) { 69 me.$el.attr(FEA_WIDGET_SELECTOR, me.settings['state']); 70 } 71 72 me.state = me.$el.attr(FEA_WIDGET_SELECTOR); 73 74 if ( !FEA.isAllowedState(me.state) ) { 75 me.$el.attr(FEA_WIDGET_SELECTOR, me.state = FEA.State.READONLY); 76 } 77 78 switch ( me.state ) { 79 case FEA.State.READONLY: 80 return; 81 break; 82 case FEA.State.ACTIVE: 83 this.activate(); 84 break; 85 } 86 }, 87 activate: function() { 88 var me = this; 89 90 if ( me.state !== FEA.State.ACTIVE ) { 91 me.$el.attr(FEA_WIDGET_SELECTOR, me.state = FEA.State.ACTIVE); 92 me.$el.addClass(me.settings['clsActive']); 93 94 //TODO moznosti volby 95 me.$el.on('click', $.proxy(me.edit, me)); 96 } 97 }, 98 deactivate: function() { 99 var me = this; 100 101 if ( me.state !== FEA.State.PASSIVE ) { 102 103 me.$el.attr(FEA_WIDGET_SELECTOR, me.state = FEA.State.PASSIVE); 104 me.$el.removeClass(me.settings['clsActive']); 105 me.$el.off('click'); 106 107 switch( me.settings['typeEdit'] ) 108 { 109 case FeaWidget.TypeEdit.IFRAME: 110 me._closeWindow(); 111 break; 112 case FeaWidget.TypeEdit.FEAFORM: 113 me.$el.find('[data-fea-form]').fea('cancelEdit'); 114 break; 115 case FeaWidget.TypeEdit.EVENT: 116 throw "Not implementted yet"; 117 break; 118 default: 119 throw 'Edit type "' + me.settings['typeEdit'] + '" is not implemented'; 120 break; 121 } 122 123 } 124 }, 125 edit: function(e) { 126 e.preventDefault(); 127 e.stopPropagation(); 128 129 var me = this; 130 131 switch( me.settings['typeEdit'] ) 132 { 133 case FeaWidget.TypeEdit.IFRAME: 134 me._openWindowIframe( me._resolveUrl(me.settings['formUrl']) ); 135 break; 136 case FeaWidget.TypeEdit.FEAFORM: 137 me.$el.find('[data-fea-form]').fea('startEdit'); 138 break; 139 case FeaWidget.TypeEdit.EVENT: 140 throw "Not implementted yet"; 141 break; 142 default: 143 throw 'Edit type "' + me.settings['typeEdit'] + '" is not implemented'; 144 break; 145 } 146 }, 147 revert: function(){ 148 var me = this; 149 150 me.refresh(); 151 me.deactivate(); 152 }, 153 refresh: function(){ 154 var me = this; 155 FEA.helpers.componentLoading(me.$el, true); 156 157 $.ajax({ 158 url: me._resolveUrl(me.settings['renderUrl']), 159 dataType: 'html', 160 complete: function() { 161 FEA.helpers.componentLoading(me.$el, false); 162 }, 163 success: function(html) { 164 me.$el.html(html); 165 } 166 }); 167 }, 168 _resolveUrl: function(url) { 169 170 var me = this, 171 vars = $.extend({}, {name: me.settings.name, options: JSON.stringify(me.settings.var)}, me.settings.var), 172 _var; 173 174 for( _var in vars ) { 175 vars[_var] = encodeURIComponent(vars[_var]); 176 } 177 178 return FEA.helpers.varsReplace(url, vars, true); 179 }, 180 _openWindow: function(html) { 181 var me = this; 182 183 me.$win = $( 184 '<div class="fea-widget-window modal" tabindex="-1" role="dialog" aria-hidden="true">' + 185 '<div class="modal-dialog" role="document">' + 186 '<div class="modal-content">' + 187 '<div class="modal-header">' + 188 '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' + 189 '</div>' + 190 '<div class="modal-body">'+ html + '</div>' + 191 '</div>'+ 192 '</div>'+ 193 '</div>' 194 ); 195 196 me.$win.appendTo('body'); 197 198 me.$win.modal('show'); 199 200 me.$win.bind('hide.bs.modal', function(){ 201 me.refresh(); 202 me._removeWindow(); 203 }); 204 205 return me.$win; 206 }, 207 _openWindowIframe: function(url) { 208 var me = this, 209 html = '<iframe class="fea-remote-content" src="'+url+'"></iframe>', 210 win = me._openWindow(html), 211 iframe = win.find('iframe'),
212 body = win.find('.modal-body'); 213 214 FEA.helpers.componentLoading(body, true); 215 216 iframe.bind('load', function(){ 217 FEA.helpers.componentLoading(body, false); 218 }); 219 220 iframe.iFrameResize({ 221 }); 222 }, 223 _removeWindow: function(){ 224 var me = this; 225 226 if ( me.$win ) { 227 me.$win.remove(); 228 me.$win = undefined; 229 } 230 }, 231 _closeWindow: function(){ 232 var me = this; 233 234 if ( me.$win ) { 235 me.$win.modal('hide'); 236 me._removeWindow(); 237 } 238 } 239 240}; 241 242$.fn.extend({ 243 /** 244 * 245 */ 246 feaWidget: function(options) { 247 var action = 'init', 248 element = this; 249 250 if ($.type(options) === 'string' ) { 251 action = options; 252 options = Array.prototype.splice.call(arguments, 1); 253 } 254 255 // DESTROY action 256 if( 'destroy' === action ) { 257 console.error('[FeaWidget] action "destroy" is not implemented yet!'); 258 return element; 259 } 260 261 // INIT action 262 if( 'init' === action ) { 263 element.each(function(){ 264 var el = $(this); 265 if( el.data(FEA_WIDGET_PROPERTY) ) { 266 //return el.data(FEA_WIDGET_PROPERTY); 267 } else { 268 var FeaWidgetInstance = new FeaWidget(); 269 FeaWidgetInstance.init(this, options); 270 el.data(FEA_WIDGET_PROPERTY, FeaWidgetInstance); 271 } 272 }); 273 } else { 274 element.each(function(){ 275 var el = $(this); 276 if( el.data(FEA_WIDGET_PROPERTY) ) { 277 var instance = el.data(FEA_WIDGET_PROPERTY); 278 279 if ( typeof (instance[action]) === 'function' ) { 280 instance[action].apply(instance, options); 281 } 282 } 283 else { 284 console.error('[FeaWidget] instance not exist'); 285 } 286 }); 287 } 288 289 return element; 290 } 291 292}); 293 294$.feaWidget = FeaWidget; 295 296})(jQuery, $.fea, window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.