diff --git a/assets/js/codiad.js b/assets/js/codiad.js new file mode 100644 index 0000000..e9635e1 --- /dev/null +++ b/assets/js/codiad.js @@ -0,0 +1,4 @@ +( function( global, $ ) { + + var codiad = global.codiad = {}; +})( this, jQuery ); \ No newline at end of file diff --git a/assets/js/common.js b/assets/js/common.js new file mode 100644 index 0000000..e69de29 diff --git a/components/events/events.js b/assets/js/events.js similarity index 100% rename from components/events/events.js rename to assets/js/events.js diff --git a/assets/js/forms.js b/assets/js/forms.js new file mode 100644 index 0000000..67d1318 --- /dev/null +++ b/assets/js/forms.js @@ -0,0 +1,1725 @@ +( function( global, $ ) { + + // Define core + let codiad = global.codiad, + scripts = document.getElementsByTagName( 'script' ), + path = scripts[scripts.length-1].src.split( '?' )[0], + curpath = path.split( '/' ).slice( 0, -1 ).join( '/' ) + '/'; + + codiad.forms = function( instance = null ) { + + let _i = this; + + this.page = 0; + this.saving = false; + this.topics = {}; + + + //Sub objects + this.m = { + + conditional: function( o = {} ) { + + let self = this; + this.form = _i; + this.parent = null; + this.subject = null; + this.valid_actions = [ + "change_type", + "hide", + "show", + ]; + this.values = []; + + this.change_type = function() { + + let p = self.parent; + + if( p.type === self.type ) { + + return; + } + + if( p.element ) { + + let c = p.element.parent().parent(); + let element = p.element; + + element.detach(); + element.off(); + + if( c.length ) { + + c.remove(); + } + p.element = null; + } + + p.type = self.type; + + let a = self.form.m.create( p ); + + self.parent.replace( a ); + + if( self.parent.previous.element.parent().length ) { + + self.form.v.add( self.parent, null, self.parent.previous.element, null, true, false ); + } else if( c.element.parent().length ) { + + self.form.v.add( self.parent, c.element, null, null, true, false ); + } else { + + self.form.v.add( self.parent, null, null, null, true, false ); + } + }; + this.check = function() { + + let value = null; + let total = self.values.length; + let pass = false; + + if( ! self.parent.element ) { + + console.log( self.parent.element ); + return; + } + + if( typeof self.subject === "object" ) { + + value = self.subject.value; + } else if( typeof self.subject === "function" ) { + + value = self.subject(); + } + + for( let i = 0;i < total;i++ ) { + + if( self.values[i].type ) { + + self.type = self.values[i].type; + } else { + + self.type = self.parent.type; + } + + if( value === self.values[i].value || `${value}` === `${self.values[i].value}` ) { + + pass = true; + } else { + + pass = false; + } + + if( pass ) { + + if( self.valid_actions.includes( self.values[i].true ) ) { + + self[self.values[i].true](); + } else if( typeof values[i].action === "function" ) { + + self.values[i].true(); + } + } else { + + if( self.valid_actions.includes( self.values[i].false ) ) { + + self[self.values[i].false](); + } else if( typeof self.values[i].false === "function" ) { + + self.values[i].false(); + } + } + } + }; + this.hide = function() { + + self.form.v.remove( this.parent ); + }; + this.init = function( o ) { + + $.each( o, function( key, value ) { + + if( typeof o[key] === "object" && o[key] !== null && ! Array.isArray( o[key] ) ) { + + self[key] = $.extend( true, self[key], value ); + } else { + + self[key] = value; + } + }); + }; + this.show = function() { + + let i = true; + let j = false; + + while( i === true ) { + + if( j === false ) { + + j = self.parent.previous; + } else { + + j = j.previous; + } + + if( j === null ) { + + i = false; + self.form.v.add( self.parent, null, null, null, true ); + break; + } + + if( j.element.parent().length ) { + + i = false; + self.form.v.add( self.parent, j.element.parent(), null, null, true ); + break; + } else if( j.parent.element && j.parent.element.parent().length ) { + + i = false; + self.form.v.add( self.parent, j.parent.element.parent(), null, null, true ); + break; + } + } + }; + + self.init( o ); + }, + data: {}, + modal_data: function() { + + let self = this; + this.class_names = {}; + this.conditions = {}; + this.css = {}; + this.default = ""; + this.description = null; + this.element = null; + this.form = _i; + this.key = null; + this.label = ""; + this.listeners = { + + change: [], + update_modal: [], + update_view: [], + }; + this.name = ""; + this.next = null; + this.options = {}; + this.page = 0; + this.parent = _i.m.data; + this.placeholder = ""; + this.previous = null; + this.render = null; + this.required = false; + this.shown_by_default = true; + this.subdescription = null; + this.subfields = {}; + this.subscriptions = []; + this.subtitle = null; + this.title = null; + this.type = "text"; + this.update = null; + this.validation = null; + this.value = ""; + + this.clone = function() { + + return _i.m.clone( self ); + }; + this.conditionals = function() { + + let total = self.subscriptions.length; + for( let i = 0;i < total;i++ ) { + + self.subscriptions[i]( self ); + } + }; + this.publish = function() { + + let total = self.subscriptions.length; + + for( let i = 0;i < total;i++ ) { + + self.subscriptions[i]( self ); + } + }; + this.remove = function() { + + _i.v.remove( self ); + _i.m.remove( self, _i.m.data ); + }; + this.replace = function( i, o = {} ) { + + let restricted = [ + "next", + "parent", + "previous", + ]; + + if( typeof i === "object" ) { + + $.each( i, function( key, value ) { + + if( ! restricted.includes( key ) ) { + + self[key] = value; + } + }); + } else if( typeof i === "string" ) { + + self[i] = o; + } + }; + this.subscribe = function( subscription ) { + + if( typeof subscription !== "function" ) { + + return; + } + + let index = self.subscriptions.indexOf( subscription ); + + if( index < 0 ) { + + self.subscriptions.push( subscription ); + } + }; + this.unsubscribe = function( subscription ) { + + let index = self.subscriptions.indexOf( subscription ); + + if( index >= 0 ) { + + self.subscription.splice( index, 1 ); + } + }; + }, + parent: _i, + requirements: { + + default: [ + "default", + ], + repeatable: [ + "default", + "subfields", + ], + sortable: [ + "default", + ["options", "subfields"], + ], + }, + v: _i.v, + + init: function() { + + let _this = _i.m; + }, + create: function( o ) { + + let _this = _i.m; + + if( typeof o !== "object" ) { + + throw new Error({ + message: "Creation of modal_data requires data of type Object", + data_recieved: o, + type_recieved: ( typeof o ), + }); + } + + if( ! o.type ) { + + throw new Error({ + message: "Creation of modal_data requires type to be specified in the passed object", + data_recieved: o, + type_recieved: ( typeof o.type ), + }); + } + + if( _this.requirements[`${o.type}`] ) { + + let keys = _this.requirements[`${o.type}`]; + let total = keys.length; + let pass = false; + + for( let i = 0;i < total;i++ ) { + + if( typeof keys[i] === "string" ) { + + if( o[keys[i]] === undefined ) { + + reject({ + message: "Error, requirements to create object not met.", + required_fields: o.requirements[`${o.type}`], + }); + } + } else if( Array.isArray( keys[i] ) ) { + + let inner_total = keys[i].length; + pass = false; + + for( let j = 0;j < total;j++ ) { + + if( o[keys[i]] !== undefined ) { + + pass = true; + break; + } + } + + if( ! pass ) { + + reject({ + message: "Error, requirements to create object not met.", + required_fields: o.requirements[`${o.type}`], + }); + } + } + } + } + + let a = new _this.modal_data(); + + a.get_value = function() { + + a.update(); + return a.value; + }; + a.listeners.change = [function( o, e ) { + + o.update(); + }]; + $.each( o, function( key, value ) { + + a[key] = value; + }); + a.value = a.default; + + switch( true ) { + + case( a.type === "checkbox" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + a.update = function() { + + a.value = !!a.element.prop( "checked" ); + a.conditionals(); + }; + a.render = function() { + + a.element.prop( "checked", a.value ); + }; + break; + + case( a.type === "checkboxes" ): + + if( ! a.element ) { + + a.element = $( "
" ); + } + + a.update = function() { + + let v = codiad.common.get_check_box_values( a.name ); + a.value = v; + a.conditionals(); + }; + a.render = function() { + + let _this = a; + let checks = a.element.find( ':checkbox' ); + + a.element.html( '' ); + $.each( a.options, function( id, value ) { + + let label = $( `` ); + let check = $( `` ); + label.append( check ); + label.append( `${id}` ); + + if( _this.value.includes( value ) ) { + + check.prop( "checked", true ); + } else { + + check.prop( "checked", false ); + } + _i.v.add_listeners( _this, check ); + _this.element.append( label ); + }); + }; + a.get_value = function() { + + a.update(); + return a.value; + }; + break; + + case( a.type === "date" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + break; + + case( a.type === "datetime" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + break; + + case( a.type === "editor" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + a.element.text( a.value ); + a.label += "Please make sure to ( Edit->Select All ) then ( Format->Clear Formatting ) after pasting any content into this editor."; + a.update = function() { + + let e = a.element.tinymce(); + a.value = e.getContent(); + }; + a.render = function() { + + let _this = a; + let e = a.element.tinymce(); + + if( ! e ) { + + a.element.tinymce({ + //theme: 'modern', + width: '100%', + height: 300, + plugins: [ + 'advlist autolink link image lists charmap print hr anchor pagebreak spellchecker', + 'searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking', + 'save table directionality emoticons template paste' + ], + content_style: 'body { font-size: 12pt; font-family: "Open Sans", sans-serif; }', + toolbar: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons', + relative_urls: false, + convert_urls: true, + }); + e = a.element.tinymce(); + } + + if( ! _this.value ) { + + _this.value = ""; + } + + setTimeout( function() { + + e.render(); + setTimeout( function() { + + e.setContent( _this.value ); + e.on( "change", _this.update ); + }, 500 ); + }, 500 ); + }; + break; + + case( a.type === "email" ): + + if( ! o.element ) { + + a.element = $( `` ); + } + + if( ! a.validation ) { + + a.validation = _i.validation.validate; + } + break; + + case( a.type === "hidden" ): + + if( ! o.element ) { + + a.element = $( `` ); + } + break; + + case( a.type === "number" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + if( ! a.validation ) { + + a.validation = _i.validation.validate; + } + break; + + case( a.type === "phone" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + if( ! a.validation ) { + + a.validation = _i.validation.validate; + } + break; + + case( _i.m.is_repeatable( a ) ): + + if( ! a.element ) { + + if( a.type === "sortable" ) { + + a.element = $( `` ); + } else { + + a.element = $( `
` ); + } + } + + a.value = []; + + a.add = function( o = {} ) { + + let _r = a; + let subs = {}; + + subs.data = {}; + subs.element = $( `
` ); + subs.type = "repeatable-data"; + + let previous = null; + + $.each( _r.subfields, function( key, value ) { + + let object = _i.m.clone( value ); + if( o[key] !== undefined && o[key] !== null ) { + + object.default = o[key]; + } + + if( object.element ) { + + object.element = object.element.clone(); + object.element.detach(); + object.element.off(); + } + + subs.data[key] = _i.m.create( object ); + subs.data[key].parent = subs; + subs.data[key].previous = previous; + subs.data[key].key = key; + + if( previous !== null ) { + + previous.next = subs.data[key]; + } + previous = subs.data[key]; + }); + + $.each( o, function( key, value ) { + + if( typeof value !== "object" && Object.keys( subs ).includes( key ) ) { + + subs.data[key].value = value; + } else if( typeof subs[key] === "object" && typeof value === "object" && value !== null ) { + + subs.data[key] = $.extend( subs.data[key], value ); + } else if( typeof value === "object" && value !== null ) { + + subs.data[key] = value; + } else { + + console.log( "Unknown add field action", key, value ); + } + }); + subs.parent = _r; + subs.remove = function() { + + let total = subs.parent.value.length; + + for( let i = 0;i < total;i++ ) { + + if( subs.parent.value[i] === subs ) { + + subs.parent.value.splice( i, 1 ); + } + } + _r.render(); + }; + subs.render = function() { + + let _this = subs; + _this.element.off(); + _this.element.detach(); + _this.element.html( "" ); + + console.log( _this.element, _this.element[0].isConnected ); + if( ! _this.element[0].isConnected ) { + + _this.parent.element.append( _this.element ); + } + + $.each( _this.data, function( key, value ) { + + value.element.off(); + value.element.detach(); + value.element.html( "" ); + _i.v.add( value, _this.element ); + }); + + let remove = $( `Remove` ); + remove.addClass( 'button' ); + remove.on( 'click', function( e ) { + + _this.remove(); + }); + _this.element.append( remove ); + }; + + _i.m.conditionals( [], subs.data ); + _r.value.push( subs ); + }; + a.get_value = function() { + + let _this = a; + let data = []; + let total = a.value.length; + + for( let i = 0;i < total;i++ ) { + + let object = {}; + $.each( a.value[i].data, function( key, value ) { + + object[key] = value.get_value(); + }); + data.push( object ); + } + return data; + }; + a.remove = function( i ) { + + a.value[i].remove(); + return a; + }; + a.render = function() { + + a.element.html( "" ); + let total = a.value.length; + + for( let i = 0;i < total;i++ ) { + + a.value[i].render(); + } + + if( a.element.parent().find( '[data-type="repeatable-add"]' ).length < 1 ) { + + let add = $( `${o.repeat_label}` ); + add.addClass( 'button' ); + add.on( 'click', function( e ) { + + a.add(); + a.value[(a.value.length - 1)].render(); + }); + a.element.parent().append( add ); + } + }; + a.update = function() {}; + + if( a.default ) { + + let total = a.default.length; + for( let i = 0;i < total;i++ ) { + + a.add( a.default[i] ); + } + } + break; + + case( a.type === "select" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + a.render = function() { + + a.element.html( '' ); + let options = {}; + let _this = a; + + if( typeof a.options === "function" ) { + + options = a.options( o ); + } else { + + options = a.options; + } + + $.each( options, function( id, value ) { + + let option = $( `` ); + _this.element.append( option ); + }); + a.element.val( a.value ); + }; + break; + + case( a.type === "ssn" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + if( ! a.validation ) { + + a.validation = _i.validation.validate; + } + break; + + case( a.type === "sortable" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + break; + + case( a.type === "weekday" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + a.render = function() { + + a.element.html( '' ); + let options = { + + "Sunday": "sunday", + "Monday": "monday", + "Tuesday": "tuesday", + "Wednesday": "wednesday", + "Thursday": "thursday", + "Friday": "friday", + "Sunday": "sunday", + }; + let _this = a; + + $.each( options, function( id, value ) { + + let option = $( `` ); + _this.element.append( option ); + }); + a.element.val( a.value ); + }; + break; + + case( a.type === "year" ): + + if( ! a.element ) { + + a.element = $( `` ); + } + + if( ! a.validation ) { + + a.validation = _i.validation.validate; + } + break; + + default: + + if( ! a.element ) { + + a.element = $( `` ); + } + break; + } + + if( typeof a.update !== "function" ) { + + a.update = function() { + + a.value = a.element.val(); + a.conditionals(); + } + } + + if( typeof a.render !== "function" ) { + + a.render = function() { + + a.element.val( a.value ); + } + } + return a; + }, + clone: function( o ) { + + return $.extend( true, {}, o ); + }, + conditionals: function( filter = [], data = null ) { + + let _this = this; + let total_filters = filter.length; + let root_keys = Object.keys( this.data ); + + if( data === null ) { + + data = this.data; + } + + let keys = Object.keys( data ); + + $.each( data, function( key, value ) { + + if( total_filters > 0 && filter.inlcudes( value ) ) { + + return; + } + + $.each( value.conditions, function( k, v ) { + + let c = new _this.conditional( v ); + + c.parent = value; + + if( keys.includes( k ) ) { + + if( c.subject === null ) { + + c.subject = data[k]; + } + } else { + + if( root_keys.includes( k ) ) { + + if( c.subject === null ) { + + c.subject = data[k]; + } + } + } + c.subject.subscribe( c.check ); + value.conditions[k] = c; + }); + }); + }, + get_value: function( o ) { + + let _this = _i.m; + let data = null; + + if( typeof o === "string" ) { + + o = _i.m.data[o]; + } + return o.get_value(); + }, + get_values: function( o = null ) { + + let _this = _i.m; + let data = {}; + + if( o === null ) { + + o = this.data; + } + + $.each( o, function( key, value ) { + + data[key] = _this.get_value( value ); + }); + return data; + }, + is_repeatable: function( o ) { + + return ( o.type === "repeatable" || ( o.type === "sortable" && Object.keys( o.subfields ).length ) ); + }, + merge: function( ...objects ) { + + let o = objects[0]; + let total = objects.length; + + for( let i = 0;i < total;i++ ) { + + $.extend( true, o, objects[i] ); + } + return o; + }, + remove: function( o, data ) { + + $.each( data, function( key, value ) { + + if( value === o ) { + + _i.v.remove( value ); + data[key] = undefined; + delete data[key]; + } + }); + }, + set_data: function() {}, + update: function() {}, + }; + this.publish = function( topic, info ) { + + let _this = _i; + + // If the topic doesn't exist, or there's no listeners in queue, just leave + if( ! _this.topics.hasOwnProperty.call( _this.topics, topic ) ) { + + return; + } + + // Cycle through topics queue, fire! + _this.topics[topic].forEach( function( item ) { + + item( info !== undefined ? info : {} ); + }); + }; + this.subscribe = function( topic, listener ) { + + let _this = _i; + let add = true; + + // Create the topic's object if not yet created + if( ! _this.topics.hasOwnProperty.call( _this.topics, topic ) ) { + + _this.topics[topic] = []; + } + + // Add the listener to queue if not already in it. + for( let i = _this.topics[topic].length;i--; ) { + + if( _this.topics[topic][i] == listener ) { + + add = false; + break; + } + } + + if( add ) { + + let index = _this.topics[topic].push( listener ) - 1; + } + + // Provide handle back for removal of topic + return { + remove: function() { + + delete _this.topics[topic][index]; + } + }; + }; + this.v = { + + checking_conditions: false, + container: null, + controls: null, + m: _i.m, + message: null, + parent: _i, + + init: function() { + + let _this = this; + + if( _this.container === null ) { + + _this.container = $( "
" ); + } + + if( _this.message === null ) { + + _this.message = $( "
" ); + } + }, + add: function( o, c = null, previous = null, next = null, show = false, conditions = true ) { + + let _this = _i.v; + + if( typeof o !== "object" ) { + + throw new Error({ + message: "Creation of form view object requires data of type modal_data", + data_recieved: o, + type_recieved: ( typeof o ), + }); + } + + let connected = ( !!o.element ) ? o.element.parent().length : false; + + if( connected || ( ! o.shown_by_default && ! show ) ) { + + return; + } + + if( c === null ) { + + c = this.container; + } + + _this.add_listeners( o ); + + let container = $( '
' ); + let label = $( `` ); + let text = ( !!o.label ) ? o.label : ' '; + let repeatable = _i.m.is_repeatable( o ); + + let title = null; + let description = null; + let subtitle = null; + let subdescription = null; + let instructions = null; + + if( typeof o.title === "function" ) { + + title = o.title( o, container, label ); + } else if( o.title ) { + + if( ! $( c ).find( `#${o.key}_title` ).length ) { + + title = $( `

` ).html( o.title ); + } + } + + if( typeof o.description === "function" ) { + + description = o.description( o, container, label ); + } else if( o.description ) { + + if( ! $( c ).find( `#${o.key}_description` ).length ) { + + description = $( `

` ).html( o.description ); + } + } + + if( typeof o.subtitle === "function" ) { + + subtitle = o.subtitle( o, container, label ); + } else if( o.subtitle ) { + + if( ! $( c ).find( `#${o.key}_subtitle` ).length ) { + + subtitle = $( `

` ).text( o.subtitle ); + } + } + + if( typeof o.subdescription === "function" ) { + + subdescription = o.subdescription( o, container, label ); + } else if( o.subdescription ) { + + if( ! $( c ).find( `#${o.key}_subdescription` ).length ) { + + subdescription = $( `
` ).text( o.subdescription ); + } + } + + if( typeof o.label === "function" ) { + + text = o.label( o, container, label ); + } + + if( o.required === true ) { + + text = text + ' *'; + } + + label.text( text ); + + if( o.classes && o.classes.container ) { + + let total_classes = o.classes.container; + + for( let i = 0;i < total_classes;i++ ) { + + o.addClass( o.classes.container[i] ); + } + } + + o.element.css( o.css ); + + if( o.title ) { + + container.append( title ); + } + + if( o.description ) { + + container.append( description ); + } + + if( o.subtitle ) { + + label.append( subtitle ); + } + + if( o.subdescription ) { + + label.append( subdescription ); + } + + if( o.placeholder ) { + + o.element.attr( "placeholder", o.placeholder ); + } + + if( o.label ) { + + container.append( label ); + label.append( o.element ); + } else { + + container.append( o.element ); + } + + switch( true ) { + + case( typeof o.load === "function" ): + + o.load( o ); + return o; + break; + + default: + + break; + } + + if( previous ) { + + container.insertAfter( previous ); + } else if( next ) { + + container.insertBefore( next ); + } else { + + c.append( container ); + } + + if( typeof o.render === "function" ) { + + o.render(); + } + + if( conditions ) { + + if( o.parent.type === "repeatable-data" ) { + + _this.conditionals( [o.key], o.parent.data ); + } else { + + _this.conditionals( [o.key], o.parent ); + } + } + return o; + }, + add_listeners: function( o, element = null ) { + + if( element === null ) { + + element = o.element; + } + + $.each( o.listeners, function( id, value ) { + + if( Array.isArray( value ) ) { + + for( let i = value.length;i--; ) { + + element.on( id, function( e ) { + + value[i]( o, e ); + }); + } + } else { + + element.on( id, function( e ) { + + value( o, e ); + }); + } + }); + }, + conditionals: function( filter = [], data = null ) { + + let _this = this; + let filter_length = filter.length; + + console.log( "checking conditions", filter, data, this.checking_conditions, _i.m.data.element ); + + if( this.checking_conditions === true && data === null ) { + + return; + } + + this.checking_conditions = true; + + if( data === null ) { + + data = _i.m.data; + } + + $.each( data, function( key, value ) { + + if( filter_length && ! filter.includes( value ) ) { + + return; + } + + if( _i.page !== value.page ) { + + return; + } + + if( value.type === "repeatable" ) { + + let length = value.value.length; + for( let i = 0;i < length;i++ ) { + + _this.conditionals( [], value.value[i].data ); + } + } else { + + $.each( value.conditions, function( k, v ) { + + v.check(); + }); + } + }); + this.checking_conditions = false; + }, + next_page: async function() { + + let _this = _i.v; + let v = await _i.validation.verify(); + let total = _i.get_total_pages() + if( _i.page < total && v ) { + + _i.v.render_page( _i.page + 1 ); + } else if( _i.page == total && v ) { + + _i.submit(); + } + return _i.page; + }, + previous_page: async function() { + + if( _i.page > 0 ) { + + _i.v.render_page( _i.page - 1 ); + } + return _i.page; + }, + remove: function( o ) { + + if( typeof o !== "object" || ! o ) { + + throw new Error({ + message: "Removal of form view object requires data of type modal_data", + data_recieved: o, + type_recieved: ( typeof o.type ), + }); + } + + if( ! o.element ) { + + console.log( "removal of blank element", o ); + return o; + } + + let c = o.element.parent(); + let element = o.element; + let i = null; + + i = element.detach(); + i = element.off(); + + if( c.length ) { + + i = c.remove(); + } + return o; + }, + render_controls: function() { + + let _this = _i.v; + let controls = _this.controls; + let next = $( `Next` ); + let back = $( `Back` ); + + controls.html( '' ); + + next.on( "click", function( e ) { + + _this.next_page(); + }); + back.on( "click", function( e ) { + + _this.previous_page(); + }); + + if( _i.page == _i.get_total_pages() ) { + + next.text( "Submit" ); + next.attr( "type", "submit" ) + } + + if( _i.page == 0 ) { + + controls.append( next ); + } else { + + controls.append( back ); + controls.append( next ); + } + }, + render_page: function( id ) { + + let _this = this; + + _i.publish( "data.onWillRenderPage", { + page: id, + view: this, + }); + + _this.unrender_page( _i.page ); + _i.page = id; + _this.container.append( _this.message ); + + $.each( _this.m.data, function( key, value ) { + + if( ! value || value.page !== _i.page || value.shown_by_default === false ) { + + return; + } + + _this.add( value ); + }); + + _this.controls = $( `
` ); + _this.container.append( _this.controls ); + _this.render_controls(); + _this.update(); + _this.conditionals(); + + _i.publish( "data.onRenderedPage", { + page: id, + view: this, + }); + }, + unrender_page: function( id ) { + + let _this = this; + let keys = Object.keys( _this.m.data ); + let total = keys.length; + + _i.publish( "data.onUnRenderPage", { + page: id, + view: this, + }); + + for( let i = 0;i < total;i++ ) { + + if( _this.m.data[keys[i]].page === id ) { + + _this.remove( _this.m.data[keys[i]] ); + } + } + _this.container.html( '' ); + }, + update: function() {}, + }; + this.validation = { + + empty_values: [ + undefined, + null, + 'null', + '', + [], + ], + parent: _i, + types: { + + email: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/, + number: /^(?=.)([+-]?(\d*)(\.(\d+))?)$/, + phone: /^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/, + ssn: /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/, + year: /^[0-9]{4}$/, + }, + + validate: function( o ) { + + let _this = _i.validation; + let pass = true; + + return new Promise( function( resolve, reject ) { + + if( Object.keys( _this.types ).includes( o.type ) ) { + + if( typeof _this.types[`${o.type}`] === "function" ) { + + _this.types[`${o.type}`]( o ) + .then( resolve ); + } else { + + if( o.required ) { + + if( _this.empty_values.includes( o.value ) ) { + + pass = false; + } + } + + console.log( o, o.value ); + + if( pass && ! _this.types[`${o.type}`].test( o.value ) ) { + + pass = false; + } + + resolve( pass ); + } + } else { + + reject( false ); + } + }); + }, + verify: function( data = null ) { + + return new Promise( async function( resolve, reject ) { + + let empty_values = [ + undefined, + null, + 'null', + '', + [], + ]; + let _this = _i.validation; + let values = []; + + if( data == null ) { + + _i.publish( "data.onWillVerify", { + form: _i, + }); + + data = _i.m.data + } + + $.each( data, async function( key, value ) { + + let show = true; + let repeatable = _i.m.is_repeatable( value ); + let pass = true; + + if( value.page !== _i.page ) { + + return; + } + + if( ! value.required && ! repeatable ) { + + return; + } + + if( typeof value.conditions === "function" ) { + + show = value.conditions( value ); + } else if( value.conditions ) { + + $.each( value.conditions, function( i, v ) { + + if( ( v.value === v.o.value || `${v.value}` === `${v.o.value}` ) && show ) { + + show = true; + } else { + + show = false; + } + }); + } + + if( ! show ) { + + return; + } + + if( repeatable ) { + + let total = value.value.length; + for( let i = 0;i < total;i++ ) { + + let a = await _this.verify( value.value[i].data ); + values.concat( a ); + } + } + + if( value.subfields ) { + + if( value.required && ! value.value.length ) { + + pass = false; + } + } + + if( typeof value.validation === "function" ) { + + pass = await value.validation( value ); + } + + values.push( pass ); + if( pass ) { + + value.element.parent().css( 'color', 'black' ); + } else { + + value.element.parent().css( 'color', 'red' ); + } + }); + + if( ! values.includes( false ) ) { + + resolve( true ); + _i.publish( "data.onVerificationSuccess", { + form: _i, + }); + } else { + + _i.publish( "data.onVerificationFailure", { + form: _i, + }); + _i.v.message.html( codiad.common.message( "Please be sure to fill out all denoted fields", "error" ) ); + $( "html, body" ).animate( { scrollTop: 0 }, "slow" ); + reject( false ); + } + }); + }, + }; + this.submit = function() {}; + + _i.m.init(); + _i.v.init(); + + if( instance !== null ) { + + _i.init( instance ); + } + return _i; + }; + + codiad.forms.prototype.create = function( key, o ) { + + if( typeof o !== "object" ) { + + throw new Error({ + message: "Initialization of form object requires data of type Object", + data_recieved: o, + type_recieved: ( typeof o ), + }); + } + + this.m.data[key] = this.m.create( o ); + }; + codiad.forms.prototype.get_first_page = function() { + + let _this = this; + let i = null; + + $.each( _this.m.data, function( key, value ) { + + if( i === null ) { + + i = value.page; + } else if( value.page > i ) { + + i = value.page; + } + }); + return i; + }, + codiad.forms.prototype.get_total_pages = function() { + + let _this = this; + let i = 0; + $.each( _this.m.data, function( key, value ) { + + if( value.page > i ) { + + i = value.page; + } + }); + return i; + }; + codiad.forms.prototype.clone = function( o ) {}; + codiad.forms.prototype.init = function( o ) { + + if( typeof o !== "object" ) { + + throw new Error({ + message: "Initialization of form object requires data of type Object", + data_recieved: o, + type_recieved: ( typeof o.type ), + }); + } + + let _this = this; + + if( o.container ) { + + this.v.container = o.container; + } + + if( ! o.data ) { + + o.data = {}; + } + + let previous = null; + + $.each( o.data, async function( key, value ) { + + if( value.key === undefined ) { + + value.key = key; + } + _this.create( key, value ); + + _this.m.data[key].previous = previous; + + if( previous !== null ) { + + previous.next = _this.m.data[key]; + } + + previous = _this.m.data[key]; + }); + this.m.conditionals(); + this.v.render_page( 0 ); + }; +})( this, jQuery ); \ No newline at end of file diff --git a/assets/js/jquery-3.5.1.js b/assets/js/jquery-3.5.1.js new file mode 100644 index 0000000..b061403 --- /dev/null +++ b/assets/js/jquery-3.5.1.js @@ -0,0 +1,2 @@ +/*! jQuery v3.5.1 | (c) JS Foundation and other contributors | jquery.org/license */ +!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],r=Object.getPrototypeOf,s=t.slice,g=t.flat?function(e){return t.flat.call(e)}:function(e){return t.concat.apply([],e)},u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},x=function(e){return null!=e&&e===e.window},E=C.document,c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.5.1",S=function(e,t){return new S.fn.init(e,t)};function p(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0+~]|"+M+")"+M+"*"),U=new RegExp(M+"|>"),X=new RegExp(F),V=new RegExp("^"+I+"$"),G={ID:new RegExp("^#("+I+")"),CLASS:new RegExp("^\\.("+I+")"),TAG:new RegExp("^("+I+"|[*])"),ATTR:new RegExp("^"+W),PSEUDO:new RegExp("^"+F),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+R+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/HTML$/i,Q=/^(?:input|select|textarea|button)$/i,J=/^h\d$/i,K=/^[^{]+\{\s*\[native \w/,Z=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ee=/[+~]/,te=new RegExp("\\\\[\\da-fA-F]{1,6}"+M+"?|\\\\([^\\r\\n\\f])","g"),ne=function(e,t){var n="0x"+e.slice(1)-65536;return t||(n<0?String.fromCharCode(n+65536):String.fromCharCode(n>>10|55296,1023&n|56320))},re=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ie=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},oe=function(){T()},ae=be(function(e){return!0===e.disabled&&"fieldset"===e.nodeName.toLowerCase()},{dir:"parentNode",next:"legend"});try{H.apply(t=O.call(p.childNodes),p.childNodes),t[p.childNodes.length].nodeType}catch(e){H={apply:t.length?function(e,t){L.apply(e,O.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function se(t,e,n,r){var i,o,a,s,u,l,c,f=e&&e.ownerDocument,p=e?e.nodeType:9;if(n=n||[],"string"!=typeof t||!t||1!==p&&9!==p&&11!==p)return n;if(!r&&(T(e),e=e||C,E)){if(11!==p&&(u=Z.exec(t)))if(i=u[1]){if(9===p){if(!(a=e.getElementById(i)))return n;if(a.id===i)return n.push(a),n}else if(f&&(a=f.getElementById(i))&&y(e,a)&&a.id===i)return n.push(a),n}else{if(u[2])return H.apply(n,e.getElementsByTagName(t)),n;if((i=u[3])&&d.getElementsByClassName&&e.getElementsByClassName)return H.apply(n,e.getElementsByClassName(i)),n}if(d.qsa&&!N[t+" "]&&(!v||!v.test(t))&&(1!==p||"object"!==e.nodeName.toLowerCase())){if(c=t,f=e,1===p&&(U.test(t)||z.test(t))){(f=ee.test(t)&&ye(e.parentNode)||e)===e&&d.scope||((s=e.getAttribute("id"))?s=s.replace(re,ie):e.setAttribute("id",s=S)),o=(l=h(t)).length;while(o--)l[o]=(s?"#"+s:":scope")+" "+xe(l[o]);c=l.join(",")}try{return H.apply(n,f.querySelectorAll(c)),n}catch(e){N(t,!0)}finally{s===S&&e.removeAttribute("id")}}}return g(t.replace($,"$1"),e,n,r)}function ue(){var r=[];return function e(t,n){return r.push(t+" ")>b.cacheLength&&delete e[r.shift()],e[t+" "]=n}}function le(e){return e[S]=!0,e}function ce(e){var t=C.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function fe(e,t){var n=e.split("|"),r=n.length;while(r--)b.attrHandle[n[r]]=t}function pe(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function de(t){return function(e){return"input"===e.nodeName.toLowerCase()&&e.type===t}}function he(n){return function(e){var t=e.nodeName.toLowerCase();return("input"===t||"button"===t)&&e.type===n}}function ge(t){return function(e){return"form"in e?e.parentNode&&!1===e.disabled?"label"in e?"label"in e.parentNode?e.parentNode.disabled===t:e.disabled===t:e.isDisabled===t||e.isDisabled!==!t&&ae(e)===t:e.disabled===t:"label"in e&&e.disabled===t}}function ve(a){return le(function(o){return o=+o,le(function(e,t){var n,r=a([],e.length,o),i=r.length;while(i--)e[n=r[i]]&&(e[n]=!(t[n]=e[n]))})})}function ye(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}for(e in d=se.support={},i=se.isXML=function(e){var t=e.namespaceURI,n=(e.ownerDocument||e).documentElement;return!Y.test(t||n&&n.nodeName||"HTML")},T=se.setDocument=function(e){var t,n,r=e?e.ownerDocument||e:p;return r!=C&&9===r.nodeType&&r.documentElement&&(a=(C=r).documentElement,E=!i(C),p!=C&&(n=C.defaultView)&&n.top!==n&&(n.addEventListener?n.addEventListener("unload",oe,!1):n.attachEvent&&n.attachEvent("onunload",oe)),d.scope=ce(function(e){return a.appendChild(e).appendChild(C.createElement("div")),"undefined"!=typeof e.querySelectorAll&&!e.querySelectorAll(":scope fieldset div").length}),d.attributes=ce(function(e){return e.className="i",!e.getAttribute("className")}),d.getElementsByTagName=ce(function(e){return e.appendChild(C.createComment("")),!e.getElementsByTagName("*").length}),d.getElementsByClassName=K.test(C.getElementsByClassName),d.getById=ce(function(e){return a.appendChild(e).id=S,!C.getElementsByName||!C.getElementsByName(S).length}),d.getById?(b.filter.ID=function(e){var t=e.replace(te,ne);return function(e){return e.getAttribute("id")===t}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n=t.getElementById(e);return n?[n]:[]}}):(b.filter.ID=function(e){var n=e.replace(te,ne);return function(e){var t="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return t&&t.value===n}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),b.find.TAG=d.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):d.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},b.find.CLASS=d.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&E)return t.getElementsByClassName(e)},s=[],v=[],(d.qsa=K.test(C.querySelectorAll))&&(ce(function(e){var t;a.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&v.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||v.push("\\["+M+"*(?:value|"+R+")"),e.querySelectorAll("[id~="+S+"-]").length||v.push("~="),(t=C.createElement("input")).setAttribute("name",""),e.appendChild(t),e.querySelectorAll("[name='']").length||v.push("\\["+M+"*name"+M+"*="+M+"*(?:''|\"\")"),e.querySelectorAll(":checked").length||v.push(":checked"),e.querySelectorAll("a#"+S+"+*").length||v.push(".#.+[+~]"),e.querySelectorAll("\\\f"),v.push("[\\r\\n\\f]")}),ce(function(e){e.innerHTML="";var t=C.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&v.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&v.push(":enabled",":disabled"),a.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&v.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),v.push(",.*:")})),(d.matchesSelector=K.test(c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.oMatchesSelector||a.msMatchesSelector))&&ce(function(e){d.disconnectedMatch=c.call(e,"*"),c.call(e,"[s!='']:x"),s.push("!=",F)}),v=v.length&&new RegExp(v.join("|")),s=s.length&&new RegExp(s.join("|")),t=K.test(a.compareDocumentPosition),y=t||K.test(a.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return l=!0,0;var n=!e.compareDocumentPosition-!t.compareDocumentPosition;return n||(1&(n=(e.ownerDocument||e)==(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!d.sortDetached&&t.compareDocumentPosition(e)===n?e==C||e.ownerDocument==p&&y(p,e)?-1:t==C||t.ownerDocument==p&&y(p,t)?1:u?P(u,e)-P(u,t):0:4&n?-1:1)}:function(e,t){if(e===t)return l=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e==C?-1:t==C?1:i?-1:o?1:u?P(u,e)-P(u,t):0;if(i===o)return pe(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?pe(a[r],s[r]):a[r]==p?-1:s[r]==p?1:0}),C},se.matches=function(e,t){return se(e,null,null,t)},se.matchesSelector=function(e,t){if(T(e),d.matchesSelector&&E&&!N[t+" "]&&(!s||!s.test(t))&&(!v||!v.test(t)))try{var n=c.call(e,t);if(n||d.disconnectedMatch||e.document&&11!==e.document.nodeType)return n}catch(e){N(t,!0)}return 0":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(te,ne),e[3]=(e[3]||e[4]||e[5]||"").replace(te,ne),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||se.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&se.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return G.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=h(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(te,ne).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=m[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&m(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(n,r,i){return function(e){var t=se.attr(e,n);return null==t?"!="===r:!r||(t+="","="===r?t===i:"!="===r?t!==i:"^="===r?i&&0===t.indexOf(i):"*="===r?i&&-1:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function D(e,n,r){return m(n)?S.grep(e,function(e,t){return!!n.call(e,t,e)!==r}):n.nodeType?S.grep(e,function(e){return e===n!==r}):"string"!=typeof n?S.grep(e,function(e){return-1)[^>]*|#([\w-]+))$/;(S.fn.init=function(e,t,n){var r,i;if(!e)return this;if(n=n||j,"string"==typeof e){if(!(r="<"===e[0]&&">"===e[e.length-1]&&3<=e.length?[null,e,null]:q.exec(e))||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){if(t=t instanceof S?t[0]:t,S.merge(this,S.parseHTML(r[1],t&&t.nodeType?t.ownerDocument||t:E,!0)),N.test(r[1])&&S.isPlainObject(t))for(r in t)m(this[r])?this[r](t[r]):this.attr(r,t[r]);return this}return(i=E.getElementById(r[2]))&&(this[0]=i,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):m(e)?void 0!==n.ready?n.ready(e):e(S):S.makeArray(e,this)}).prototype=S.fn,j=S(E);var L=/^(?:parents|prev(?:Until|All))/,H={children:!0,contents:!0,next:!0,prev:!0};function O(e,t){while((e=e[t])&&1!==e.nodeType);return e}S.fn.extend({has:function(e){var t=S(e,this),n=t.length;return this.filter(function(){for(var e=0;e\x20\t\r\n\f]*)/i,he=/^$|^module$|\/(?:java|ecma)script/i;ce=E.createDocumentFragment().appendChild(E.createElement("div")),(fe=E.createElement("input")).setAttribute("type","radio"),fe.setAttribute("checked","checked"),fe.setAttribute("name","t"),ce.appendChild(fe),y.checkClone=ce.cloneNode(!0).cloneNode(!0).lastChild.checked,ce.innerHTML="",y.noCloneChecked=!!ce.cloneNode(!0).lastChild.defaultValue,ce.innerHTML="",y.option=!!ce.lastChild;var ge={thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};function ve(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&A(e,t)?S.merge([e],n):n}function ye(e,t){for(var n=0,r=e.length;n",""]);var me=/<|&#?\w+;/;function xe(e,t,n,r,i){for(var o,a,s,u,l,c,f=t.createDocumentFragment(),p=[],d=0,h=e.length;d\s*$/g;function qe(e,t){return A(e,"table")&&A(11!==t.nodeType?t:t.firstChild,"tr")&&S(e).children("tbody")[0]||e}function Le(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function He(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Oe(e,t){var n,r,i,o,a,s;if(1===t.nodeType){if(Y.hasData(e)&&(s=Y.get(e).events))for(i in Y.remove(t,"handle events"),s)for(n=0,r=s[i].length;n").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),E.head.appendChild(r[0])},abort:function(){i&&i()}}});var Ut,Xt=[],Vt=/(=)\?(?=&|$)|\?\?/;S.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=Xt.pop()||S.expando+"_"+Ct.guid++;return this[e]=!0,e}}),S.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Vt.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Vt.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=m(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Vt,"$1"+r):!1!==e.jsonp&&(e.url+=(Et.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||S.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=C[r],C[r]=function(){o=arguments},n.always(function(){void 0===i?S(C).removeProp(r):C[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,Xt.push(r)),o&&m(i)&&i(o[0]),o=i=void 0}),"script"}),y.createHTMLDocument=((Ut=E.implementation.createHTMLDocument("").body).innerHTML="
",2===Ut.childNodes.length),S.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(y.createHTMLDocument?((r=(t=E.implementation.createHTMLDocument("")).createElement("base")).href=E.location.href,t.head.appendChild(r)):t=E),o=!n&&[],(i=N.exec(e))?[t.createElement(i[1])]:(i=xe([e],t,o),o&&o.length&&S(o).remove(),S.merge([],i.childNodes)));var r,i,o},S.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1").append(S.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},S.expr.pseudos.animated=function(t){return S.grep(S.timers,function(e){return t===e.elem}).length},S.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=S.css(e,"position"),c=S(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=S.css(e,"top"),u=S.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),m(t)&&(t=t.call(e,n,S.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):("number"==typeof f.top&&(f.top+="px"),"number"==typeof f.left&&(f.left+="px"),c.css(f))}},S.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){S.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===S.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===S.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=S(e).offset()).top+=S.css(e,"borderTopWidth",!0),i.left+=S.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-S.css(r,"marginTop",!0),left:t.left-i.left-S.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===S.css(e,"position"))e=e.offsetParent;return e||re})}}),S.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;S.fn[t]=function(e){return $(this,function(e,t,n){var r;if(x(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),S.each(["top","left"],function(e,n){S.cssHooks[n]=$e(y.pixelPosition,function(e,t){if(t)return t=Be(e,n),Me.test(t)?S(e).position()[n]+"px":t})}),S.each({Height:"height",Width:"width"},function(a,s){S.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){S.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return $(this,function(e,t,n){var r;return x(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?S.css(e,t,i):S.style(e,t,n,i)},s,n?e:void 0,n)}})}),S.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){S.fn[t]=function(e){return this.on(t,e)}}),S.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)},hover:function(e,t){return this.mouseenter(e).mouseleave(t||e)}}),S.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){S.fn[n]=function(e,t){return 0 "none", + "message" => "", + "value" => null, + ); + } + public static function i18n( $key, $args = array() ) { global $lang; diff --git a/components/data/class.data.php b/components/data/class.data.php index c49dcc0..2d08dba 100644 --- a/components/data/class.data.php +++ b/components/data/class.data.php @@ -21,6 +21,37 @@ class Data { return self::$instance; } + + public function query( $query, $bind_vars, $default, $action='fetchAll', $errors="default" ) { + + $return = common::get_default_return(); + + if( is_array( $query ) ) { + + if( in_array( DBTYPE, array_keys( $query ) ) ) { + + $query = $query[DBTYPE]; + } else { + + if( isset( $query["*"] ) ) { + + $query = $query["*"]; + } else { + + $return = $default; + + if( $errors == "message" ) { + + $return = json_encode( array( "error" => "No query specified for database type." ) ); + } elseif( $errors == "exception" ) { + + throw new Error( "No query specified for database type." ); + } + return $return; + } + } + } + } } ?> \ No newline at end of file diff --git a/components/initialize/class.initialize.php b/components/initialize/class.initialize.php index 6219dc3..2550fdf 100644 --- a/components/initialize/class.initialize.php +++ b/components/initialize/class.initialize.php @@ -32,26 +32,31 @@ class Initialize { function __construct() { - $config = realpath( __DIR__ . "/../../config.php" ); + $config = realpath( dirname( __FILE__ ) . "/../../config.php" ); - if( ! file_exists( $config ) || ! is_readable( $config ) ) { + if( ! self::is_installing() ) { - $message = "Error loading config file."; - - if( ! file_exists( $config ) ) { + if( ( ! file_exists( $config ) || ! is_readable( $config ) ) ) { - $message = "Error, could not find config file. If you have not installed Codiad please go to /install/ to complete the installation."; - } elseif( ! is_readable( $config ) ) { + $message = "Error loading config file."; - $message = "Error config file is not readable, please check permissions."; + if( ! file_exists( $config ) ) { + + $message = "Error, could not find config file. If you have not installed Codiad please go to /install/ to complete the installation."; + } elseif( ! is_readable( $config ) ) { + + $message = "Error config file is not readable, please check permissions."; + } + echo $message; + exit(); + } else { + + require_once( $config ); } - echo $message; - exit(); } - require_once( $config ); - $this->register_constants(); + $this->register_globals(); $bases = self::BASES; @@ -67,7 +72,6 @@ class Initialize { } } - $this->register_globals(); $this->check_paths(); } @@ -101,6 +105,11 @@ class Initialize { return self::$instance; } + public static function is_installing() { + + return ! ( strpos( $_SERVER["SCRIPT_NAME"], "install" ) === false ); + } + function register_constants() { if( ! defined( 'AUTH_TYPE' ) ) { @@ -140,6 +149,11 @@ class Initialize { define( 'SITE_NAME', "Codiad" ); } + if( ! defined( 'THEME' ) ) { + + define( "THEME", "default" ); + } + if( ! defined( 'THEMES' ) ) { define( "THEMES", BASE_PATH . "/themes" ); diff --git a/config.php b/config.php deleted file mode 100644 index 5ce436c..0000000 --- a/config.php +++ /dev/null @@ -1,65 +0,0 @@ - diff --git a/install/index.php b/install/index.php new file mode 100644 index 0000000..4d6b809 --- /dev/null +++ b/install/index.php @@ -0,0 +1,132 @@ + + + + + + + <?php echo SITE_NAME;?> + ' ); + } else { + + echo( '' ); + } + } + + // Load Component CSS Files + foreach( $components as $component ) { + + if( file_exists( THEMES . "/". $theme . "/" . $component . "/screen.css" ) ) { + + echo( '' ); + } else { + + if( file_exists( "../themes/default/" . $component . "/screen.css" ) ) { + + echo( '' ); + } else { + + if( file_exists( COMPONENTS . "/" . $component . "/screen.css" ) ) { + + echo( '' ); + } + } + } + } + + if( file_exists( THEMES . "/". $theme . "/favicon.ico" ) ) { + + echo( '' ); + } else { + + echo( '' ); + } + ?> + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/login.php b/login.php index 4e9e80e..8fd942b 100644 --- a/login.php +++ b/login.php @@ -1,9 +1,5 @@