1 module hunt.framework.breadcrumb.BreadcrumbsManager; 2 3 import hunt.framework.breadcrumb.BreadcrumbItem; 4 import hunt.framework.breadcrumb.Breadcrumbs; 5 6 import std.array; 7 8 /** 9 * 10 */ 11 class BreadcrumbsManager { 12 private Breadcrumbs generator; 13 private Handler[string] callbacks; 14 15 this() { 16 generator = new Breadcrumbs(); 17 } 18 19 void register(string name, Handler handler) { 20 if (name in callbacks) 21 throw new Exception("Breadcrumb name " ~ name ~ " has already been registered"); 22 callbacks[name] = handler; 23 } 24 25 bool exists(string name) { 26 auto itemPtr = name in callbacks; 27 return itemPtr !is null; 28 } 29 30 BreadcrumbItem[] generate(string name, Object[] params...) { 31 string origName = name; 32 33 if (name.empty) { 34 return []; 35 } 36 37 try { 38 return this.generator.generate(this.callbacks, name, params); 39 } catch (Exception ex) { 40 return []; 41 } 42 } 43 44 string render(string name, Object[] params...) { 45 BreadcrumbItem[] breadcrumbs = generate(name, params); 46 string r; 47 bool isFirst = true; 48 foreach(BreadcrumbItem item; breadcrumbs) { 49 if(isFirst) { 50 isFirst = false; 51 } else { 52 r ~= " / "; 53 } 54 r ~= item.title ~ "[url=\"" ~ item.link ~ "\"]"; 55 } 56 return r; 57 } 58 59 60 } 61 62 // private __gshared BreadcrumbsManager _breadcrumbsManager; 63 64 // BreadcrumbsManager breadcrumbsManager() { 65 // if (_breadcrumbsManager is null) { 66 // _breadcrumbsManager = new BreadcrumbsManager; 67 // } 68 69 // return _breadcrumbsManager; 70 // }