# Your Adapter SHOULD define: # * @QueryDefaults - The default parameters if no others are passed in. class Embeditor.Adapter className: "Adapter" @QueryDefaults = {} @DisplayDefaults = placement : 'after' constructor: (@element, @options={}) -> @adapter = Embeditor.Adapters[@className] @href = @element.attr('href') @wrapper = $("
", class: @options.wrapperClass) displayData = @_extractData('DisplayDefaults') @display = @_buildDisplayOptions(displayData) queryData = @_extractData('QueryDefaults') @dataOptions = queryData # Deprecated @queryParams = @_buildQueryParams(queryData) # @Override swap: -> return embed: (html) -> @wrapper.html(html) @element[Embeditor.PlacementFunctions[@display.placement]](@wrapper) _extractData: (defaults) -> data = {} for key,val of @element.data() # Make sure we care about this attribute if @adapter[defaults]?[key] data[key] = val data _buildDisplayOptions: (data) -> @_defaultsWithoutEmptyStrings(data, # What the user wants @options[@adapter]?['display'], # What the developer wants @options['display'], # What the developer wants globally @adapter.DisplayDefaults # What Embeditor wants ) # We're combining a few things (in order of precedence): # 1. The `data-attributes` of the placeholder, # 2. The adapter-specific options specified at Embeditor # initialization, # 3. The global options specified at Embeditor initialization, # 4. This adapter's default options (fallback options). _buildQueryParams: (data) -> @_defaultsWithoutEmptyStrings(data, @options[@adapter]?['query'], @options['query'], @adapter.QueryDefaults ) # Like Underscore.defaults, but it will also fill in empty strings. # This should be used when merging objects that including any user # input. _defaultsWithoutEmptyStrings: (obj) -> args = Array.prototype.slice.call(arguments, 1) for source in args continue if !source for prop,value of source obj[prop] = source[prop] if !obj[prop] obj