@@ -610,14 +610,15 @@ void function (window, _ext) {
610610} ( window , _ext )
611611
612612//////////////////// action ////////////////////
613- //include and wrap external lib : action.js
613+ //include and wrap external module : action.js
614614
615615void function ( window , _ext ) {
616616 'use strict'
617617
618618/**
619619 * Action.js - Easy and lazy solution for click-event-binding.
620- * (https://github.com/cssmagic/action.js)
620+ * Released under the MIT license.
621+ * https://github.com/cssmagic/action.js
621622 */
622623var action = function ( ) {
623624 'use strict'
@@ -628,6 +629,20 @@ var action = function () {
628629 var SELECTOR = '[data-action]'
629630 var _actionList = { }
630631
632+ //util
633+ function _getActionName ( $elem ) {
634+ var result = $elem . data ( 'action' ) || ''
635+ if ( ! result ) {
636+ var href = $ . trim ( $elem . attr ( 'href' ) )
637+ if ( href && href . indexOf ( '#' ) === 0 ) result = href
638+ }
639+ return _formatActionName ( result )
640+ }
641+ function _formatActionName ( s ) {
642+ var result = s ? $ . trim ( String ( s ) ) . replace ( / ^ [ # ! ] + / , '' ) : ''
643+ return $ . trim ( result )
644+ }
645+
631646 function _init ( ) {
632647 var $wrapper = $ ( document . body || document . documentElement )
633648 $wrapper . on ( 'click' , SELECTOR , function ( ev ) {
@@ -645,19 +660,6 @@ var action = function () {
645660 }
646661 } )
647662 }
648-
649- function _getActionName ( $elem ) {
650- var result = $elem . data ( 'action' ) || ''
651- if ( ! result ) {
652- var href = $ . trim ( $elem . attr ( 'href' ) )
653- if ( href && href . indexOf ( '#' ) === 0 ) result = href
654- }
655- return _formatActionName ( result )
656- }
657- function _formatActionName ( s ) {
658- var result = s ? $ . trim ( s + '' ) . replace ( / ^ [ # ! ] + / , '' ) : ''
659- return $ . trim ( result )
660- }
661663 function _handle ( actionName , context ) {
662664 var fn = _actionList [ actionName ]
663665 if ( $ . isFunction ( fn ) ) {
@@ -728,39 +730,42 @@ var action = function () {
728730} ( window , _ext )
729731
730732//////////////////// template ////////////////////
731- //front-end template fetching, caching and rendering
733+ //include and wrap external module: underscore.template
732734
733735void function ( window , _ext ) {
734736 'use strict'
735737
738+ /**
739+ * Underscore.template - More APIs for Underscore's template engine.
740+ * Released under the MIT license.
741+ * https://github.com/cssmagic/underscore.template
742+ */
743+ var template = function ( ) {
744+ 'use strict'
745+
736746 //namespace
737747 var template = { }
738748
739749 //config
740- var _config = {
741- //for jedi 1.0
742- needStripCommentTag : true ,
743-
744- //compatible with ejs
745- interpolate : / < % - ( [ \s \S ] + ?) % > / g,
746- escape : / < % = ( [ \s \S ] + ?) % > / g,
747-
748- //to avoid use `with` in compiled templates
749- //see: https://github.com/cssmagic/blog/issues/4
750- variable : 'data'
751- }
752- var PREFIX_TEMPLATE = 'template-'
750+ var ELEM_ID_PREFIX = 'template-'
753751
754752 //cache
755753 var _cacheTemplate = { }
756754 var _cacheCompiledTemplate = { }
757755
758756 //util
759757 function _toTemplateId ( id ) {
760- return String ( id ) . replace ( PREFIX_TEMPLATE , '' )
758+ //`#template-my-tpl-001` -> `my-tpl-001`
759+ // `template-my-tpl-001` -> `my-tpl-001`
760+ // `my-tpl-001` -> `my-tpl-001`
761+ id = id ? _ . str . trim ( id ) . replace ( / ^ [ # ! ] + / , '' ) : ''
762+ return _ . str . trim ( id ) . replace ( ELEM_ID_PREFIX , '' )
761763 }
762764 function _toElementId ( id ) {
763- return _ . str . startsWith ( id , PREFIX_TEMPLATE ) ? id : PREFIX_TEMPLATE + id
765+ //`template-my-tpl-001` -> `template-my-tpl-001`
766+ // `my-tpl-001` -> `template-my-tpl-001`
767+ id = id ? _ . str . trim ( id ) : ''
768+ return _ . str . startsWith ( id , ELEM_ID_PREFIX ) ? id : ELEM_ID_PREFIX + id
764769 }
765770 function _stripCommentTag ( str ) {
766771 str = String ( str )
@@ -772,25 +777,33 @@ void function (window, _ext) {
772777 }
773778 //get template by id (of dummy script element in html)
774779 function _getTemplateById ( id ) {
775- if ( ! id || ! _ . isString ( id ) ) return false
780+ if ( ! id ) return false
776781 var result
777- var idElement = _toElementId ( id )
778- var elem = document . getElementById ( idElement )
779- if ( ! elem ) {
780- console . error ( 'Element "#' + idElement + '" not found!' )
781- } else {
782+ var elementId = _toElementId ( String ( id ) )
783+ var elem = document . getElementById ( elementId )
784+ if ( elem ) {
782785 var str = _ . str . trim ( elem . innerHTML )
783- if ( ! str ) {
784- console . error ( 'Element "#' + idElement + '" is empty!' )
785- } else {
786+ if ( str ) {
786787 //strip html comment tag wrapping template code
787- if ( _ . templateSettings . needStripCommentTag ) str = _stripCommentTag ( str )
788+ //especially for jedi 1.0 (https://github.com/baixing/jedi)
789+ if ( _ . templateSettings . shouldUnwrapCommentTag ) str = _stripCommentTag ( str )
790+
788791 if ( _isTemplateCode ( str ) ) {
789792 result = str
790793 } else {
791- console . error ( 'Template code in element "#' + idElement + '" is no valid!' )
794+ /** DEBUG_INFO_START **/
795+ console . warn ( '[Template] Template code in element "#' + elementId + '" is invalid!' )
796+ /** DEBUG_INFO_END **/
792797 }
798+ } else {
799+ /** DEBUG_INFO_START **/
800+ console . warn ( '[Template] Element "#' + elementId + '" is empty!' )
801+ /** DEBUG_INFO_END **/
793802 }
803+ } else {
804+ /** DEBUG_INFO_START **/
805+ console . warn ( '[Template] Element "#' + elementId + '" not found!' )
806+ /** DEBUG_INFO_END **/
794807 }
795808 return result || false
796809 }
@@ -800,19 +813,18 @@ void function (window, _ext) {
800813 }
801814
802815 //fn
803- function updateSettings ( ) {
804- _ . extend ( _ . templateSettings , _config )
805- }
806816 function add ( id , templateCode ) {
807817 //todo: accept second param as a function, to support pre-compiled template.
808- if ( ! id || ! _ . isString ( id ) ) return false
809- id = _ . str . stripHash ( id )
818+ if ( arguments . length < 2 ) return false
819+
810820 var result
811821 if ( templateCode ) {
812822 var templateId = _toTemplateId ( id )
823+ /** DEBUG_INFO_START **/
813824 if ( _cacheTemplate [ templateId ] ) {
814- console . warn ( 'Template cache already has id: "' + templateId + '"' )
825+ console . warn ( '[ Template] Template id "' + templateId + '" already existed. ' )
815826 }
827+ /** DEBUG_INFO_END **/
816828 result = _cacheTemplate [ templateId ] = templateCode
817829 } else {
818830 //todo: support `_.template.add(id)` to add from dummy script element
@@ -862,14 +874,31 @@ void function (window, _ext) {
862874 return result || ''
863875 }
864876
865- //init
866- updateSettings ( )
867-
877+ /** DEBUG_INFO_START **/
868878 //exports for unit test
879+ template . __toTemplateId = _toTemplateId
880+ template . __toElementId = _toElementId
869881 template . __isTemplateCode = _isTemplateCode
870882 template . __stripCommentTag = _stripCommentTag
871883 template . __cacheTemplate = _cacheTemplate
872884 template . __cacheCompiledTemplate = _cacheCompiledTemplate
885+ /** DEBUG_INFO_END **/
886+
887+ //exports
888+ return template
889+
890+ } ( )
891+
892+ var _config = {
893+ //compatible with ejs
894+ interpolate : / < % - ( [ \s \S ] + ?) % > / g,
895+ escape : / < % = ( [ \s \S ] + ?) % > / g,
896+
897+ //to avoid use `with` in compiled templates
898+ //see: https://github.com/cssmagic/blog/issues/4
899+ variable : 'data'
900+ }
901+ _ . extend ( _ . templateSettings , _config )
873902
874903 //exports
875904 _ext . exports ( 'template' , template )
0 commit comments