ystep.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* ========================================================================
  2. * Bootstrap: tooltip.js v3.0.3
  3. * http://getbootstrap.com/javascript/#tooltip
  4. * Inspired by the original jQuery.tipsy by Jason Frame
  5. * ========================================================================
  6. * Copyright 2013 Twitter, Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ======================================================================== */
  20. +function ($) { "use strict";
  21. // TOOLTIP PUBLIC CLASS DEFINITION
  22. // ===============================
  23. var Tooltip = function (element, options) {
  24. this.type =
  25. this.options =
  26. this.enabled =
  27. this.timeout =
  28. this.hoverState =
  29. this.$element = null
  30. this.init('tooltip', element, options)
  31. }
  32. Tooltip.DEFAULTS = {
  33. animation: true
  34. , placement: 'top'
  35. , selector: false
  36. , template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
  37. , trigger: 'hover focus'
  38. , title: ''
  39. , delay: 0
  40. , html: false
  41. , container: false
  42. }
  43. Tooltip.prototype.init = function (type, element, options) {
  44. this.enabled = true
  45. this.type = type
  46. this.$element = $(element)
  47. this.options = this.getOptions(options)
  48. var triggers = this.options.trigger.split(' ')
  49. for (var i = triggers.length; i--;) {
  50. var trigger = triggers[i]
  51. if (trigger == 'click') {
  52. this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
  53. } else if (trigger != 'manual') {
  54. var eventIn = trigger == 'hover' ? 'mouseenter' : 'focus'
  55. var eventOut = trigger == 'hover' ? 'mouseleave' : 'blur'
  56. this.$element.on(eventIn + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
  57. this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
  58. }
  59. }
  60. this.options.selector ?
  61. (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
  62. this.fixTitle()
  63. }
  64. Tooltip.prototype.getDefaults = function () {
  65. return Tooltip.DEFAULTS
  66. }
  67. Tooltip.prototype.getOptions = function (options) {
  68. options = $.extend({}, this.getDefaults(), this.$element.data(), options)
  69. if (options.delay && typeof options.delay == 'number') {
  70. options.delay = {
  71. show: options.delay
  72. , hide: options.delay
  73. }
  74. }
  75. return options
  76. }
  77. Tooltip.prototype.getDelegateOptions = function () {
  78. var options = {}
  79. var defaults = this.getDefaults()
  80. this._options && $.each(this._options, function (key, value) {
  81. if (defaults[key] != value) options[key] = value
  82. })
  83. return options
  84. }
  85. Tooltip.prototype.enter = function (obj) {
  86. var self = obj instanceof this.constructor ?
  87. obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  88. clearTimeout(self.timeout)
  89. self.hoverState = 'in'
  90. if (!self.options.delay || !self.options.delay.show) return self.show()
  91. self.timeout = setTimeout(function () {
  92. if (self.hoverState == 'in') self.show()
  93. }, self.options.delay.show)
  94. }
  95. Tooltip.prototype.leave = function (obj) {
  96. var self = obj instanceof this.constructor ?
  97. obj : $(obj.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type)
  98. clearTimeout(self.timeout)
  99. self.hoverState = 'out'
  100. if (!self.options.delay || !self.options.delay.hide) return self.hide()
  101. self.timeout = setTimeout(function () {
  102. if (self.hoverState == 'out') self.hide()
  103. }, self.options.delay.hide)
  104. }
  105. Tooltip.prototype.show = function () {
  106. var e = $.Event('show.bs.'+ this.type)
  107. if (this.hasContent() && this.enabled) {
  108. this.$element.trigger(e)
  109. if (e.isDefaultPrevented()) return
  110. var $tip = this.tip()
  111. this.setContent()
  112. if (this.options.animation) $tip.addClass('fade')
  113. var placement = typeof this.options.placement == 'function' ?
  114. this.options.placement.call(this, $tip[0], this.$element[0]) :
  115. this.options.placement
  116. var autoToken = /\s?auto?\s?/i
  117. var autoPlace = autoToken.test(placement)
  118. if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
  119. $tip
  120. .detach()
  121. .css({ top: 0, left: 0, display: 'block' })
  122. .addClass(placement)
  123. this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
  124. var pos = this.getPosition()
  125. var actualWidth = $tip[0].offsetWidth
  126. var actualHeight = $tip[0].offsetHeight
  127. if (autoPlace) {
  128. var $parent = this.$element.parent()
  129. var orgPlacement = placement
  130. var docScroll = document.documentElement.scrollTop || document.body.scrollTop
  131. var parentWidth = this.options.container == 'body' ? window.innerWidth : $parent.outerWidth()
  132. var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
  133. var parentLeft = this.options.container == 'body' ? 0 : $parent.offset().left
  134. placement = placement == 'bottom' && pos.top + pos.height + actualHeight - docScroll > parentHeight ? 'top' :
  135. placement == 'top' && pos.top - docScroll - actualHeight < 0 ? 'bottom' :
  136. placement == 'right' && pos.right + actualWidth > parentWidth ? 'left' :
  137. placement == 'left' && pos.left - actualWidth < parentLeft ? 'right' :
  138. placement
  139. $tip
  140. .removeClass(orgPlacement)
  141. .addClass(placement)
  142. }
  143. var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
  144. this.applyPlacement(calculatedOffset, placement)
  145. this.$element.trigger('shown.bs.' + this.type)
  146. }
  147. }
  148. Tooltip.prototype.applyPlacement = function(offset, placement) {
  149. var replace
  150. var $tip = this.tip()
  151. var width = $tip[0].offsetWidth
  152. var height = $tip[0].offsetHeight
  153. // manually read margins because getBoundingClientRect includes difference
  154. var marginTop = parseInt($tip.css('margin-top'), 10)
  155. var marginLeft = parseInt($tip.css('margin-left'), 10)
  156. // we must check for NaN for ie 8/9
  157. if (isNaN(marginTop)) marginTop = 0
  158. if (isNaN(marginLeft)) marginLeft = 0
  159. offset.top = offset.top + marginTop
  160. offset.left = offset.left + marginLeft
  161. $tip
  162. .offset(offset)
  163. .addClass('in')
  164. // check to see if placing tip in new offset caused the tip to resize itself
  165. var actualWidth = $tip[0].offsetWidth
  166. var actualHeight = $tip[0].offsetHeight
  167. if (placement == 'top' && actualHeight != height) {
  168. replace = true
  169. offset.top = offset.top + height - actualHeight
  170. }
  171. if (/bottom|top/.test(placement)) {
  172. var delta = 0
  173. if (offset.left < 0) {
  174. delta = offset.left * -2
  175. offset.left = 0
  176. $tip.offset(offset)
  177. actualWidth = $tip[0].offsetWidth
  178. actualHeight = $tip[0].offsetHeight
  179. }
  180. this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
  181. } else {
  182. this.replaceArrow(actualHeight - height, actualHeight, 'top')
  183. }
  184. if (replace) $tip.offset(offset)
  185. }
  186. Tooltip.prototype.replaceArrow = function(delta, dimension, position) {
  187. this.arrow().css(position, delta ? (50 * (1 - delta / dimension) + "%") : '')
  188. }
  189. Tooltip.prototype.setContent = function () {
  190. var $tip = this.tip()
  191. var title = this.getTitle()
  192. $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
  193. $tip.removeClass('fade in top bottom left right')
  194. }
  195. Tooltip.prototype.hide = function () {
  196. var that = this
  197. var $tip = this.tip()
  198. var e = $.Event('hide.bs.' + this.type)
  199. function complete() {
  200. if (that.hoverState != 'in') $tip.detach()
  201. }
  202. this.$element.trigger(e)
  203. if (e.isDefaultPrevented()) return
  204. $tip.removeClass('in')
  205. $.support.transition && this.$tip.hasClass('fade') ?
  206. $tip
  207. .one($.support.transition.end, complete)
  208. .emulateTransitionEnd(150) :
  209. complete()
  210. this.$element.trigger('hidden.bs.' + this.type)
  211. return this
  212. }
  213. Tooltip.prototype.fixTitle = function () {
  214. var $e = this.$element
  215. if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
  216. $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
  217. }
  218. }
  219. Tooltip.prototype.hasContent = function () {
  220. return this.getTitle()
  221. }
  222. Tooltip.prototype.getPosition = function () {
  223. var el = this.$element[0]
  224. return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
  225. width: el.offsetWidth
  226. , height: el.offsetHeight
  227. }, this.$element.offset())
  228. }
  229. Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
  230. return placement == 'bottom' ? { top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2 } :
  231. placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 - 20 } :
  232. placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
  233. /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
  234. }
  235. Tooltip.prototype.getTitle = function () {
  236. var title
  237. var $e = this.$element
  238. var o = this.options
  239. title = $e.attr('data-original-title')
  240. || (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
  241. return title
  242. }
  243. Tooltip.prototype.tip = function () {
  244. return this.$tip = this.$tip || $(this.options.template)
  245. }
  246. Tooltip.prototype.arrow = function () {
  247. return this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow')
  248. }
  249. Tooltip.prototype.validate = function () {
  250. if (!this.$element[0].parentNode) {
  251. this.hide()
  252. this.$element = null
  253. this.options = null
  254. }
  255. }
  256. Tooltip.prototype.enable = function () {
  257. this.enabled = true
  258. }
  259. Tooltip.prototype.disable = function () {
  260. this.enabled = false
  261. }
  262. Tooltip.prototype.toggleEnabled = function () {
  263. this.enabled = !this.enabled
  264. }
  265. Tooltip.prototype.toggle = function (e) {
  266. var self = e ? $(e.currentTarget)[this.type](this.getDelegateOptions()).data('bs.' + this.type) : this
  267. self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
  268. }
  269. Tooltip.prototype.destroy = function () {
  270. this.hide().$element.off('.' + this.type).removeData('bs.' + this.type)
  271. }
  272. // TOOLTIP PLUGIN DEFINITION
  273. // =========================
  274. var old = $.fn.tooltip
  275. $.fn.tooltip = function (option) {
  276. return this.each(function () {
  277. var $this = $(this)
  278. var data = $this.data('bs.tooltip')
  279. var options = typeof option == 'object' && option
  280. if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
  281. if (typeof option == 'string') data[option]()
  282. })
  283. }
  284. $.fn.tooltip.Constructor = Tooltip
  285. // TOOLTIP NO CONFLICT
  286. // ===================
  287. $.fn.tooltip.noConflict = function () {
  288. $.fn.tooltip = old
  289. return this
  290. }
  291. }(jQuery);
  292. /* ========================================================================
  293. * Bootstrap: popover.js v3.0.3
  294. * http://getbootstrap.com/javascript/#popovers
  295. * ========================================================================
  296. * Copyright 2013 Twitter, Inc.
  297. *
  298. * Licensed under the Apache License, Version 2.0 (the "License");
  299. * you may not use this file except in compliance with the License.
  300. * You may obtain a copy of the License at
  301. *
  302. * http://www.apache.org/licenses/LICENSE-2.0
  303. *
  304. * Unless required by applicable law or agreed to in writing, software
  305. * distributed under the License is distributed on an "AS IS" BASIS,
  306. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  307. * See the License for the specific language governing permissions and
  308. * limitations under the License.
  309. * ======================================================================== */
  310. +function ($) { "use strict";
  311. // POPOVER PUBLIC CLASS DEFINITION
  312. // ===============================
  313. var Popover = function (element, options) {
  314. this.init('popover', element, options)
  315. }
  316. if (!$.fn.tooltip) throw new Error('Popover requires tooltip.js')
  317. Popover.DEFAULTS = $.extend({} , $.fn.tooltip.Constructor.DEFAULTS, {
  318. placement: 'right'
  319. , trigger: 'click'
  320. , content: ''
  321. , template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
  322. })
  323. // NOTE: POPOVER EXTENDS tooltip.js
  324. // ================================
  325. Popover.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype)
  326. Popover.prototype.constructor = Popover
  327. Popover.prototype.getDefaults = function () {
  328. return Popover.DEFAULTS
  329. }
  330. Popover.prototype.setContent = function () {
  331. var $tip = this.tip()
  332. var title = this.getTitle()
  333. var content = this.getContent()
  334. $tip.find('.popover-title')[this.options.html ? 'html' : 'text'](title)
  335. $tip.find('.popover-content')[this.options.html ? 'html' : 'text'](content)
  336. $tip.removeClass('fade top bottom left right in')
  337. // IE8 doesn't accept hiding via the `:empty` pseudo selector, we have to do
  338. // this manually by checking the contents.
  339. if (!$tip.find('.popover-title').html()) $tip.find('.popover-title').hide()
  340. }
  341. Popover.prototype.hasContent = function () {
  342. return this.getTitle() || this.getContent()
  343. }
  344. Popover.prototype.getContent = function () {
  345. var $e = this.$element
  346. var o = this.options
  347. return $e.attr('data-content')
  348. || (typeof o.content == 'function' ?
  349. o.content.call($e[0]) :
  350. o.content)
  351. }
  352. Popover.prototype.arrow = function () {
  353. return this.$arrow = this.$arrow || this.tip().find('.arrow')
  354. }
  355. Popover.prototype.tip = function () {
  356. if (!this.$tip) this.$tip = $(this.options.template)
  357. return this.$tip
  358. }
  359. // POPOVER PLUGIN DEFINITION
  360. // =========================
  361. var old = $.fn.popover
  362. $.fn.popover = function (option) {
  363. return this.each(function () {
  364. var $this = $(this)
  365. var data = $this.data('bs.popover')
  366. var options = typeof option == 'object' && option
  367. if (!data) $this.data('bs.popover', (data = new Popover(this, options)))
  368. if (typeof option == 'string') data[option]()
  369. })
  370. }
  371. $.fn.popover.Constructor = Popover
  372. // POPOVER NO CONFLICT
  373. // ===================
  374. $.fn.popover.noConflict = function () {
  375. $.fn.popover = old
  376. return this
  377. }
  378. }(jQuery);
  379. (function($){
  380. $.fn.extend({
  381. //初始化
  382. loadStep: function(params){
  383. //基础框架
  384. var baseHtml = "<div class='ystep-container'>"+
  385. "<ul class='ystep-container-steps'>"+
  386. "</ul>"+
  387. "<div class='ystep-progress'>"+
  388. "<p class='ystep-progress-bar'>"+
  389. "<span class='ystep-progress-highlight' style='width:0%'>"+
  390. "</span>"+
  391. "</p>"+
  392. "</div>"+
  393. "</div>";
  394. //步骤框架
  395. var stepHtml = "<li class='ystep-step ystep-step-undone' data-container='body' data-toggle='popover' data-placement='top' data-title='' data-content='' data-trigger='hover'>"+
  396. "</li>";
  397. //决策器
  398. var logic = {
  399. size: {
  400. small: function($html){
  401. var stepCount = $html.find("li").length-1,
  402. containerWidth = (stepCount*65+100)+"px",
  403. progressWidth = (stepCount*65)+"px";
  404. $html.css({
  405. width: containerWidth
  406. });
  407. $html.find(".ystep-progress").css({
  408. width: progressWidth
  409. });
  410. $html.find(".ystep-progress-bar").css({
  411. width: progressWidth
  412. });
  413. $html.addClass("ystep-sm");
  414. },
  415. large: function($html){
  416. var stepCount = $html.find("li").length-1,
  417. containerWidth = (stepCount*100+100)+"px",
  418. progressWidth = (stepCount*100)+"px";
  419. $html.css({
  420. width: containerWidth
  421. });
  422. $html.find(".ystep-progress").css({
  423. width: progressWidth
  424. });
  425. $html.find(".ystep-progress-bar").css({
  426. width: progressWidth
  427. });
  428. $html.addClass("ystep-lg");
  429. }
  430. },
  431. color: {
  432. green: function($html){
  433. $html.addClass("ystep-green");
  434. },
  435. blue: function($html){
  436. $html.addClass("ystep-blue");
  437. }
  438. }
  439. };
  440. //支持填充多个步骤容器
  441. $(this).each(function(i,n){
  442. var $baseHtml = $(baseHtml),
  443. $stepHtml = $(stepHtml),
  444. $ystepContainerSteps = $baseHtml.find(".ystep-container-steps"),
  445. arrayLength = 0,
  446. $n = $(n),
  447. i=0;
  448. //步骤
  449. arrayLength = params.steps.length;
  450. for(i=0;i<arrayLength;i++){
  451. var _s = params.steps[i];
  452. //构造步骤html
  453. $stepHtml.attr("data-title",_s.title);
  454. $stepHtml.attr("data-content",_s.content);
  455. $stepHtml.text(_s.title);
  456. //将步骤插入到步骤列表中
  457. $ystepContainerSteps.append($stepHtml);
  458. //重置步骤
  459. $stepHtml = $(stepHtml);
  460. }
  461. //尺寸
  462. logic.size[params.size||"small"]($baseHtml);
  463. //配色
  464. logic.color[params.color||"green"]($baseHtml);
  465. //插入到容器中
  466. $n.append($baseHtml);
  467. //渲染提示气泡
  468. $n.find(".ystep-step").popover({});
  469. //默认执行第一个步骤
  470. $n.setStep(1);
  471. });
  472. },
  473. //跳转到指定步骤
  474. setStep: function(step) {
  475. $(this).each(function(i,n){
  476. //获取当前容器下所有的步骤
  477. var $steps = $(n).find(".ystep-container").find("li");
  478. var $progress =$(n).find(".ystep-container").find(".ystep-progress-highlight");
  479. //判断当前步骤是否在范围内
  480. if(1<=step && step<=$steps.length){
  481. //更新进度
  482. var scale = "%";
  483. scale = Math.round((step-1)*100/($steps.length-1))+scale;
  484. $progress.animate({
  485. width: scale
  486. },{
  487. speed: 1000,
  488. done: function() {
  489. //移动节点
  490. $steps.each(function(j,m){
  491. var _$m = $(m);
  492. var _j = j+1;
  493. if(_j < step){
  494. _$m.attr("class","ystep-step-done");
  495. }else if(_j === step){
  496. _$m.attr("class","ystep-step-active");
  497. }else if(_j > step){
  498. _$m.attr("class","ystep-step-undone");
  499. }
  500. });
  501. }
  502. });
  503. }else{
  504. return false;
  505. }
  506. });
  507. },
  508. //获取当前步骤
  509. getStep: function() {
  510. var result = [];
  511. $(this)._searchStep(function(i,j,n,m){
  512. result.push(j+1);
  513. });
  514. if(result.length == 1) {
  515. return result[0];
  516. }else{
  517. return result;
  518. }
  519. },
  520. //下一个步骤
  521. nextStep: function() {
  522. $(this)._searchStep(function(i,j,n,m){
  523. $(n).setStep(j+2);
  524. });
  525. },
  526. //上一个步骤
  527. prevStep: function() {
  528. $(this)._searchStep(function(i,j,n,m){
  529. $(n).setStep(j);
  530. });
  531. },
  532. //通用节点查找
  533. _searchStep: function (callback) {
  534. $(this).each(function(i,n){
  535. var $steps = $(n).find(".ystep-container").find("li");
  536. $steps.each(function(j,m){
  537. //判断是否为活动步骤
  538. if($(m).attr("class") === "ystep-step-active"){
  539. if(callback){
  540. callback(i,j,n,m);
  541. }
  542. return false;
  543. }
  544. });
  545. });
  546. }
  547. });
  548. })(jQuery);