editor1.js
2 years ago
handlebars.baldrick2.js
8 years ago
handlebars2.js
8 years ago
jquery.baldrick3.js
8 years ago
panel.js
8 years ago
scripts-pod_reference.js
2 years ago
scripts-view_template.js
8 years ago
jquery.baldrick3.js
415 lines
| 1 | /* BaldrickJS V2.1 | (C) David Cramer - 2013 | MIT License */ |
| 2 | (function ( $ ) { |
| 3 | |
| 4 | var baldrickCache = {}, baldrickhelpers = { |
| 5 | _plugins : {}, bind : {}, event : function ( el, e ) { |
| 6 | return el; |
| 7 | }, filter : function ( opts ) { |
| 8 | return opts; |
| 9 | }, target : function ( opts ) { |
| 10 | if ( opts.params.target ) { |
| 11 | opts.params.target[opts.params.targetInsert]( opts.data ); |
| 12 | if ( typeof opts.params.callback === 'string' ) { |
| 13 | if ( typeof window[opts.params.callback] === 'function' ) { |
| 14 | return window[opts.params.callback]( opts ); |
| 15 | } |
| 16 | } |
| 17 | else { |
| 18 | if ( typeof opts.params.callback === 'function' ) { |
| 19 | return opts.params.callback( opts ); |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | }, request : function ( opts ) { |
| 24 | if ( opts.request.url.indexOf( '#_cache_' ) > -1 ) { |
| 25 | if ( typeof baldrickCache[opts.request.url.split( '#_cache_' )[1]] === 'object' ) { |
| 26 | return {data : baldrickCache[opts.request.url.split( '#_cache_' )[1]]}; |
| 27 | } |
| 28 | } |
| 29 | return $.ajax( opts.request ); |
| 30 | }, xhr : function ( xhr, defaults, params ) { |
| 31 | //Upload progress |
| 32 | if ( params.trigger.data( 'progress' ) ) { |
| 33 | if ( $( params.trigger.data( 'progress' ) ).length > 0 ) { |
| 34 | //reset progress |
| 35 | var progress = $( params.trigger.data( 'progress' ) ); |
| 36 | progress.width( 0 ); |
| 37 | xhr.upload.addEventListener( "progress", function ( evt ) { |
| 38 | if ( evt.lengthComputable ) { |
| 39 | var percentComplete = evt.loaded / evt.total; |
| 40 | progress.width( percentComplete * 100 + '%' ); |
| 41 | } |
| 42 | }, false ); |
| 43 | //Download progress |
| 44 | xhr.addEventListener( "progress", function ( evt ) { |
| 45 | if ( evt.lengthComputable ) { |
| 46 | var percentComplete = evt.loaded / evt.total; |
| 47 | //Do something with download progress |
| 48 | progress.width( percentComplete * 100 + '%' ); |
| 49 | } |
| 50 | }, false ); |
| 51 | } |
| 52 | } |
| 53 | return xhr; |
| 54 | }, request_complete : function ( opts ) { |
| 55 | opts.params.complete( opts ); |
| 56 | opts.params.loadElement.removeClass( opts.params.loadClass ); |
| 57 | }, request_error : function ( opts ) { |
| 58 | opts.params.complete( opts.jqxhr, opts.textStatus ); |
| 59 | }, refresh : function ( opts, defaults ) { |
| 60 | $( defaults.triggerClass ).baldrick( defaults ); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | $.fn.baldrick = function ( opts ) { |
| 65 | |
| 66 | var triggerClass = this.selector, inst = this.not( '._tisBound' ); |
| 67 | |
| 68 | inst.addClass( '_tisBound' ); |
| 69 | if ( typeof opts !== 'undefined' ) { |
| 70 | if ( typeof opts.helper === 'object' ) { |
| 71 | baldrickhelpers._plugins._params_helpers_ = opts.helper; |
| 72 | } |
| 73 | } |
| 74 | var defaults = $.extend( true, opts, {helpers : baldrickhelpers}, {triggerClass : triggerClass} ), |
| 75 | ncb = function () { |
| 76 | return true; |
| 77 | }, callbacks = { |
| 78 | "before" : ncb, "callback" : false, "complete" : ncb, "error" : ncb |
| 79 | }, output; |
| 80 | |
| 81 | for ( var c in callbacks ) { |
| 82 | if ( typeof defaults[c] === 'string' ) { |
| 83 | callbacks[c] = (typeof window[defaults[c]] === 'function' ? window[defaults[c]] : ncb); |
| 84 | } |
| 85 | else { |
| 86 | if ( typeof defaults[c] === 'function' ) { |
| 87 | callbacks[c] = defaults[c]; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | var do_helper = function ( h, input, ev ) { |
| 92 | var out; |
| 93 | // pull in plugins before |
| 94 | for ( var before in defaults.helpers._plugins ) { |
| 95 | if ( typeof defaults.helpers._plugins[before][h] === 'function' ) { |
| 96 | out = defaults.helpers._plugins[before][h]( input, defaults, ev ); |
| 97 | if ( typeof out !== 'undefined' ) { |
| 98 | input = out; |
| 99 | } |
| 100 | if ( input === false ) { |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if ( typeof defaults.helpers[h] === 'function' ) { |
| 106 | out = defaults.helpers[h]( input, defaults, ev ); |
| 107 | if ( typeof out !== 'undefined' ) { |
| 108 | input = out; |
| 109 | } |
| 110 | if ( !input ) { |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | // pull in plugins after |
| 115 | for ( var after in defaults.helpers._plugins ) { |
| 116 | if ( typeof defaults.helpers._plugins[after]['after_' + h] === 'function' ) { |
| 117 | out = defaults.helpers._plugins[after]['after_' + h]( input, defaults, ev ); |
| 118 | if ( typeof out !== 'undefined' ) { |
| 119 | input = out; |
| 120 | } |
| 121 | if ( input === false ) { |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | return input; |
| 127 | }; |
| 128 | |
| 129 | inst = do_helper( 'bind', inst ); |
| 130 | if ( inst === false ) { |
| 131 | return this; |
| 132 | } |
| 133 | return do_helper( 'ready', inst.each( function ( key ) { |
| 134 | if ( !this.id ) { |
| 135 | this.id = "baldrick_trigger_" + (new Date().getTime() + key); |
| 136 | } |
| 137 | var el = $( this ), |
| 138 | ev = (el.data( 'event' ) ? el.data( 'event' ) : (defaults.event ? defaults.event : ( el.is( 'form' ) ? 'submit' : 'click' ))); |
| 139 | el.on( ev, function ( e ) { |
| 140 | |
| 141 | var tr = $( do_helper( 'event', this, e ) ); |
| 142 | |
| 143 | if ( tr.data( 'for' ) ) { |
| 144 | var fort = $( tr.data( 'for' ) ), datamerge = $.extend( {}, fort.data(), tr.data() ); |
| 145 | delete datamerge['for']; |
| 146 | fort.data( datamerge ); |
| 147 | return fort.trigger( (fort.data( 'event' ) ? fort.data( 'event' ) : ev) ); |
| 148 | } |
| 149 | if ( tr.is( 'form' ) && !tr.data( 'request' ) && tr.attr( 'action' ) ) { |
| 150 | tr.data( 'request', tr.attr( 'action' ) ); |
| 151 | } |
| 152 | if ( tr.is( 'a' ) && !tr.data( 'request' ) && tr.attr( 'href' ) ) { |
| 153 | if ( tr.attr( 'href' ).indexOf( '#' ) < 0 ) { |
| 154 | tr.data( 'request', tr.attr( 'href' ) ); |
| 155 | } |
| 156 | else { |
| 157 | tr.data( 'href', tr.attr( 'href' ) ); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if ( (tr.data( 'before' ) ? (typeof window[tr.data( 'before' )] === 'function' ? window[tr.data( 'before' )]( this, e ) : callbacks.before( this, e )) : callbacks.before( this, e )) === false ) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | var params = { |
| 166 | trigger : tr, |
| 167 | callback : (tr.data( 'callback' ) ? ((typeof window[tr.data( 'callback' )] === 'function') ? window[tr.data( 'callback' )] : tr.data( 'callback' )) : callbacks.callback), |
| 168 | method : (tr.data( 'method' ) ? tr.data( 'method' ) : (tr.attr( 'method' ) ? tr.attr( 'method' ) : (defaults.method ? defaults.method : 'GET'))), |
| 169 | dataType : (tr.data( 'type' ) ? tr.data( 'type' ) : (defaults.dataType ? defaults.dataType : false)), |
| 170 | target : (tr.data( 'target' ) ? $( tr.data( 'target' ) ) : (defaults.target ? $( defaults.target ) : $( '<html>' ))), |
| 171 | targetInsert : (tr.data( 'targetInsert' ) ? (tr.data( 'targetInsert' ) === 'replace' ? 'replaceWith' : tr.data( 'targetInsert' )) : (defaults.targetInsert ? (defaults.targetInsert === 'replace' ? 'replaceWith' : defaults.targetInsert) : 'html')), |
| 172 | loadClass : (tr.data( 'loadClass' ) ? tr.data( 'loadClass' ) : (defaults.loadClass ? defaults.loadClass : 'loading')), |
| 173 | activeClass : (tr.data( 'activeClass' ) ? tr.data( 'activeClass' ) : (defaults.activeClass ? defaults.activeClass : 'active')), |
| 174 | activeElement : (tr.data( 'activeElement' ) ? (tr.data( 'activeElement' ) === '_parent' ? tr.parent() : $( tr.data( 'activeElement' ) )) : (defaults.activeElement ? (defaults.activeElement === '_parent' ? tr.parent() : $( defaults.activeElement )) : tr)), |
| 175 | cache : (tr.data( 'cache' ) ? tr.data( 'cache' ) : (defaults.cache ? defaults.cache : false)), |
| 176 | complete : (tr.data( 'complete' ) ? (typeof window[tr.data( 'complete' )] === 'function' ? window[tr.data( 'complete' )] : callbacks.complete ) : callbacks.complete), |
| 177 | contentType : (tr.data( 'contentType' ) ? tr.data( 'contentType' ) : 'application/x-www-form-urlencoded; charset=UTF-8'), |
| 178 | processData : (tr.data( 'processData' ) ? tr.data( 'processData' ) : true), |
| 179 | resultSelector : false |
| 180 | }; |
| 181 | params.url = (tr.data( 'request' ) ? tr.data( 'request' ) : (defaults.request ? defaults.request : params.callback)); |
| 182 | params.loadElement = (tr.data( 'loadElement' ) ? $( tr.data( 'loadElement' ) ) : (defaults.loadElement ? ($( defaults.loadElement ) ? $( defaults.loadElement ) : params.target) : params.target)); |
| 183 | |
| 184 | params = do_helper( 'params', params ); |
| 185 | if ( params === false ) { |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | switch ( typeof params.url ) { |
| 190 | case 'function' : |
| 191 | return params.url( this, e ); |
| 192 | case 'boolean' : |
| 193 | case 'object': |
| 194 | return; |
| 195 | case 'string' : |
| 196 | if ( params.url.indexOf( ' ' ) > -1 ) { |
| 197 | var rp = params.url.split( ' ' ); |
| 198 | params.url = rp[0]; |
| 199 | params.resultSelector = rp[1]; |
| 200 | } |
| 201 | } |
| 202 | e.preventDefault(); |
| 203 | var active = (tr.data( 'group' ) ? $( '._tisBound[data-group="' + tr.data( 'group' ) + '"]' ).each( function () { |
| 204 | var or = $( this ), |
| 205 | tel = (or.data( 'activeElement' ) ? (or.data( 'activeElement' ) === '_parent' ? or.parent() : $( or.data( 'activeElement' ) )) : (defaults.activeElement ? (defaults.activeElement === '_parent' ? tr.parent() : $( defaults.activeElement )) : or) ); |
| 206 | tel.removeClass( (or.data( 'activeClass' ) ? or.data( 'activeClass' ) : (defaults.activeClass ? defaults.activeClass : params.activeClass)) ); |
| 207 | } ) : $( '._tisBound:not([data-group])' ).each( function () { |
| 208 | var or = $( this ), |
| 209 | tel = (or.data( 'activeElement' ) ? (or.data( 'activeElement' ) === '_parent' ? or.parent() : $( or.data( 'activeElement' ) )) : (defaults.activeElement ? (defaults.activeElement === '_parent' ? tr.parent() : $( defaults.activeElement )) : or) ); |
| 210 | tel.removeClass( (or.data( 'activeClass' ) ? or.data( 'activeClass' ) : (defaults.activeClass ? defaults.activeClass : params.activeClass)) ); |
| 211 | } )); |
| 212 | |
| 213 | params.activeElement.addClass( params.activeClass ); |
| 214 | params.loadElement.addClass( params.loadClass ); |
| 215 | |
| 216 | var data; |
| 217 | if ( FormData && ( tr.is( 'input:file' ) || ( tr.is( 'form' ) && params.method === 'POST') ) ) { |
| 218 | if ( tr.is( 'form' ) ) { |
| 219 | data = new FormData( tr[0] ); |
| 220 | if ( tr.find( 'input:file' ).length ) { |
| 221 | //Options to tell jQuery not to process data or worry about content-type. |
| 222 | params.method = 'POST'; |
| 223 | params.contentType = false; |
| 224 | } |
| 225 | } |
| 226 | else { |
| 227 | data = new FormData(); |
| 228 | } |
| 229 | params.processData = false; |
| 230 | params.cache = false; |
| 231 | |
| 232 | // make field vars |
| 233 | for ( var att in tr.data() ) { |
| 234 | //data.append('_'+att, tr.data(att)); |
| 235 | data.append( att, tr.data( att ) ); |
| 236 | } |
| 237 | // use input |
| 238 | if ( tr.is( 'input' ) ) { |
| 239 | if ( tr.is( 'input:file' ) ) { |
| 240 | if ( tr[0].files.length > 1 ) { |
| 241 | for ( var file = 0; file < tr[0].files.length; file++ ) { |
| 242 | data.append( tr.prop( 'name' ), tr[0].files[file] ); |
| 243 | } |
| 244 | } |
| 245 | else { |
| 246 | data.append( tr.prop( 'name' ), tr[0].files[0] ); |
| 247 | } |
| 248 | //Options to tell jQuery not to process data or worry about content-type. |
| 249 | params.method = 'POST'; |
| 250 | params.contentType = false; |
| 251 | //tr.wrap('<form>').parent('form').trigger('reset'); |
| 252 | //tr.unwrap(); |
| 253 | |
| 254 | } |
| 255 | else { |
| 256 | if ( tr.is( 'input:checkbox' ) || tr.is( 'input:radio' ) ) { |
| 257 | if ( tr.prop( 'checked' ) ) { |
| 258 | data.append( tr.prop( 'name' ), tr.val() ); |
| 259 | } |
| 260 | } |
| 261 | else { |
| 262 | data.append( tr.prop( 'name' ), tr.val() ); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | else { |
| 268 | |
| 269 | var sd = tr.serializeArray(), atts = tr.data(), param = []; |
| 270 | // insert user set params |
| 271 | if ( defaults.data ) { |
| 272 | atts = $.extend( defaults.data, atts ); |
| 273 | } |
| 274 | $.each( atts, function ( k, v ) { |
| 275 | param.push( {name : k, value : v} ); |
| 276 | } ); |
| 277 | if ( sd.length ) { |
| 278 | $.each( sd, function ( k, v ) { |
| 279 | param.push( v ); |
| 280 | } ); |
| 281 | } |
| 282 | data = $.param( param ); |
| 283 | } |
| 284 | //data = do_helper('data', params); |
| 285 | var request = { |
| 286 | url : params.url, |
| 287 | data : data, |
| 288 | cache : params.cache, |
| 289 | type : params.method, |
| 290 | contentType : params.contentType, |
| 291 | processData : params.processData, |
| 292 | xhr : function () { |
| 293 | var xhr = new window.XMLHttpRequest(); |
| 294 | return do_helper( 'xhr', xhr, params ); |
| 295 | }, |
| 296 | success : function ( dt, ts, xhr ) { |
| 297 | if ( params.resultSelector ) { |
| 298 | if ( typeof dt === 'object' ) { |
| 299 | var traverse = params.resultSelector.replace( /\[/g, '.' ).replace( /\]/g, '' ).split( '.' ), |
| 300 | data_object = dt; |
| 301 | for ( var i = 0; i < traverse.length; i++ ) { |
| 302 | data_object = data_object[traverse[i]]; |
| 303 | } |
| 304 | dt = data_object; |
| 305 | } |
| 306 | else { |
| 307 | if ( typeof dt === 'string' ) { |
| 308 | var tmp = $( params.resultSelector, $( '<html>' ).html( dt ) ); |
| 309 | if ( tmp.length === 1 ) { |
| 310 | dt = $( '<html>' ).html( tmp ).html(); |
| 311 | } |
| 312 | else { |
| 313 | dt = $( '<html>' ); |
| 314 | tmp.each( function () { |
| 315 | dt.append( this ); |
| 316 | } ); |
| 317 | dt = dt.html(); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | var rawdata = dt; |
| 323 | dt = do_helper( 'filter', {data : dt, rawData : rawdata, request : request, params : params} ); |
| 324 | do_helper( 'target', dt ); |
| 325 | }, |
| 326 | complete : function ( xhr, ts ) { |
| 327 | |
| 328 | do_helper( 'request_complete', { |
| 329 | jqxhr : xhr, textStatus : ts, request : request, params : params |
| 330 | } ); |
| 331 | |
| 332 | do_helper( 'refresh', {jqxhr : xhr, textStatus : ts, request : request, params : params} ); |
| 333 | |
| 334 | if ( tr.data( 'once' ) ) { |
| 335 | tr.off( ev ).removeClass( '_tisBound' ); |
| 336 | } |
| 337 | }, |
| 338 | error : function ( xhr, ts, ex ) { |
| 339 | do_helper( 'request_error', { |
| 340 | jqxhr : xhr, textStatus : ts, error : ex, request : request, params : params |
| 341 | } ); |
| 342 | } |
| 343 | }; |
| 344 | if ( params.dataType ) { |
| 345 | request.dataType = params.dataType; |
| 346 | } |
| 347 | request = do_helper( 'request_params', request, params ); |
| 348 | if ( request === false ) { |
| 349 | return inst; |
| 350 | } |
| 351 | |
| 352 | var request_result = do_helper( 'request', {request : request, params : params} ); |
| 353 | |
| 354 | if ( request_result.data ) { |
| 355 | //alert('hey?'); |
| 356 | var dt = request_result.data, rawdata = dt; |
| 357 | |
| 358 | do_helper( 'filter', {data : dt, rawData : rawdata, request : request, params : params} ); |
| 359 | do_helper( 'request_complete', { |
| 360 | jqxhr : false, textStatus : true, request : request, params : params |
| 361 | } ); |
| 362 | do_helper( 'refresh', {jqxhr : false, textStatus : true, request : request, params : params} ); |
| 363 | |
| 364 | } |
| 365 | } ); |
| 366 | if ( el.data( 'autoload' ) || el.data( 'poll' ) ) { |
| 367 | if ( el.data( 'delay' ) ) { |
| 368 | setTimeout( function ( el, ev ) { |
| 369 | return el.trigger( ev ); |
| 370 | }, el.data( 'delay' ), el, ev ); |
| 371 | } |
| 372 | else { |
| 373 | el.trigger( ev ); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | if ( el.data( 'poll' ) ) { |
| 378 | if ( el.data( 'delay' ) ) { |
| 379 | setTimeout( function ( el, ev ) { |
| 380 | return setInterval( function ( el, ev ) { |
| 381 | return el.trigger( ev ); |
| 382 | }, el.data( 'poll' ), el, ev ); |
| 383 | }, el.data( 'delay' ) ); |
| 384 | } |
| 385 | else { |
| 386 | setInterval( function ( el, ev ) { |
| 387 | return el.trigger( ev ); |
| 388 | }, el.data( 'poll' ), el, ev ); |
| 389 | } |
| 390 | } |
| 391 | return this; |
| 392 | } ) ); |
| 393 | }; |
| 394 | $.fn.baldrick.cacheObject = function ( id, object ) { |
| 395 | baldrickCache[id] = object; |
| 396 | }; |
| 397 | $.fn.baldrick.registerhelper = function ( slug, helper, callback ) { |
| 398 | var newhelper = {}; |
| 399 | if ( typeof helper === 'object' ) { |
| 400 | newhelper[slug] = helper; |
| 401 | baldrickhelpers._plugins = $.extend( true, newhelper, baldrickhelpers._plugins ); |
| 402 | } |
| 403 | else { |
| 404 | if ( typeof helper === 'string' && typeof slug === 'string' && typeof callback === 'function' ) { |
| 405 | newhelper[helper] = {}; |
| 406 | newhelper[helper][slug] = callback; |
| 407 | baldrickhelpers._plugins = $.extend( true, newhelper, baldrickhelpers._plugins ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | }; |
| 412 | jQuery( function ( $ ) { |
| 413 | $( '.baldrick' ).baldrick(); |
| 414 | } ); |
| 415 | })( jQuery ); |