main.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var config = require('./config'),
  2. Seed = require('./seed'),
  3. directives = require('./directives'),
  4. filters = require('./filters'),
  5. textParser = require('./text-parser')
  6. var controllers = config.controllers,
  7. datum = config.datum,
  8. api = {},
  9. reserved = ['datum', 'controllers']
  10. /*
  11. * Store a piece of plain data in config.datum
  12. * so it can be consumed by sd-data
  13. */
  14. api.data = function (id, data) {
  15. if (!data) return datum[id]
  16. if (datum[id]) {
  17. console.warn('data object "' + id + '"" already exists and has been overwritten.')
  18. }
  19. datum[id] = data
  20. }
  21. /*
  22. * Store a controller function in config.controllers
  23. * so it can be consumed by sd-controller
  24. */
  25. api.controller = function (id, extensions) {
  26. if (!extensions) return controllers[id]
  27. if (controllers[id]) {
  28. console.warn('controller "' + id + '" already exists and has been overwritten.')
  29. }
  30. controllers[id] = extensions
  31. }
  32. /*
  33. * Allows user to create a custom directive
  34. */
  35. api.directive = function (name, fn) {
  36. if (!fn) return directives[name]
  37. directives[name] = fn
  38. }
  39. /*
  40. * Allows user to create a custom filter
  41. */
  42. api.filter = function (name, fn) {
  43. if (!fn) return filters[name]
  44. filters[name] = fn
  45. }
  46. /*
  47. * Bootstrap the whole thing
  48. * by creating a Seed instance for top level nodes
  49. * that has either sd-controller or sd-data
  50. */
  51. api.bootstrap = function (opts) {
  52. if (opts) {
  53. for (var key in opts) {
  54. if (reserved.indexOf(key) === -1) {
  55. config[key] = opts[key]
  56. }
  57. }
  58. }
  59. textParser.buildRegex()
  60. var el,
  61. ctrlSlt = '[' + config.prefix + '-controller]',
  62. dataSlt = '[' + config.prefix + '-data]'
  63. /* jshint boss: true */
  64. while (el = document.querySelector(ctrlSlt) || document.querySelector(dataSlt)) {
  65. new Seed(el)
  66. }
  67. }
  68. module.exports = api