This repository was archived by the owner on May 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.coffee
More file actions
126 lines (106 loc) · 4.21 KB
/
section.coffee
File metadata and controls
126 lines (106 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
define [
"events"
"config"
"loader"
"widgets"
"utils/widgetsData"
"underscore"
"clicks/forms"
"dom"
], (events, config, loader, widgets, widgetsData, _, forms, dom) ->
extensionRegex = /\.css$/
Section = () ->
@name
@params = {}
@element
@getSectionHtml = () ->
unless @sectionHtml
@sectionHtml = if @element? and @element.childNodes? then Array.prototype.slice.call @element.childNodes else []
@sectionHtml
@
Section:: =
turnOn: (before, after) ->
@loadStyles =>
before?()
@turnOnWidgets()
@insertIntoDOM()
@onInsert()
after?()
# Из-за того что браузеры отменяют загрузку ресурсов после удаления DOM элементов, то после
# смены секций мы возобновляем загрузку ресурсов за счет резолва html строки в DOM элемент.
dom @getSectionHtml()
loadStyles: (callback) ->
depList = []
headElement = dom('head')[0]
hasExternalPlugin = window.require?.specified "css"
for element in dom(@getSectionHtml()).find("[#{config.widgetCssAttributeName}]").get()
if element.getAttribute?
cssPath = element.getAttribute config.widgetCssAttributeName
cssPath = "#{cssPath.replace(extensionRegex, "")}.css" if cssPath?
if not hasExternalPlugin
linkNode = document.createElement "link"
linkNode.rel = "stylesheet"
linkNode.type = "text/css"
linkNode.href = cssPath
headElement.appendChild linkNode
depList.push "css!#{cssPath}"
if depList.length
if hasExternalPlugin
# Если загрузчик поддерживает обработчик завершения загрузки стилей, то чтоб стили
# точно успели применится делаем задержку в 100мс перед возобновлением пайплайна смены
# секций.
window.require depList, -> setTimeout callback, 100
else
console.warn "External plugin for loading css is not found. Creating direct links..."
callback?()
else
callback?()
turnOff: ->
@turnOffWidgets()
@removeFromDOM()
@onRemove()
removeFromDOM: ->
for element in @getSectionHtml()
element.parentNode.removeChild element if element.parentNode?
insertIntoDOM: ->
return unless @params.target
switch @params.target
when "icon"
return unless @element.href
try
newFavicon = document.createElement("link")
newFavicon.setAttribute "type", "image/ico"
newFavicon.setAttribute "rel", "shortcut icon"
newFavicon.setAttribute "href", @element.href
oldFavicon = dom('link[rel="shortcut icon"]')[0]
oldFavicon.parentNode.replaceChild newFavicon, oldFavicon if oldFavicon?
else
container = dom(@params.target)[0]
return unless container?
# говорим контейнеру, мол, теперь внутри вот такая-то секция.
container.setAttribute config.sectionSelectorAttributeName, "#{@name}: #{JSON.stringify @params}"
for element in @getSectionHtml()
container.appendChild element
turnOnWidgets: ->
loader.search @getSectionHtml()
turnOffWidgets: ->
for data in widgetsData @getSectionHtml()
widgets.get(data.name, data.element)?.sleepDown()
onInsert: ->
postfix = "inserted"
@notifyAll postfix
@processNamespaces postfix
onRemove: ->
postfix = "removed"
@notifyAll postfix
@processNamespaces postfix
processNamespaces: (postfix) ->
return if not @params.ns?
@params.ns = [@params.ns] if _.isString @params.ns
@notifyAll(postfix, "-#{type}") for type in @params.ns
notifyAll: (postfix, suffix, params) ->
return unless postfix
triggerParams = [@]
triggerParams.push params if params?
events.trigger "section#{suffix}:#{postfix}", triggerParams
Section